得到的数据通过串口传输过程中损坏 [英] Data gets corrupted during transmission over the serial port

查看:193
本文介绍了得到的数据通过串口传输过程中损坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个程序与旧系统进行通信。我用System.IO.Ports.SerialPort这一点。问题是,当我送一个较​​长的消息,bevome损坏的消息。我使用线路监听器,并得到下面的结果。

I am developing a program to communicate with an old system. I use System.IO.Ports.SerialPort for this. The problem is when I send a longer message, the message bevome corrupt. I use a line listener and get the following results

我发送

aa 01 00 00 12 03 06 18 02 c1 94 02 c1 94 00 00 00 00 00 00 00 00 00 00 00 00 1e fd 

我得到什么

c2 aa 01 00 00 12 03 06 18 02 c3 81 c2 94 02 c3 81 c2 94 00 00 00 00 00 00 00 00 00 00 00 00 1e c3 bd



我使用的代码是

The code I'm using is

_comPort.Encoding = new UTF8Encoding();
_comPort.PortName = PortName;  //Com1
_comPort.BaudRate = BaudRate;  //9600
_comPort.StopBits = StopBits;  //1
_comPort.DataBits = DataBits;  //8
_comPort.Parity   = Parity;    //None
_comPort.Open();

_comPort.Write(messageStr);



为什么数据损坏,我该如何解决这个问题?

Why is data corrupted and how do I fix this?

推荐答案

我的猜测是, messageStr 是一个字符串,而你看到的编码问题。您明确指定的UTF-8编码,所以这是你得到了什么 - 但我的犯罪嫌疑人的不是你真正想要的。

My guess is that messageStr is a string, and you're seeing encoding issues. You've explicitly specified the UTF-8 encoding, so that's what you're getting - but I suspect it's not what you really want.

您已经证明的二进制的数据,所以我假设你真的想送正是二进制数据 - 在这种情况下,你应该使用的 写入(字节[],INT,INT) 超载​​。

You've shown binary data, so I assume you actually want to send exactly that binary data - in which case you should use the Write(byte[], int, int) overload.

如果您的真正的要写入文本数据,你可能只需要挑选合适的编码 - 但你需要给我们更多的信息,以便我们帮助你做出正确的选择。

If you really want to write text data, you probably just need to pick the right encoding - but you'll need to give us more information for us to help you make the right choice.

这篇关于得到的数据通过串口传输过程中损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆