Read/Write Com Port :: C++

Started by
4 comments, last by kuphryn 22 years, 4 months ago
Hi. Is it easy to read from and write to the com port using C++? Is there a library for it? Thanks, Kuphryn
Advertisement
Most communications is OS specific

Sometimes you can open a file to com1, com2, etc...

But this doesn''t give you must control over how it''s opened etc...
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Thanks.

What do you mean "open to?"

ifstream readCOM;
readCOM.open(com1, ios::in | ios::binary);

readCOM << ''test'';

Is that you are talking about?

Kuphryn
more or less, I''m used to seeing fopen with that, but I guess the iostreams should kinda-sort work.
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
C++ Style:

ifstream readCOM;
readCOM.open(com1, ios::in | ios::binary)

char *ptrStore;

try
{
ptrStore = new char[SIZE];
}
catch (bad_alloc error)
{
cerr << error.what();
exit(1);
}

readCOM.read(reinterpret_cast (ptrStore), SIZE);

How do you get the exact size of whatever you're reading from the COM port? It is not a measurable storage container.

Do I have to use an MFC class? What is the major difference if I were to use the ifstream and ofstream instead of an MFC class? I have no experience with MFC or windows programming.

Kuphryn

Edited by - kuphryn on December 27, 2001 1:12:10 PM
The biggest problem is that you have no control over the baud rate, secondly you can''t check the modem/line status bits.

You need to look into the Win32 communication functions
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

This topic is closed to new replies.

Advertisement