Skip to main content
added 7 characters in body; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Better Approach to Using the C# SerialPort Classclass for a library

A few notes: 

First off, I understand that Thread.Sleep() is much maligned, but I don't have many other choices. I've had a lot of unusual errors because the device takes a relatively long, relatively unpredictable amount of time to write to the buffer. Currently I try to sleep and discard the buffer whenever possible. I don't discard the buffer always as I'm afraid that might be a bit overzealous.

I'm worried as I've never used the SerialPortSerialPort class before. I don't know if there's anything I'm missing here. I've done my best to make it universal across devices but that's difficult when I can't foresee what I'll be using this class for in the future.

ReadLineReadLine does not wipe the buffer. I've seen this firsthand when Rts and Dtr aren't enabled. Say I issue the command: *IDN? normally this returns: FLUKE,5500A,8030005,2.61+1.3+2.0+* 66> but sometimes issuing it and then calling ReadLine() doesn't give the machine a chance to completely write to the buffer. Due to this, stale data will be left in the buffer. Say I then issue another command, OUT 30V, 60KHZ and call ReadLine() again, if there's still stale data, ReadLine() will receive FLUKE,5500A,8030005,2.61+1.3+2.0+* 66> from the last command that was issued instead of the expected output.

Better Approach to Using the C# SerialPort Class

A few notes: First off, I understand that Thread.Sleep() is much maligned, but I don't have many other choices. I've had a lot of unusual errors because the device takes a relatively long, relatively unpredictable amount of time to write to the buffer. Currently I try to sleep and discard the buffer whenever possible. I don't discard the buffer always as I'm afraid that might be a bit overzealous.

I'm worried as I've never used the SerialPort class before. I don't know if there's anything I'm missing here. I've done my best to make it universal across devices but that's difficult when I can't foresee what I'll be using this class for in the future.

ReadLine does not wipe the buffer. I've seen this firsthand when Rts and Dtr aren't enabled. Say I issue the command: *IDN? normally this returns: FLUKE,5500A,8030005,2.61+1.3+2.0+* 66> but sometimes issuing it and then calling ReadLine() doesn't give the machine a chance to completely write to the buffer. Due to this, stale data will be left in the buffer. Say I then issue another command, OUT 30V, 60KHZ and call ReadLine() again, if there's still stale data, ReadLine() will receive FLUKE,5500A,8030005,2.61+1.3+2.0+* 66> from the last command that was issued instead of the expected output.

SerialPort class for a library

A few notes: 

First off, I understand that Thread.Sleep() is much maligned, but I don't have many other choices. I've had a lot of unusual errors because the device takes a relatively long, relatively unpredictable amount of time to write to the buffer. Currently I try to sleep and discard the buffer whenever possible. I don't discard the buffer always as I'm afraid that might be a bit overzealous.

I'm worried as I've never used the SerialPort class before. I don't know if there's anything I'm missing here. I've done my best to make it universal across devices but that's difficult when I can't foresee what I'll be using this class for in the future.

ReadLine does not wipe the buffer. I've seen this firsthand when Rts and Dtr aren't enabled. Say I issue the command: *IDN? normally this returns: FLUKE,5500A,8030005,2.61+1.3+2.0+* 66> but sometimes issuing it and then calling ReadLine() doesn't give the machine a chance to completely write to the buffer. Due to this, stale data will be left in the buffer. Say I then issue another command, OUT 30V, 60KHZ and call ReadLine() again, if there's still stale data, ReadLine() will receive FLUKE,5500A,8030005,2.61+1.3+2.0+* 66> from the last command that was issued instead of the expected output.

edited tags
Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237
added 615 characters in body
Source Link

ReadLine does not wipe the buffer. I've seen this firsthand when Rts and Dtr aren't enabled. Say I issue the command: *IDN? normally this returns: FLUKE,5500A,8030005,2.61+1.3+2.0+* 66> but sometimes issuing it and then calling ReadLine() doesn't give the machine a chance to completely write to the buffer. Due to this, stale data will be left in the buffer. Say I then issue another command, OUT 30V, 60KHZ and call ReadLine() again, if there's still stale data, ReadLine() will receive FLUKE,5500A,8030005,2.61+1.3+2.0+* 66> from the last command that was issued instead of the expected output.

ReadLine does not wipe the buffer. I've seen this firsthand when Rts and Dtr aren't enabled. Say I issue the command: *IDN? normally this returns: FLUKE,5500A,8030005,2.61+1.3+2.0+* 66> but sometimes issuing it and then calling ReadLine() doesn't give the machine a chance to completely write to the buffer. Due to this, stale data will be left in the buffer. Say I then issue another command, OUT 30V, 60KHZ and call ReadLine() again, if there's still stale data, ReadLine() will receive FLUKE,5500A,8030005,2.61+1.3+2.0+* 66> from the last command that was issued instead of the expected output.

Source Link
Loading