简体   繁体   中英

rs232 communication creates error frequently

I know this question is not new and it's possible to google for some example codes/class to do rs232 communication. However, now I'm already using a class. But it works sometimes but very often don't. The communication just stops It will take me a great deal of time to read all serial communication concepts and debug this code

Could I have some help from SO?

Thanks,

PS: by the way, in my project, I need to frequently send a string (array of char) to another rs232 terminal continuously or at some defined instances. Can the error be caused by not closing, reopening RS232/COM handle along with each time of communication?

Please, I've included my code

RS232.cpp RS232.hpp RS232_client

Edit01:

RS232_client is the code written on the other side. My side is a PC using win7, project is compiled with MSVS2008

About the error in my code, it's sometimes when I call "int RS232::Write(double* joints)", it raises the error and the transmission stops, showing nothing on PC's side

Edit02:

As @Chris Stratton suggests, my problem might be from bad written rs232 class without necessary checking. So I tested my project with another class downloaded from CodeProject CSerial. I put the code here for reference

Serial.cpp Serial.h

And this is how I adapt this class to my code:

my adaptation

So, the process from PC side is the same, ie, send a string, wait to read 's' or 'S', and send a "q\\r" for quitting.

for the 3 steps above with this new code, I can do step 1 OK, but for step 2, it hangs there as if no (reading) event is evoked

        // Wait for an event
        lLastError = serialPort.WaitEvent();   // it waits here forever!!!!
        if (lLastError != ERROR_SUCCESS)
            ShowError(serialPort.GetLastError(), _T("Unable to wait for a COM-port event."));

I finally can have an RS232 module that works freeing of the weird sudden stopping. Here I upload the new version of RS232.cpp

The only different between the two versions is in new RS232_open(), I do not use GetCommState. Instead, the configuration is set by BuildCommDCBA using a string of setting baudr

Thanks for the help of SO!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM