Read/Write Com Port :: C++

kuphryn

Senior member
Jan 7, 2001
400
0
0
Hi.
Is it easy to read from and write to the com port using C++? Is there a library for it?

I heard of functions CreateFile, ReadFile and WriteFile from members at a couple of forums. There are functions inp() and outp() from conio.h too. Does anyone know the difference between these functions?

Thanks,
Kuphryn
 

kuphryn

Senior member
Jan 7, 2001
400
0
0
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<char *> (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
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
Maybe it's just my lack of C++ expertise, but why not just use read() and write()? They return how many bytes they processed.