Click here to Skip to main content
15,898,881 members
Home / Discussions / C#
   

C#

 
GeneralRe: How to print the property values of an object to a file/console ? Pin
AtaChris22-Apr-24 21:23
AtaChris22-Apr-24 21:23 
GeneralRe: How to print the property values of an object to a file/console ? Pin
Pete O'Hanlon22-Apr-24 22:44
mvePete O'Hanlon22-Apr-24 22:44 
GeneralRe: How to print the property values of an object to a file/console ? Pin
Dave Kreskowiak23-Apr-24 4:10
mveDave Kreskowiak23-Apr-24 4:10 
QuestionHow to identify Key Combinations ? Pin
AtaChris15-Apr-24 6:26
AtaChris15-Apr-24 6:26 
AnswerRe: How to identify Key Combinations ? Pin
Dave Kreskowiak15-Apr-24 6:35
mveDave Kreskowiak15-Apr-24 6:35 
QuestionHow to send property values via eventhandler ? Pin
AtaChris7-Apr-24 7:41
AtaChris7-Apr-24 7:41 
AnswerRe: How to send property values via eventhandler ? Pin
OriginalGriff7-Apr-24 20:23
mveOriginalGriff7-Apr-24 20:23 
GeneralRe: How to send property values via eventhandler ? Pin
AtaChris15-Apr-24 6:46
AtaChris15-Apr-24 6:46 
GeneralRe: How to send property values via eventhandler ? Pin
OriginalGriff15-Apr-24 19:09
mveOriginalGriff15-Apr-24 19:09 
QuestionRead information from serial port in Mathlab format Pin
Victor Gonzalez 20245-Apr-24 9:43
Victor Gonzalez 20245-Apr-24 9:43 
AnswerRe: Read information from serial port in Mathlab format Pin
Richard Andrew x645-Apr-24 11:01
professionalRichard Andrew x645-Apr-24 11:01 
GeneralRe: Read information from serial port in Mathlab format Pin
Victor Gonzalez 20245-Apr-24 12:26
Victor Gonzalez 20245-Apr-24 12:26 
AnswerRe: Read information from serial port in Mathlab format Pin
jschell5-Apr-24 12:20
jschell5-Apr-24 12:20 
GeneralRe: Read information from serial port in Mathlab format Pin
Victor Gonzalez 20245-Apr-24 12:28
Victor Gonzalez 20245-Apr-24 12:28 
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 

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.