I usually retrieve the read & write buffer size of the hardware, adjust it if it has ridiculous values, then use that as the length of my reads. Also, hang big reads - let the port give you as much as it has in one gulp.
In this context, each of those 3 choices is incorrect. DO NOT set it so that it 1) returns immediately or 2) returns after a specific interval or 3) returns after x-number of bytes have been received. Set the read timeout such that it waits until one data byte is available. hanging a blocking read in a loop will NOT make your program CPU-busy UNLESS your port timeouts are set incorrectly (effectively making it a nonblocking read). HOWEVER - your first statement indicates a different problem.
#Serial library c++ windows code#
if you're using ANY polling in code like this, it is a clear sign of a design defect. The code will be somewhat verbose, but it will work perfectly, will not lose characters as long as your ports are sufficiently buffered (check the hardware buffering capabilities if you wish, and adjust the buffering if necessary and if doing so is supported by your comm port hardware), and will contain no polling at all. Overlapped I/O is the preferred method of comm port I/O in Windows. (And, by "handle it", i mean put incoming data in a queue somewhere and set an event or send a message for the main program thread to pick up the data - NOT "plot the trajectory of the comet described by the incoming data")
You'll always be hanging a new read, of course, unless you're shutting down the comm. If there is no I/O of that type pending (e.g., you've written all you have to write for now) then just don't hang any new write until you have more data. this can correctly be done with either one thread (doing all comm port I/O, which i have found to work quite nicely) or with up to 3 threads (one for input, one for output, one for status changes, if important) but i found that the latter was way way overkill.Īfter the various I/O's are hung on the port: wait for one of them to finish, handle it as quickly as is reasonable, and then hang the next I/O to replace the one you just completed. OsWrite.Ideally, all 3 of the basic comm I/O tasks should be performed using overlapped I/O. Create this write operation's OVERLAPPED structure's hEvent.
In the mean time here is my code to take a peek at it. Let me check out your link and read them carefully. Not to mention that I used MSDN WIN32 codes to write this piece of code given by the following link. I am trying to find out what the problem is! Perhaps by checking the firmware of my meter! In the mean time do you guys have any idea why my code is not working in a way that it was expected? But it looks exactly the same as Tera Term! The buffer is totally blank! This tells me that I read nothing! So, my home-made terminal is acting differently when it sends the characters. I went through the code and realized that my buffer cannot get filled.
Sending and receiving data is absolutely fine as it is expected! Using Tera-Term it is absolutely working in a real-time basis.
#Serial library c++ windows plus#
However, when I use my device whichĪs an electricity meter and suppose to receive data when I send "N" character plus the proper address returning nothing! Testing this code using a NULL Cable and a Laptop to send and receive data was successful. I wrote a piece of code to communicate with outside world through a RS232 Cable using Visual C++ WIN32 Console Application.