本文介绍了发送消息时出现 0x0A 和 0x0D 的 RS232 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行编写消息并通过 RS232 总线将其发送到设备的代码.

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

当消息中有 0A(换行)或 0D(回车)字符(或两者都有)时,我遇到了问题.

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

阅读方告诉我它不理解消息(通过向我发送 NAK 响应).我推断问题出在这两个字符上.

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.

即:

022620255846060A00003003
022620255946060D00003703

有没有想过如何在不更改 0A 或 0D 的情况下发送相同的信息?

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);

$msg 等于上述代码之一,$ob_w 是端口本身.

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

谢谢

推荐答案

在这种情况下,这是与串行端口配置相关的问题.

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

通过改变

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

到:

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

将修复该消息.上面的一些命令将 0x0A 和 0x0D 更改为 0x0A0D,因此在这种特殊情况下,接收机器计算校验和并丢弃消息,因为接收和生成的校验和不相同.

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.

这篇关于发送消息时出现 0x0A 和 0x0D 的 RS232 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-30 10:35