简体   繁体   English

如何从串行端口读取设备数据

[英]How to Read Device Data From serial port

I have one device which sends data on COM port say on COM13. 我有一台设备通过COM13上的COM端口发送数据。 Now i want to read that data and display it in the RichTextBox or in any text control. 现在,我想读取该数据并将其显示在RichTextBox或任何文本控件中。

I have written the application with the help of IO and IO.Ports but comport.DataRecived event does not fire, even though device is sending data on that port. 我已经在IO和IO.Ports的帮助下编写了应用程序,但是即使设备正在该端口上发送数据,comport.DataRecived事件也不会触发。

I have some software on which i define the port number and it successfully display data, which insure me that data is receiving on the Port but i am unable to receive. 我有一些软件可以定义端口号,并且可以成功显示数据,这可以确保我在端口上接收到数据,但是无法接收。

Is there any way i can read data? 有什么办法可以读取数据?

comm.Parity = cboParity.Text;//None
comm.StopBits = cboStop.Text;//One
comm.DataBits = cboData.Text;//8
comm.BaudRate = cboBaud.Text;//9600
comm.DisplayWindow = rtbDisplay;//Null
comm.PortName = "COM13";
comm.OpenPort();

cmdOpen.Enabled = false;
cmdClose.Enabled = true;
cmdSend.Enabled = true;

public bool OpenPort()
{
    if (comPort.IsOpen)
    {
        comPort.Close();
    }

    comPort.DataReceived += new SerialDataReceivedEventHandler(comPort_DataReceived);
    comPort.PortName = _portName;
    comPort.Open();return true;
}

This normally comes from a wrong configuration of a serial port. 这通常是由于串口配置错误所致。 It is not enough to simple open a serial port and waiting for some data to come in. You have also to set all the SerialPort.Properties to a correct value for your wanted connection. 仅打开一个串行端口并等待一些数据输入是不够的。您还必须将所有 SerialPort.Properties设置为所需连接的正确值。

Some of the common ones are BaudRate , DataBits or Parity , but to be really sure you have to set all of them. 一些常见的是BaudRateDataBitsParity ,但是要确保您必须全部设置它们。 Even such things as RtsEnable or ReadTimeout . 甚至诸如RtsEnableReadTimeout类的东西。

You have to set the all, cause the configuration state will be saved from the port itself. 您必须设置所有,因为配置状态将从端口本身保存。 So if one application opens such a port, makes some changes to the configuration and closes it, the next application that opens the port starts with this configuration, till it change it. 因此,如果一个应用程序打开了这样的端口,对配置进行了一些更改并将其关闭,则下一个打开端口的应用程序将从此配置开始,直到更改为止。

Update 更新

Seems to be a problem i can't see from here. 似乎是一个问题,我从这里看不到。 ;-)) ;-))

The only advice i can give you is to use a Monitor tool , to better understand what your other application really does and what comes on the wire. 我能给您的唯一建议是使用Monitor工具 ,以更好地了解您的其他应用程序的实际功能以及网络上的功能。 Additionally you can set up two virtual com ports to test reading and writing on one machine (even within the same application), to have a better control about when will which data be send. 此外,您可以设置两个虚拟com端口以测试在一台计算机上(即使在同一应用程序中)的读写,从而更好地控制何时发送哪些数据。

Have you read the documentation for the DataReceived event? 您是否已阅读DataReceived事件的文档?

From MSDN : MSDN

The DataReceived event is not guaranteed to be raised for every byte received . 不保证每个接收到的字节都会引发DataReceived事件 Use the BytesToRead property to determine how much data is left to be read in the buffer. 使用BytesToRead属性确定缓冲区中还有多少数据要读取。

The DataReceived event is raised on a secondary thread when data is received from the SerialPort object. 从SerialPort对象接收数据时,在辅助线程上引发DataReceived事件。 Because this event is raised on a secondary thread, and not the main thread, attempting to modify some elements in the main thread, such as UI elements, could raise a threading exception. 由于此事件是在辅助线程而非主线程上引发的,因此尝试修改主线程中的某些元素(例如UI元素)可能会引发线程异常。 If it is necessary to modify elements in the main Form or Control, post change requests back using Invoke, which will do the work on the proper thread. 如果需要修改主窗体或控件中的元素,请使用Invoke将更改请求回发,这将在适当的线程上完成工作。

The snippet you've posted is quite rough, but I'd set the ReceivedBytesThreshold property to one. 您发布的代码段相当粗糙,但是我将ReceivedBytesThreshold属性设置为1。 This ensures the event firing when at least one byte is present in the incoming buffer. 这样可以确保在传入缓冲区中至少存在一个字节时触发事件。 Cheers 干杯

Use PortMon to capture the working software, and then capture your software; 使用PortMon捕获工作软件,然后捕获软件; then compare the traces. 然后比较痕迹。 Pay particularly close attention to all the configuration parameters, making sure they are the same (as Oliver mentioned). 特别注意所有配置参数,确保它们相同(如Oliver所述)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM