Click here to Skip to main content
15,888,351 members
Home / Discussions / C#
   

C#

 
GeneralRe: Read information from serial port in Mathlab format Pin
Richard MacCutchan5-Apr-24 22:44
mveRichard MacCutchan5-Apr-24 22:44 
GeneralRe: Read information from serial port in Mathlab format Pin
Victor Gonzalez 20245-Apr-24 23:22
Victor Gonzalez 20245-Apr-24 23:22 
GeneralRe: Read information from serial port in Mathlab format Pin
Richard MacCutchan5-Apr-24 23:41
mveRichard MacCutchan5-Apr-24 23:41 
GeneralRe: Read information from serial port in Mathlab format Pin
Luc Pattyn6-Apr-24 8:16
sitebuilderLuc Pattyn6-Apr-24 8:16 
GeneralRe: Read information from serial port in Mathlab format Pin
Victor Gonzalez 20247-Apr-24 1:12
Victor Gonzalez 20247-Apr-24 1:12 
GeneralRe: Read information from serial port in Mathlab format Pin
Luc Pattyn7-Apr-24 1:52
sitebuilderLuc Pattyn7-Apr-24 1:52 
GeneralRe: Read information from serial port in Mathlab format Pin
Victor Gonzalez 20247-Apr-24 6:04
Victor Gonzalez 20247-Apr-24 6:04 
GeneralRe: Read information from serial port in Mathlab format Pin
Luc Pattyn7-Apr-24 8:13
sitebuilderLuc Pattyn7-Apr-24 8:13 
Your problem description is way too vague to get useful suggestions.

A couple of points:

1. why are you calling sp.DiscardInBuffer() ?
all the data that you get by executing Read operations is removed from the serial stream automatically, however if any data comes in while your handler is running will be thrown away, more specifically anything that comes in after sp.BytesToRead is not read and discarded.

2. you are ignoring the return value of sp.Read().
this method will not always return the number of bytes requested: when fewer are available, fewer will be returned!

3. you should not switch on the number of bytes read, as sp.BytesToRead may return just any value: it could be as low as 3, or as high as 300, all depending on what your peripheral is sending and how fast Windows is dealing with it.

4. you need to see the incoming data as chunks of one big stream, and provide code to detect the messages in that stream, then act on them. A serial port does not care about messages, it just sends bytes; so a DataReceived handler might get only half a message, or several messages, or ...

5. serial ports (and all interfaces to external devices) may temporarily fail, e.g. loose a few bytes; to get a reliable program your code must be capable of surviving such mishaps.

6. as I mentioned before, I would avoid DataReceived; collecting the incoming data, discovering the messages in the stream, and acting on them is way easier in a dedicated thread.


If you want someone to help, you need to provide lots of very specific information, such as:

1. description of the application. What is it about, is this a commercial peripheral (make & type), is the peripheral's software fixed?

2. is there a document that describes the protocol your peripheral is using on its serial port?

3. how often will a message be sent?

4. is there a master-slave relationship? i.e. send one command to get one response? that would be easy.
or is the peripheral free to send whatever and whenever it likes to do so?

etc.

All this info is needed to come up with a reliable approach.

Smile | :)
Luc Pattyn [My Articles]
The Windows 11 taskbar is a disgrace; a third-party add-on is needed to reverse the deterioration. I decline such a downgrade.

GeneralRe: Read information from serial port in Mathlab format Pin
Victor Gonzalez 20247-Apr-24 9:53
Victor Gonzalez 20247-Apr-24 9:53 
GeneralRe: Read information from serial port in Mathlab format Pin
Luc Pattyn7-Apr-24 11:04
sitebuilderLuc Pattyn7-Apr-24 11:04 
GeneralRe: Read information from serial port in Mathlab format Pin
Victor Gonzalez 20247-Apr-24 22:42
Victor Gonzalez 20247-Apr-24 22:42 
GeneralRe: Read information from serial port in Mathlab format Pin
Luc Pattyn7-Apr-24 22:45
sitebuilderLuc Pattyn7-Apr-24 22:45 
QuestionDebugging the Sandbox Pin
Richard Andrew x645-Apr-24 3:43
professionalRichard Andrew x645-Apr-24 3:43 
QuestionIParsable? IFormattable? ICustomFormatter? IFormatProvider? TryParse! ToString! Pin
Tracy Dryden24-Mar-24 12:29
Tracy Dryden24-Mar-24 12:29 
AnswerRe: IParsable? IFormattable? ICustomFormatter? IFormatProvider? TryParse! ToString! Pin
Mycroft Holmes24-Mar-24 15:07
professionalMycroft Holmes24-Mar-24 15:07 
GeneralRe: IParsable? IFormattable? ICustomFormatter? IFormatProvider? TryParse! ToString! Pin
Tracy Dryden24-Mar-24 16:19
Tracy Dryden24-Mar-24 16:19 
AnswerRe: IParsable? IFormattable? ICustomFormatter? IFormatProvider? TryParse! ToString! Pin
Ralf Meier24-Mar-24 22:35
mveRalf Meier24-Mar-24 22:35 
GeneralRe: IParsable? IFormattable? ICustomFormatter? IFormatProvider? TryParse! ToString! Pin
Tracy Dryden25-Mar-24 7:51
Tracy Dryden25-Mar-24 7:51 
AnswerRe: IParsable? IFormattable? ICustomFormatter? IFormatProvider? TryParse! ToString! Pin
jschell26-Mar-24 12:00
jschell26-Mar-24 12:00 
Rant(Current) AI Rant. Pin
Gerry Schmitz17-Mar-24 12:29
mveGerry Schmitz17-Mar-24 12:29 
GeneralRe: (Current) AI Rant. Pin
Eddy Vluggen26-Mar-24 5:34
professionalEddy Vluggen26-Mar-24 5:34 
GeneralRe: (Current) AI Rant. Pin
lmoelleb1-Apr-24 21:44
lmoelleb1-Apr-24 21:44 
GeneralRe: (Current) AI Rant. Pin
lmoelleb1-Apr-24 21:49
lmoelleb1-Apr-24 21:49 
QuestionMessageBox Appearing Behind Window Pin
Richard Andrew x6417-Mar-24 1:27
professionalRichard Andrew x6417-Mar-24 1:27 
AnswerRe: MessageBox Appearing Behind Window Pin
OriginalGriff17-Mar-24 4:33
mveOriginalGriff17-Mar-24 4:33 

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.