Click here to Skip to main content
15,889,281 members
Home / Discussions / C#
   

C#

 
GeneralRe: reading from a dynamically created textarea Pin
Hbr_Tha_real28-Aug-13 1:53
Hbr_Tha_real28-Aug-13 1:53 
GeneralRe: reading from a dynamically created textarea Pin
Jason Gleim28-Aug-13 7:24
professionalJason Gleim28-Aug-13 7:24 
GeneralRe: reading from a dynamically created textarea Pin
Hbr_Tha_real28-Aug-13 11:19
Hbr_Tha_real28-Aug-13 11:19 
GeneralRe: reading from a dynamically created textarea Pin
Jason Gleim29-Aug-13 4:14
professionalJason Gleim29-Aug-13 4:14 
GeneralRe: reading from a dynamically created textarea Pin
Hbr_Tha_real29-Aug-13 12:16
Hbr_Tha_real29-Aug-13 12:16 
QuestionSerial Port reading char string Pin
Blubbo27-Aug-13 3:02
Blubbo27-Aug-13 3:02 
AnswerRe: Serial Port reading char string Pin
OriginalGriff27-Aug-13 3:41
mveOriginalGriff27-Aug-13 3:41 
AnswerRe: Serial Port reading char string Pin
Jason Gleim27-Aug-13 5:24
professionalJason Gleim27-Aug-13 5:24 
You don't need a background worker and the baud rate has nothing to do with your ability or inability to receive a message.

The SerialPort class already operates on its own thread. The truth is the physical serial port on your PC is implemented with a USART and has no buffer of its own. It can hold a single character. When it receives data, it fires an interrupt. The interrupt handler adds the character to a FIFO buffer and notifies the serial port class attached to that port that a character has been received. That message goes into the message queue and your application will receive it via the DataReceived event when the message pump gets around to sending it to your app.

When you read the data from the serial port class, you empty the FIFO buffer either in one swoop or a character at a time. Interlocks are already built into the serial port class so you don't contend with any characters that may be received at the port while the data is being read by the application. For all intents and purposes, you read a string of characters as they have been received up to that point in time.

What is NOT present is any kind of message framing. And that is where you need to do something. The fact is the DataReceived event could be fired for every character that comes in the serial port. You have to grab those characters, append them to a message string, then determine when the message is complete. It is the same as information coming from the keyboard... the user types a single character at a time. If you are looking for a word, you buffer those characters onto a string until you see a space or a carriage return. Same this with the serial port... just think of it as another keyboard where characters arrive (not full strings!).

So you need to decide what your message delimiter is. Normally, for serial communications, the message is ended with a 13... a carriage return. Since it looks like you might be talking to a PLC for data collection, it may also be a fixed-width situation. In that case you need to test the length of the accumulated string each time DataReceived is called. Either way, you need to look for the delimiter or the length in DataRecevied and THEN process your results once the whole string has been received. Make sense?

Secondly, you are casting the serial port from the event arguments. You shouldn't do that. You already have a reference to the serial port in your class-scoped serialPort object. Use it inside of the event handler.

Usually, inside of this event, you setup a While loop that looks at the character count on the serial port. If it is greater than 0, you pull a single character with ReadChar(), check to see if it is your message delimiter, then tack it onto strMessage if it isn't. If it is your delimiter, you pass the message to the processing function and set strMessage to string.empty when you are done.

Remember that DataRecieved is called on a different thread because it is raised by the serial port class. This means your message processing will take place on the worker thread too. If you want to interact with the UI, you will have to marshal the call over to the UI thread.

HTH!
QuestionHow to paste outside of my application Pin
Mahmoud EL-Shazly26-Aug-13 13:32
Mahmoud EL-Shazly26-Aug-13 13:32 
AnswerRe: How to paste outside of my application Pin
BillWoodruff26-Aug-13 13:46
professionalBillWoodruff26-Aug-13 13:46 
GeneralRe: How to paste outside of my application Pin
Mahmoud EL-Shazly27-Aug-13 11:51
Mahmoud EL-Shazly27-Aug-13 11:51 
GeneralRe: How to paste outside of my application Pin
Bernhard Hiller27-Aug-13 20:38
Bernhard Hiller27-Aug-13 20:38 
GeneralRe: How to paste outside of my application Pin
Mahmoud EL-Shazly28-Aug-13 0:42
Mahmoud EL-Shazly28-Aug-13 0:42 
GeneralRe: How to paste outside of my application Pin
DaveyM6928-Aug-13 1:35
professionalDaveyM6928-Aug-13 1:35 
GeneralRe: How to paste outside of my application Pin
Mahmoud EL-Shazly1-Oct-13 20:01
Mahmoud EL-Shazly1-Oct-13 20:01 
AnswerRe: How to paste outside of my application Pin
Bernhard Hiller26-Aug-13 20:52
Bernhard Hiller26-Aug-13 20:52 
AnswerRe: How to paste outside of my application Pin
OriginalGriff27-Aug-13 3:42
mveOriginalGriff27-Aug-13 3:42 
GeneralRe: How to paste outside of my application Pin
Mahmoud EL-Shazly27-Aug-13 14:17
Mahmoud EL-Shazly27-Aug-13 14:17 
QuestionStrongname vs Obfuscation - resistant to tampering? Pin
devvvy26-Aug-13 13:01
devvvy26-Aug-13 13:01 
AnswerRe: Strongname vs Obfuscation - resistant to tampering? Pin
Abhinav S26-Aug-13 19:09
Abhinav S26-Aug-13 19:09 
GeneralRe: Strongname vs Obfuscation - resistant to tampering? Pin
devvvy26-Aug-13 20:02
devvvy26-Aug-13 20:02 
GeneralRe: Strongname vs Obfuscation - resistant to tampering? Pin
Eddy Vluggen26-Aug-13 23:15
professionalEddy Vluggen26-Aug-13 23:15 
GeneralRe: Strongname vs Obfuscation - resistant to tampering? Pin
Dave Kreskowiak27-Aug-13 2:12
mveDave Kreskowiak27-Aug-13 2:12 
GeneralRe: Strongname vs Obfuscation - resistant to tampering? Pin
devvvy27-Aug-13 2:19
devvvy27-Aug-13 2:19 
GeneralRe: Strongname vs Obfuscation - resistant to tampering? Pin
Dave Kreskowiak27-Aug-13 2:42
mveDave Kreskowiak27-Aug-13 2:42 

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.