0

I am trying to read data from a device connected via USB. For creating the trigger the code looks like:

private SerialPort realTimePort;
realTimePort = new SerialPort();
realTimePort.BaudRate = 9600;
realTimePort.PortName =  "COM4";
realTimePort.ReadTimeout = 5000;
realTimePort.ReadBufferSize = 32768;
realTimePort.ReceivedBytesThreshold = 1;
realTimePort.BaudRate = 9600;
realTimePort.ReadBufferSize = 4096;
realTimePort.ParityReplace = 63;
realTimePort.Parity = Parity.None;  
realTimePort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(realTimePort_DataReceived);
realTimePort.Open();

To read the data, which was sent, the code looks like:

public void realTimePort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    // Do something with the data
}

With one version of the device everything works fine and the trigger starts, when data was sent, but with a newer software-version of the device realTimePort_DataReceived is never triggered. At first i thought, that the problem might be, that the device never sends data, but then i tried to read the data with "Tera Term" and there i can see exactly, what i am expecting. I also compared the data with "Tera Term", which was sent of the devices with the different software-versions, but it is exactly the same string. Do you have any ideas, why the event is triggered with the older software-version and not with the newer one? An employee of the manufacturer of the device already gave me the specification of the SerialPort, because i had this problem, but it didn't help me.

1 Answers1

1

It is hard to reproduce the issue as i am not aware what type of device you are using and what type of data is sends here are some tips you can evaluate a quick check list to ensure the correct data receiving.

1. Play with RTS or DTR port flags for new version device

Basically some new versions of hardware uses flags of SerialPort e.g. DTR (Data Terminal Ready) indicates that your code is ready to receive, and RTS (Request to Send) a request to device to actually send data. for older hardware types it was not mandatory to use these flags although in modern devices its still not but just a standard practice so you should experiment & try enabling these by your code e.g.

realTimePort.RtsEnable =  true; //enable this mode
realTimePort.DtrEnable = true; //and also this one

2. Try to read device error stream

It is possible that your new version hardware is sendind data over error stream, the tool you was using utilizes both streams for data read so you can subscrive to error event like.

realTimePort.ErrorReceived += new SerialErrorReceivedEventHandler(sPort_ErrorReceived);

private static void sPort_ErrorReceived(object sender, SerialErrorReceivedEventArgs e)
{
    //process your error, it will give you further hint about why part of your question.
}
Zain Ul Abidin
  • 2,467
  • 1
  • 17
  • 29