Click here to Skip to main content
14,737,485 members
Home » Discussions » C#
   

C#

 
AnswerRe: how to insert unknown amount of data into a database Pin
Luc Pattyn11-Oct-20 9:32
sitebuilderLuc Pattyn11-Oct-20 9:32 
AnswerRe: how to insert unknown amount of data into a database Pin
OriginalGriff11-Oct-20 9:33
mveOriginalGriff11-Oct-20 9:33 
AnswerRe: how to insert unknown amount of data into a database Pin
jsc4213-Oct-20 4:50
professionaljsc4213-Oct-20 4:50 
QuestionWhy am I not allowed to do this with events Pin
Keith Barrow9-Oct-20 2:20
professionalKeith Barrow9-Oct-20 2:20 
AnswerRe: Why am I not allowed to do this with events Pin
Richard Deeming9-Oct-20 3:12
mveRichard Deeming9-Oct-20 3:12 
GeneralRe: Why am I not allowed to do this with events Pin
Keith Barrow9-Oct-20 4:19
professionalKeith Barrow9-Oct-20 4:19 
QuestionHow to read from serial port until timeout of no more data received? Pin
td17768-Oct-20 14:27
Membertd17768-Oct-20 14:27 
AnswerRe: How to read from serial port until timeout of no more data received? Pin
Luc Pattyn8-Oct-20 15:27
sitebuilderLuc Pattyn8-Oct-20 15:27 
Hi,

serial ports are tricky, and the best approach depends very much on what exactly it is you need.

A few facts that may help you find the best way for your situation:
1. the DataReceived event isn't very reliable, what I mean is for N bytes coming in on the serial port it may fire an arbitrary number of times, anywhere from 1 to N.
2. Asynchronous event handlers get executed on threads from the ThreadPool, not on the main (or "UI") thread, so you cannot directly manipulate your Form's Controls.
3. We all hope only one DatReceived event will be active at any one time, although I have never seen that being promised...
4. incoming data is buffered several times before it reaches your C# code (hardware in the serial port, buffers in the driver, etc).
5. when asking for a delay (e.g. by using Thread.Sleep) what you get will not exactly be what you asked for, the system clock may have limited resolution (e.g. 15 msec) and the system may not immediately put your thread back in charge.


Here is one concept that has worked for me on several occasions:

if you are sure all data will arrive in a period of M milliseconds once the first byte is sent, then have a very simple DataReceived handler that starts by sleeping slightly more than M milliseconds (so all the message data is guaranteed to have gotten time to be received), and then perform a ReadExisting or a Read(chararray) when working with text, or a Read(bytearray) when not working with text.

The one advantage of it is simplicity. However there are a few drawbacks to this approach:
1. there is artificial latency, i.e. nothing happens while the handler is sleeping, e.g. you don't see the data slowly coming in.
2. you are not immediately ready to get the next burst of data, as your sleep period must be slightly larger than your worst-case time required to get one message, and then you still need to process the data.
3. Your sleep period depends on your port settings (baud rate, number of stop bits, etc).


If the above isn't suitable, I tend to go to the other extreme:

1. consider it binary data (even when it is text) so you don't get confused by newline handling, multibyte Unicode characters and the like.
2. set up a mechanism that reads into a byte array; you can do this in the DataReceived handler; I don't, I prefer to do it periodically (e.g. in a BackGroundWorker that uses a loop
with a Thread.Sleep in there).
3. You can set a ReadTimeout to the serialport to keep the loop spinning, and to make an abort or application exit slightly easier.
4. Make sure to use the return value of the Read(bytearray) method, indicating the number of bytes actually read. Append the newly received bytes to a large buffer holding all bytes not yet processed.
5. Do whatever it takes to detect message boundaries in that larger buffer, then process and remove the messages when appropriate.

Good luck,

Smile | :)
Luc Pattyn [My Articles]
If you can't find it on YouTube try TikTok...

GeneralRe: How to read from serial port until timeout of no more data received? Pin
td17768-Oct-20 16:36
Membertd17768-Oct-20 16:36 
GeneralRe: How to read from serial port until timeout of no more data received? Pin
Luc Pattyn9-Oct-20 2:25
sitebuilderLuc Pattyn9-Oct-20 2:25 
AnswerRe: How to read from serial port until timeout of no more data received? Pin
Gerry Schmitz8-Oct-20 19:27
mveGerry Schmitz8-Oct-20 19:27 
GeneralRe: How to read from serial port until timeout of no more data received? Pin
td17769-Oct-20 12:16
Membertd17769-Oct-20 12:16 
GeneralRe: How to read from serial port until timeout of no more data received? Pin
Gerry Schmitz9-Oct-20 15:10
mveGerry Schmitz9-Oct-20 15:10 
AnswerRe: How to read from serial port until timeout of no more data received? Pin
Richard MacCutchan8-Oct-20 23:55
mveRichard MacCutchan8-Oct-20 23:55 
AnswerMy (temporary?) solution Pin
td17769-Oct-20 12:25
Membertd17769-Oct-20 12:25 
QuestionCOM exception loss Pin
Member 126326017-Oct-20 21:57
MemberMember 126326017-Oct-20 21:57 
AnswerRe: COM exception loss Pin
Richard MacCutchan7-Oct-20 22:35
mveRichard MacCutchan7-Oct-20 22:35 
GeneralRe: COM exception loss Pin
Member 126326017-Oct-20 23:03
MemberMember 126326017-Oct-20 23:03 
GeneralRe: COM exception loss Pin
Richard MacCutchan7-Oct-20 23:25
mveRichard MacCutchan7-Oct-20 23:25 
GeneralRe: COM exception loss Pin
Member 126326017-Oct-20 23:58
MemberMember 126326017-Oct-20 23:58 
GeneralRe: COM exception loss Pin
Richard MacCutchan8-Oct-20 0:24
mveRichard MacCutchan8-Oct-20 0:24 
AnswerRe: COM exception loss Pin
OriginalGriff8-Oct-20 5:10
mveOriginalGriff8-Oct-20 5:10 
QuestionScanning for servers Pin
pkfox4-Oct-20 22:52
professionalpkfox4-Oct-20 22:52 
AnswerRe: Scanning for servers Pin
Richard Deeming5-Oct-20 3:44
mveRichard Deeming5-Oct-20 3:44 
GeneralRe: Scanning for servers Pin
pkfox5-Oct-20 4:44
professionalpkfox5-Oct-20 4:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.