✅ SerialPort performance
I'm doing some tests on the performance of serial communication and I'm geting a fair chunk of perforance loss. At a baud rate of 38400, with 7 data bits and 1 stop bit, then I can expect to get a maximum of 26kbps, but I received 120 bytes in just under 50 milliseconds which seems to be about 20 kbps. Anyone know what would be causing this?
43 Replies
Is there any handshaking? Rts/cts etc?
Not that I know of
I'm not specifying any handshaking
Just a baud rate of 38400, 7 data bits, one stop bits and even parity
Then it's just a dumb fire-and-forget protocol, so you'll get data as fast as the transmitting side is sending it
I guess maybe its CPU is trying to manage doing work with sending data then
Is it some embedded device?
Yeah
A PLC
One of those knockoff ones though
It's possible that it has all of the available to go at the start of the 50ms, and is DMAing it out, but it's not unusual to have little gaps as it does work, then transmits, etc
Get a logic analyser / scope on that line
I might give that a try
I definitely saw gaps though
Its clock might be a little off also, if it's just an rc oscillator
I did some rough timing (in debug mode though) using DateTime.Now, and found that I was basically receiving data 1 byte at a time which I guess is expected since my read function only used Thread.Yield(), sometimes the time between receiving bytes was less than a millisecond, sometimes it was maybe 2 milliseconds
The thread scheduling is normally only accurate to 3-5ms or something
So if you're reading one byte then sleeping the thread, it's normally at least 3ms until it'll be woken again
I guess I should just try and optimise when I read data and maybe even write too
(use a Stopwatch for that sort of timing, not DateTime also - much more accurate)
Yeah, read into a 1024 byte buffer or so
DateTime doesn't use the performance counter?
I don't think it does, no