Hi,
I’m completely at a loss here.
We have a device that sends data over an RS485 interface.
When reading this data with any tool not written in .NET, the received data is correct
(Reading using realterm, putty, etc…)
For example we receive:
01 00 56 00 00 01 00 57 57
01 00 56 00 00 01 00 57 57
01 00 56 00 00 01 00 57 57
As soon as we try to read from the same device using a .NET application
we see ghost bytes:
3F 3F 3F 01 00 56 00 00 01 00 57 57
3F 01 00 56 00 00 01 00 57 57
3F 3F 3F 01 00 56 00 00 01 00 57 57
The value is always 3F.
If we monitor the serial interace using a serial port monitor
and read using a .NET app we also see the 3F bytes,
if we monitor and read using putty, realterm, etc…
we do not see the 3F bytes.
If we connect another pc to the other end instead of the device,
and we read data, we do not see the 3F bytes, all data is 100% correct.
It doesn’t matter what PC we run the code on
or what RS485 interface we are using.
We have tried USB-RS485 connectors (multiple from different brands),
USB-RS232 connectors connected to RS232-RS485 converters (multiple from different brands)
The result is always the same.
If we try to read data from the RS485 device using a Fez Panda II with
an RS485 interface, we see the data arrive on the pins using a scope,
we see the data in eltima, but the Fez Panda II never received any data.
If we send data from the computer to that same Panda II the
panda can read the data no problem.
Note that it makes no difference whether we try to read the data
using the OnDataReceived event or by forcing a read ourselves.
My hardware developer is persisting the problem must be in .NET
At this point I don’t know what to believe.
I’ve written lots of apps that deal with serial data in .NET
and there aren’t exactly many variations of how to read serial data in .NET
Below is just one of the variations of the code used to read
void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
var bytesToRead = port.BytesToRead;
var totalRead = 0;
while (bytesToRead > 0)
{
var buffer = new byte[bytesToRead];
var read = port.Read(buffer, 0, buffer.Length);
totalRead += read;
foreach (byte b in buffer)
{
Console.Write("0x{0:X} ", b);
}
bytesToRead = port.BytesToRead;
}
Console.WriteLine(" --- {0} bytes", totalRead);
}
For the Panda the event isn’t even triggered when receiving data from the RS485 device
(it is triggered when receiving data from another computer though)
void prismaPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
var count = prismaPort.BytesToRead;
while (count > 0)
{
byte[] buffer = new byte[count];
var read = prismaPort.Read(buffer, 0, count);
foreach (byte b in buffer)
Debug.Print(b.ToString());
count = prismaPort.BytesToRead;
}
Debug.Print("Finished reading");
}
And yes the event handler is hooked up after the port is open,
and even when forcing a read without event handlers, the read waits forever
when the data comes form the device.
So it seems the .NET code can receive correct data from anything other than the device
It receives 3F bytes when reading from the device if the code runs on a PC
It does not receive any bytes when reading from the device if the code runs on a Panda (we have over a hundred panda’s, so we tried multiple ones to rule out panda malfunctions)
Any other app can receive data from the RS485 device
etc…
Does anybody have any idea?