简体   繁体   中英

How to Read Device Data From serial port

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.

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.

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.

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 .

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.

Have you read the documentation for the DataReceived event?

From 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.

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.

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

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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