user2527567
user2527567

Reputation: 21

Does the .NET SerialPort class (C#) behave differently on older systems?

I have a relatively short C# program that feeds data over an RS232 port using the following format:

'STX' Header 'Checksum' 'ETX'

'STX' data1 'Checksum' 'ETX'

'STX' ... 'Checksum' 'ETX'

'STX' dataN 'Checksum' 'ETB'

The receiving end is supposed to reply with either an ACK or NAK byte upon receiving the ETX/ETB.

This works fine running on computers with Windows 7 64 bit.

However there was a problem getting the program to start on a Win XP 32 machine, because I had built the program to target .NET version 4.5 (which apparently isn't supported by that version of Win XP).

I changed it to target .NET version 4 instead and now the program starts just fine. But the receiving end doesn't respond with ACK or NAKs anymore, only the first time (upon sending the datafile header). I printed out the byte buffer on the console and at the place where the ETX byte is supposed to be there is an UP DOWN ARROW WITH BASE symbol (↨) which tells me it somehow didn't send an ETX at all.

What's weird about this is that it works fine on windows 7 machines, and the first transmission (header) returns an ACK just like it's supposed to, but when the first data packet is sent no more replies... no ACK or NAK.

Upvotes: 0

Views: 464

Answers (1)

user2527567
user2527567

Reputation: 21

In case anyone run into similar problems; my problem was that I used a USB to serial converter. It caused the timings to become all messed up. What I did to solve it was to subscribe to the SerialPort's DataReceived Event, and then grab the data inside the event handler with serialport.ReadExisting() (which also clears the receive buffer). So the thread would roughly do:

Send data -> wait for DataReceive to fire -> handle data -> repeat.

Just be sure to put a timeout in the waiting loop.

Upvotes: 1

Related Questions