本文介绍了无法从串口获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用串口进行锻炼.我已经在我的程序中插入了这段代码.我能够向设备发送数据,但无法从设备读取数据.在调试模式下,我只能得到 serialport.BytesToRead = 0.我可以知道为什么会发生这种情况吗?

I am working out with serial com port.I have insert this code in my program.I able to send data to the devices and fail to read data from the devices.In debug mode, i only able to get serialport.BytesToRead = 0.May i know why this will happen??

   while (serialport.BytesToRead > 0)
{
      int byte_count = serialport.BytesToRead;
      byte[] buffer = new byte[byte_count];
      int read_count = serialport.Read(buffer, 0, byte_count);
      string echo = ASCIIEncoding.ASCII.GetString(buffer, 0, read_count);
      echo = System.Text.Encoding.UTF8.GetString(buffer);
      Console.WriteLine(echo);
}

推荐答案

首先使用另一个程序,如 Putty 或 HyperTerminal 来验证设备和连接是否正常工作,并仔细检查您使用的端口是否正确,波特率、奇偶校验、停止位和数据位.如果您无法使用此类程序从设备中获取任何信息,那么使用您自己的代码也无法运行.

First use another program, like Putty or HyperTerminal to verify that the device and connection is in working order and to double-check that you are using the correct port, baudrate, parity, stopbits and databits. If you can't get anything out of the device with such a program then it won't work either using your own code.

接下来关注握手.一个常见的错误是将其置为无,然后不打开 DtrEnable 和 RtsEnable 信号.设备在认为您离线时不会发送任何内容.SysInternals 的 PortMon 实用程序 很方便,它可以显示设备上发生的事情司机级别.

Next focus on the handshaking. A common mistake is to leave it at none and then not turn on the DtrEnable and RtsEnable signals. A device won't send anything when it thinks that you're offline. SysInternals' PortMon utility can be handy, it shows you what's going on at the device driver level.

这篇关于无法从串口获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 01:25