C
C#4w ago
br4kejet

✅ 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
canton7
canton74w ago
Is there any handshaking? Rts/cts etc?
br4kejet
br4kejet4w ago
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
canton7
canton74w ago
Then it's just a dumb fire-and-forget protocol, so you'll get data as fast as the transmitting side is sending it
br4kejet
br4kejet4w ago
I guess maybe its CPU is trying to manage doing work with sending data then
canton7
canton74w ago
Is it some embedded device?
br4kejet
br4kejet4w ago
Yeah A PLC One of those knockoff ones though
canton7
canton74w ago
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
br4kejet
br4kejet4w ago
I might give that a try I definitely saw gaps though
canton7
canton74w ago
Its clock might be a little off also, if it's just an rc oscillator
br4kejet
br4kejet4w ago
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
canton7
canton74w ago
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
br4kejet
br4kejet4w ago
I guess I should just try and optimise when I read data and maybe even write too
canton7
canton74w ago
(use a Stopwatch for that sort of timing, not DateTime also - much more accurate) Yeah, read into a 1024 byte buffer or so
br4kejet
br4kejet4w ago
DateTime doesn't use the performance counter?
canton7
canton74w ago
I don't think it does, no
Want results from more Discord servers?
Add your server