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

C#

 
GeneralRe: Exceptions and out parameters Pin
mringholm10-Mar-09 5:26
mringholm10-Mar-09 5:26 
AnswerRe: Exceptions and out parameters Pin
Luc Pattyn9-Mar-09 14:55
sitebuilderLuc Pattyn9-Mar-09 14:55 
GeneralRe: Exceptions and out parameters Pin
mringholm10-Mar-09 5:23
mringholm10-Mar-09 5:23 
AnswerRe: Exceptions and out parameters Pin
Luc Pattyn10-Mar-09 6:00
sitebuilderLuc Pattyn10-Mar-09 6:00 
QuestionIXMLHTTPRequest Pin
tsahiB9-Mar-09 12:25
tsahiB9-Mar-09 12:25 
QuestionSerialport speed unstable C# Visual Studio 2008 Pin
jondaeh9-Mar-09 10:18
jondaeh9-Mar-09 10:18 
AnswerRe: Serialport speed unstable C# Visual Studio 2008 Pin
Dan Neely9-Mar-09 11:01
Dan Neely9-Mar-09 11:01 
AnswerRe: Serialport speed unstable C# Visual Studio 2008 Pin
Luc Pattyn9-Mar-09 11:16
sitebuilderLuc Pattyn9-Mar-09 11:16 
Hi,

IMO DataReceived will not necessarily fire for each byte that is received; basically when one or more bytes are available, you will get an event, and they expect you to obtain all available data each time. If you don't try getting it all, the extra data will sit in the input buffer, and your code will lag getting it out (the ReadExisting method is pretty useful for that provided your data is text, yours isn't); in the end you will loose data unless handshaking has been organized, in which case you only will get a potentially big latency (depending on buffer size).


I do have some more remarks:

1. "from 4 times a second to a thousand" is not compatible with 4800Baud, you need at least 9600Baud to be able to communicate the data at 1000 bytes/s.

2. the line display.Text = line; will never execute since InvokeRequired will always be true

3. if your serial port happens to be a USB-to-serial cable, be aware that those do buffer some characters, so they can transmit data in small packets, making better use of USB bandwidth.

4. I trust you did not enable handshaking and left ReceivedBytesThreshold at 1.


And some suggestions:

1. add a StopWatch to your code, and show both its value (up to the milliseconds) and the incoming data in a Console.WriteLine inside your DataReceived handler; that way you will see how the data comes in, and then decide whether or not it gets processed as it should.

Alternatively, add an incrementing counter to DataReceived or setDispText and have it display in another Label (using Invoke again when in DataReceived).

2. add an ErrorReceived handler, maybe something is going wrong and currently gets ignored.


[ADDED]
If you are mainly interested in the most recent value, there are alternative approaches to reading binary bytes:

1. use SerialPort.BytesToRead and Read(byte[],...) to read what is available, then process the last byte only.

2. instead of wiring DataReceived, just launch a thread that performs a synchronous ReadByte() inside an "eternal" loop, and choose an appropriate ReadTimeout value (maybe 100msec is fine). Such polling will provide better "real-time" behavior (never trust Windows to do any of that) but it will be more expensive though.
[/ADDED]

Smile | :)

Luc Pattyn [Forum Guidelines] [My Articles]

- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


AnswerRe: Serialport speed unstable C# Visual Studio 2008 Pin
0x3c09-Mar-09 11:17
0x3c09-Mar-09 11:17 
AnswerRe: Serialport speed unstable C# Visual Studio 2008 Pin
Jonathan Davies10-Mar-09 1:56
Jonathan Davies10-Mar-09 1:56 
Questionhow to know who executed my application ? Pin
shabya9-Mar-09 10:11
shabya9-Mar-09 10:11 
AnswerRe: how to know who executed my application ? Pin
Jimmanuel9-Mar-09 10:36
Jimmanuel9-Mar-09 10:36 
AnswerRe: how to know who executed my application ? Pin
harold aptroot9-Mar-09 10:40
harold aptroot9-Mar-09 10:40 
GeneralRe: how to know who executed my application ? Pin
shabya10-Mar-09 12:20
shabya10-Mar-09 12:20 
AnswerRe: how to know who executed my application ? Pin
Dragonfly_Lee9-Mar-09 19:47
Dragonfly_Lee9-Mar-09 19:47 
GeneralRe: how to know who executed my application ? Pin
shabya10-Mar-09 12:21
shabya10-Mar-09 12:21 
GeneralRe: how to know who executed my application ? Pin
Dragonfly_Lee11-Mar-09 1:17
Dragonfly_Lee11-Mar-09 1:17 
Questionwhy is it not writing to screen the first line/value in the txt file this code? Pin
wwwxyz9-Mar-09 10:07
wwwxyz9-Mar-09 10:07 
AnswerRe: why is it not writing to screen the first line/value in the txt file this code? Pin
Rutvik Dave9-Mar-09 10:19
professionalRutvik Dave9-Mar-09 10:19 
GeneralRe: why is it not writing to screen the first line/value in the txt file this code? Pin
wwwxyz9-Mar-09 10:42
wwwxyz9-Mar-09 10:42 
GeneralRe: why is it not writing to screen the first line/value in the txt file this code? Pin
Rutvik Dave9-Mar-09 11:02
professionalRutvik Dave9-Mar-09 11:02 
GeneralRe: why is it not writing to screen the first line/value in the txt file this code? Pin
wwwxyz9-Mar-09 11:09
wwwxyz9-Mar-09 11:09 
GeneralRe: why is it not writing to screen the first line/value in the txt file this code? Pin
Guffa9-Mar-09 17:57
Guffa9-Mar-09 17:57 
GeneralRe: why is it not writing to screen the first line/value in the txt file this code? Pin
wwwxyz10-Mar-09 2:00
wwwxyz10-Mar-09 2:00 
Questionlabels on line graph. Pin
alexleigh9-Mar-09 9:22
alexleigh9-Mar-09 9:22 

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.