Click here to Skip to main content
15,887,322 members
Home / Discussions / C#
   

C#

 
GeneralRe: SerialPort DataReceived Event Read Byte ? Pin
Richard MacCutchan28-Mar-18 5:28
mveRichard MacCutchan28-Mar-18 5:28 
GeneralRe: SerialPort DataReceived Event Read Byte ? Pin
ibrahimayhans28-Mar-18 5:37
ibrahimayhans28-Mar-18 5:37 
GeneralRe: SerialPort DataReceived Event Read Byte ? Pin
Richard MacCutchan28-Mar-18 6:03
mveRichard MacCutchan28-Mar-18 6:03 
AnswerRe: SerialPort DataReceived Event Read Byte ? Pin
Gerry Schmitz28-Mar-18 4:12
mveGerry Schmitz28-Mar-18 4:12 
AnswerRe: SerialPort DataReceived Event Read Byte ? Pin
Eddy Vluggen28-Mar-18 5:13
professionalEddy Vluggen28-Mar-18 5:13 
GeneralRe: SerialPort DataReceived Event Read Byte ? Pin
ibrahimayhans28-Mar-18 5:21
ibrahimayhans28-Mar-18 5:21 
GeneralRe: SerialPort DataReceived Event Read Byte ? Pin
OriginalGriff28-Mar-18 5:56
mveOriginalGriff28-Mar-18 5:56 
AnswerRe: SerialPort DataReceived Event Read Byte ? Pin
Luc Pattyn28-Mar-18 8:23
sitebuilderLuc Pattyn28-Mar-18 8:23 
Hi,

serial communication can be very hard, and it can be quite easy; it all depends on circumstances: how high the transmission rate is, how big the gap is in between messages, etc.


Here are three pointers, assuming the peripheral uses printable text:


1. Always use a terminal emulator to get acquainted with your peripheral's behavior. It will help you in checking the hardware aspects and overall settings of your port, and it will help you in understanding the message flow.


2. the DataReceived event fires whenever it wants, which could be after receiving 1 byte, or 39 bytes, or whatever. It does not care about what you expect as a message, it only knows about individual bytes and several levels of buffering inside Windows. As a result it isn't very useful except in simple cases:

If your messages are far apart, which gives you some time to waste, the easiest approach is to get started by the DataReceived event, then wait (with Thread.Sleep) sufficiently long so the entire message should be received by now, and then use ReadExisting. The required delay equals maximum string length * character time, which obviously depends on the baud rate used (say 1 msec at 9600 baud). Warning: Windows timing isn't always very accurate, so add a spare 20 msec or so.


3. The DataReceived event gets handled on an arbitrary thread (actually one from the ThreadPool) but absolutely NOT on the GUI thread (read more: Asynchronous operations run on ThreadPool threads[^]); therefore, you are not allowed to touch any of your GUI Controls inside the DataReceived handler.

The proper solution is by using Control.InvokeRequired and Control.Invoke (read more: Invalid cross-thread operations[^]). NEVER use Control.CheckForIllegalCrossThreadCalls


In more complex situations I tend to avoid the DataReceived event, instead I'd use an explicit Thread and perform blocking reads and a buffering scheme. Sometimes a BackgroundWokrer can do the job.

Hope this helps.

Smile | :)


PS: Don't trust the default settings of SerialPort, always explicitly set properties such as Encoding, NewLine, etc.
Luc Pattyn [My Articles] Nil Volentibus Arduum

QuestionHow to strip special characters from a field and replace with blanks Pin
Member 1375136828-Mar-18 3:17
Member 1375136828-Mar-18 3:17 
AnswerRe: How to strip special characters from a field and replace with blanks Pin
Gerry Schmitz28-Mar-18 5:50
mveGerry Schmitz28-Mar-18 5:50 
AnswerRe: How to strip special characters from a field and replace with blanks Pin
#realJSOP28-Mar-18 8:08
mve#realJSOP28-Mar-18 8:08 
QuestionForm1: Add combobox item from another form2: textbox Pin
Member 1374647827-Mar-18 10:20
Member 1374647827-Mar-18 10:20 
AnswerRe: Form1: Add combobox item from another form2: textbox Pin
OriginalGriff27-Mar-18 19:59
mveOriginalGriff27-Mar-18 19:59 
QuestionConvert IntPtr to Class Pin
Member 1263260127-Mar-18 0:47
Member 1263260127-Mar-18 0:47 
QuestionWeakEventHandler Sample Pin
Kevin Marois26-Mar-18 10:06
professionalKevin Marois26-Mar-18 10:06 
AnswerRe: WeakEventHandler Sample Pin
Dave Kreskowiak27-Mar-18 4:32
mveDave Kreskowiak27-Mar-18 4:32 
GeneralRe: WeakEventHandler Sample Pin
Kevin Marois27-Mar-18 5:18
professionalKevin Marois27-Mar-18 5:18 
GeneralRe: WeakEventHandler Sample Pin
Dave Kreskowiak27-Mar-18 9:34
mveDave Kreskowiak27-Mar-18 9:34 
GeneralRe: WeakEventHandler Sample Pin
Kevin Marois27-Mar-18 9:37
professionalKevin Marois27-Mar-18 9:37 
GeneralRe: WeakEventHandler Sample Pin
Dave Kreskowiak27-Mar-18 10:16
mveDave Kreskowiak27-Mar-18 10:16 
GeneralRe: WeakEventHandler Sample Pin
Kevin Marois27-Mar-18 10:17
professionalKevin Marois27-Mar-18 10:17 
QuestionC#, SQL Time period between two columns Pin
Member 1374655226-Mar-18 6:47
Member 1374655226-Mar-18 6:47 
AnswerRe: C#, SQL Time period between two columns Pin
OriginalGriff26-Mar-18 8:20
mveOriginalGriff26-Mar-18 8:20 
AnswerRe: C#, SQL Time period between two columns Pin
MadMyche27-Mar-18 6:05
professionalMadMyche27-Mar-18 6:05 
QuestionOrdering names into list box Pin
Member 1374647825-Mar-18 21:15
Member 1374647825-Mar-18 21:15 

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.