How to read device data from serial port

I have one device that is sending data to COM port to COM13. Now I want to read this data and display it in a RichTextBox or any text control.

I wrote an application using IO and IO.Ports, but the comport.DataRecived event doesn't fire even though the device is sending data to that port.

I have a software on which I figure out the port number and it displays data successfully, which is insured against the data coming into the port, but I cannot get it.

Is there a way to read the 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;
}

      

+2


a source to share


4 answers


This is usually due to a misconfiguration of the serial port. It is not enough to simply open a serial port and wait for some data to appear. You should also set all SerialPort.Properties to the correct value for your desired connection.

Some of the usual ones are BaudRate

, DataBits

or Parity

, but in order to be sure you have to install them all. Even things like RtsEnable

or ReadTimeout

.

You have to set everything up so the configuration state is saved from the port itself. Therefore, if one application opens such a port, makes some configuration changes and closes it, the next application that opens the port starts with that configuration until it changes it.



Update

I can't seem to see the problem .; -))

The only advice I can give you is to use Monitor to get a better understanding of what your other application is really doing and what happens next on the wire. In addition, you can configure two virtual COM ports to test read and write on the same machine (even within the same application) to have better control of when data is sent.

+1


a source


Have you read the documentation for the DataReceived event?

From MSDN :



The DataReceived event is not guaranteed for every byte received . Use the BytesToRead property to determine how much data is left to read in the buffer.

The DataReceived event is raised on the secondary thread when data is received from the SerialPort object. Because this event occurs on the secondary thread and not on the main thread, trying to change some elements of the main thread, such as UI elements, might throw a streaming exception. If you need to change items in the main form or control, submit change requests after using Invoke that will do the work on the appropriate flow.

0


a source


The scanned snippet is pretty crude, but I would set the ReceivedBytesThreshold property to one. This ensures that the event is triggered when at least one byte is present in the incoming buffer. Greetings

0


a source


Use PortMon to capture a working software and then capture your software; then compare the tracks. Pay special attention to all configuration options, making sure they are the same (as Oliver mentioned).

0


a source







All Articles