Read/Write Com Port :: C++

Status
Not open for further replies.
C++ has no facilities for dealing with hardware like COM ports - that'd make it highly unportable. You will need to use a library/API provided by your OS or compiler vendor. I'd hazard a guess that the majority of such libraries would be designed for use by C callers (it being a low-level language of choice for standard hardware), so they'd be fairly trivial to use from C++.
 
Thanks.

I read the first few sections of the article: Serial Communications in Win32 from MSDN. I am lost as far as windows program. I have never program a windows program. Everything I have done to this point has been win32 console, i.e. command prompt. The article discusses windows program and recommend that the read user know multithreading.

Sounds like it is time to look at buy more books. I am still reading Stroutrup. Plus, I want to learn GUI next.

Kuphryn
 

earl

Well-known member
3,066
Um, no. As PeterB pointed out, you can read COM ports like a file. I'll leave it as an exercise for you to figure out how... but it isn't hard. The advantage to C style file i/o is that it is, IMO, more straightforward. The only difficulties you will encounter are these:

  • if you want to set timeouts, you'll either have to use mode from a DOS prompt -- which unfortunately doesn't allow you to change all the settings, especially timeouts AFAIK -- or use CreateFile to open the com port, the appropriate win32 functions to change the settings, closehandle, then open with normal file i/o
  • Win32, because of a silly design decision, uses two characters as a newline constant. What this means to you is if you say, fprintf(fComPort, "bleh\n"), you will get more characters than you would think. And the same in reverse -- it will transparently change 10 13 to \n. Or maybe it's 13 10 -- I can never remember.


-earl-
 
Okay. Thanks.

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.

Kuphryn
 
I think it is stupid because they used two characters instead of one. This leads to the same problem unicode was designed to solve: with multibyte characters you can't tell the length of a string (c style, that is) without actually walking the entire array. Dumb as hell.
The problem is with ASCII; using anything other than a CRLF pair would be dumb.

But....

How do you tell how long a C-string is without walking the length of the array?

That's what all the <span style="font-family: 'Lucida Console', monospace;">strlen()</span>s I've seen do.
 
Originally posted by earl:
OK, bad example. I have a large buffer. I want to modify character N. With ASCII and two character line terminators, I have to walk all characters < N and potentially up to 2N to see where exactly in my array the Nth is.
I'm not entirely sure where this would be an issue; the Nth character is at offset N-1, always. I guess I've never come across a situation where I want to change the character that's N-places-assuming-that-CRLF-pairs-are-elided-into-a-single-character (normally I'd work with offsets that were correct no matter what, e.g. as returned by a find function, or I'm working on lines at a time and so have no embedded newlines). I'm not really seeing why I'd want to, either.
 
Status
Not open for further replies.