使用 SerialPort.ReadExisting() 从串行端口接收无效数据
我在 .NET 中编写了一个程序,用于监听特定的串行端口并处理正在接收的数据。我使用 com0com(空调制解调器模拟器)编写了一个测试程序,并且我的程序运行良好。我什至用超级终端测试过它,似乎也工作得很好。
然而,当我将软件与原始设备(从控制系统接收到的输出)连接时,接收到的数据是乱码。它包含特殊字符。与超级终端连接时的同一设备会产生所需的输出。我更改了波特率、奇偶校验等,但收到的数据是同一组垃圾字符。
我使用了 SerialPort 组件的 DataReceived 事件,并使用以下代码行来捕获数据:
string data = portRecieve.ReadExisting();
Can somebody tell me where am i missing out? In the current environment, the output from the device is directly connected with a dot matrix printer which prints whatever is received on the port. The printer seems to catch what is being sent but my code couldn't.
If you ever encountered a similar scenario, Please share your findings.
Thanks
I have written a program in .NET that listens to a particular Serial Port and processes the data that is being received. I wrote a test program using com0com (the Null-modem emulator) and my program was working fine. I even tested it with HyperTerminal and that seemed to work fine too.
However when I interfaced the software with the original device (an output received from a control system), the data received was garbled. It contained special characters. The same device when connected with Hyper Terminal produced the desired output. I changed the baud rates, parity etc but the data received was the same set of garbage characters.
I have used the DataReceived event of the SerialPort component and used the following line of code to capture data:
string data = portRecieve.ReadExisting();
Can somebody tell me where am i missing out? In the current environment, the output from the device is directly connected with a dot matrix printer which prints whatever is received on the port. The printer seems to catch what is being sent but my code couldn't.
If you ever encountered a similar scenario, Please share your findings.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是如何设置的
也许向我们展示您收到的特殊字符的示例。
How did you set
And maybe show us an example of the special chars you are receiving.
我可以想到数据可能出现乱码的以下原因:
如果物理连接不良,有时可能会得到垃圾(而不是什么也没有)。尝试拔下并重新插入导线 - 并检查导线是否正确(例如,您是否需要 nullmodem?)。看来您通过检查超级终端已经解决了这个问题。
如果波特率、停止位、奇偶校验不正确 - 听起来您已经涵盖了这一点
您正在尝试以字符串形式接收数据。如果它不是以纯文本形式发送,或者您的编码错误,那么它很容易看起来像垃圾。尝试使用二进制接收并检查您正在接收的原始数据。这将告诉您数据是错误的还是 .net 转换搞砸了 - 消除中间人!
I can think of the following reasons why the data might apperar garbled:
If there is a bad physical connection, you can sometimes just get garbage (rather than nothing at all). Try unplugging and replugging the leads - and check that you have the correct lead (e.g. do you need a nullmodem?). It looks as though you have this covered by checking in HyperTerminal.
If the baud rate, stop bits, parity are not correct - sounds like you have this one covered
You are trying to receive the data as a string. If it is not sent as plain text, or if your encoding is wrong, then it could easily appear garbage-like. Try using a binary receive and examine the raw data that you are receiving. This will tell you whether the data is just wrong or the .net conversion is screwing it up - eliminate the middle man!
在我看来,该设备正在将打印机置于某种特殊的图形模式。如果是这样,发送到打印机的数据中可能存在转义序列,即。以转义 (27, 0x1B) 字符开头的字符序列。
在这种情况下,您必须查看打印机手册以了解命令的作用。或者,您可以告诉设备使用简单的纯 ASCII 打印机,而不是智能打印机。
It sounds to me like the device is putting the printer into some special graphics mode. If so, there is likely to be escape sequences in the data being sent to the printer, ie. character sequences starying with an escape (27, 0x1B) character.
In this case, you'll have to look at the printer manual to see what the commands do. Alternatively, you might be able to tell the device to use a simple ASCII only printer, rather than a intellifent one.