简体   繁体   English

rs232通讯经常出错

[英]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. 我知道这个问题并不新鲜,可以通过Google搜索一些示例代码/类来进行rs232通信。 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? 我可以从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. PS:顺便说一句,在我的项目中,我需要经常将字符串(字符数组)连续或在某些定义的实例中发送到另一个rs232终端。 Can the error be caused by not closing, reopening RS232/COM handle along with each time of communication? 错误可能是由于每次通信时不关闭,重新打开RS232 / COM手柄引起的吗?

Please, I've included my code 拜托,我已经包含了我的代码

RS232.cpp RS232.hpp RS232_client RS232.cpp RS232.hpp RS232_ 客户

Edit01: 编辑01:

RS232_client is the code written on the other side. RS232_client是另一侧写的代码。 My side is a PC using win7, project is compiled with MSVS2008 我这边是一台使用win7的PC,项目是用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 关于我的代码中的错误,有时是当我调用“ int RS232 :: Write(double * joints)”时,它会引发错误,并且传输停止,在PC端不显示任何内容

Edit02: Edit02:

As @Chris Stratton suggests, my problem might be from bad written rs232 class without necessary checking. 就像@克里斯·斯特拉顿(Chris Stratton)所建议的那样,我的问题可能是由于书面rs232类错误而没有进行必要的检查。 So I tested my project with another class downloaded from CodeProject CSerial. 因此,我使用从CodeProject CSerial下载的另一个类测试了我的项目。 I put the code here for reference 我把代码放在这里供参考

Serial.cpp Serial.h 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. 因此,从PC端开始的过程是相同的,即发送字符串,等待读取“ s”或“ S”,然后发送“ q \\ r”退出。

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 对于上面用此新代码执行的3个步骤,我可以执行步骤1的OK,但是对于步骤2,它挂在那里,好像没有引发(读取)事件一样

        // 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. 我终于可以拥有一个RS232模块,该模块可以释放奇怪的突然停止。 Here I upload the new version of RS232.cpp 在这里,我上传了新版本的RS232.cpp

The only different between the two versions is in new RS232_open(), I do not use GetCommState. 这两个版本之间唯一的区别是在新的RS232_open()中,我不使用GetCommState。 Instead, the configuration is set by BuildCommDCBA using a string of setting baudr 而是由BuildCommDCBA使用一串设置baudr来设置配置

Thanks for the help of SO! 感谢您的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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