如何从串口读取设备数据?

时间:2023-02-10 15:09:57

I have one device which sends data on COM port say on COM13. Now i want to read that data and display it in the RichTextBox or in any text control.

我有一个设备可以在COM13上传送数据。现在我想读取数据并将其显示在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的帮助下完成了申请。港口,但相称。即使设备正在该端口上发送数据,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;
}

4 个解决方案

#1


1  

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。属性设置为所需连接的正确值。

Some of the common ones are BaudRate, DataBits or Parity, but to be really sure you have to set all of them. Even such things as RtsEnable or ReadTimeout.

一些常见的是BaudRate、DataBits或奇偶校验,但是要确保您必须设置所有这些。甚至像RtsEnable或ReadTimeout这样的东西。

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. 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端口来测试一台机器上的读写(甚至在同一个应用程序中),以便更好地控制何时发送哪些数据。

#2


0  

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. Use the BytesToRead property to determine how much data is left to be read in the buffer.

对于接收到的每个字节,不保证会引发DataReceived事件。使用BytesToRead属性来确定还剩下多少数据可以在缓冲区中读取。

The DataReceived event is raised on a secondary thread when data is received from the SerialPort object. 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. 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.

当从SerialPort对象接收数据时,DataReceived事件在辅助线程上引发。因为这个事件是在次要线程上引发的,而不是主线程,试图修改主线程中的某些元素,比如UI元素,可能会引发线程异常。如果需要修改主窗体或控件中的元素,请使用Invoke将更改请求发送回来,这将在适当的线程上完成工作。

#3


0  

The snippet you've posted is quite rough, but I'd set the ReceivedBytesThreshold property to one. This ensures the event firing when at least one byte is present in the incoming buffer. Cheers

您发布的代码片段相当粗糙,但是我将接收到的属性设置为一个。这将确保在传入缓冲区中出现至少一个字节时触发事件。干杯

#4


0  

Use PortMon to capture the working software, and then capture your software; then compare the traces. Pay particularly close attention to all the configuration parameters, making sure they are the same (as Oliver mentioned).

使用PortMon捕获工作软件,然后捕获您的软件;然后比较痕迹。特别注意所有的配置参数,确保它们是相同的(正如Oliver提到的)。

#1


1  

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。属性设置为所需连接的正确值。

Some of the common ones are BaudRate, DataBits or Parity, but to be really sure you have to set all of them. Even such things as RtsEnable or ReadTimeout.

一些常见的是BaudRate、DataBits或奇偶校验,但是要确保您必须设置所有这些。甚至像RtsEnable或ReadTimeout这样的东西。

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. 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端口来测试一台机器上的读写(甚至在同一个应用程序中),以便更好地控制何时发送哪些数据。

#2


0  

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. Use the BytesToRead property to determine how much data is left to be read in the buffer.

对于接收到的每个字节,不保证会引发DataReceived事件。使用BytesToRead属性来确定还剩下多少数据可以在缓冲区中读取。

The DataReceived event is raised on a secondary thread when data is received from the SerialPort object. 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. 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.

当从SerialPort对象接收数据时,DataReceived事件在辅助线程上引发。因为这个事件是在次要线程上引发的,而不是主线程,试图修改主线程中的某些元素,比如UI元素,可能会引发线程异常。如果需要修改主窗体或控件中的元素,请使用Invoke将更改请求发送回来,这将在适当的线程上完成工作。

#3


0  

The snippet you've posted is quite rough, but I'd set the ReceivedBytesThreshold property to one. This ensures the event firing when at least one byte is present in the incoming buffer. Cheers

您发布的代码片段相当粗糙,但是我将接收到的属性设置为一个。这将确保在传入缓冲区中出现至少一个字节时触发事件。干杯

#4


0  

Use PortMon to capture the working software, and then capture your software; then compare the traces. Pay particularly close attention to all the configuration parameters, making sure they are the same (as Oliver mentioned).

使用PortMon捕获工作软件,然后捕获您的软件;然后比较痕迹。特别注意所有的配置参数,确保它们是相同的(正如Oliver提到的)。