RS232 issue with 0x0A and 0x0D while sending a message

╄→гoц情女王★ 提交于 2019-12-25 00:04:23

问题


I'm running a code that composes a message and send it over RS232 bus to a device.

I'm having issues when the message has 0A (new line) or 0D (carriage return) chars (or both) on the message.

The reading side is telling me that it doesn't understand the message (by sending me a NAK response). I've deduced that the issue is with those two chars.

ie:

022620255846060A00003003
022620255946060D00003703

Any thoughts of how may I send the same information without having to change the 0A or 0D?

The code that I'm using for sending the message is this:

my $stt = pack 'H*',$msg;
$ob_w->write($stt);

being $msg equal to one of the above codes and $ob_w is the port itself.

Thanks


回答1:


When outputting binary data with perl, it seems that it is necessary to set it to binmode and then output it by print. Please refer to the following article.

How to work with binary data in a perl script


In Addition:

Input and Output in Binary Mode 1
Input and Output in Binary Mode 2
How to read and write the serial port using perl
Accessing Serial port on windows and Linux
perlport
Handling Binary Files in Perl




回答2:


In this case, this was an issue related with serial port configuration.

By changing from

$ob_w->stty_icrnl (1);
$ob_w->stty_ocrnl (1);
$ob_w->stty_onlcr (1);
$ob_w->stty_opost (1);

to:

$ob_w->stty_icrnl (0);
$ob_w->stty_ocrnl (0);
$ob_w->stty_onlcr (0);
$ob_w->stty_opost (0);

will fix the message. Some of the orders from above was changing 0x0A and 0x0D to 0x0A0D, so in this particular case, the receiving machine calculates the checksum and discard the message because the checksum received and generated was not the same.



来源:https://stackoverflow.com/questions/52076840/rs232-issue-with-0x0a-and-0x0d-while-sending-a-message

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!