Click here to Skip to main content
15,923,164 members
Home / Discussions / C#
   

C#

 
AnswerRe: Creating Events Pin
DaveyM693-Jun-10 1:38
professionalDaveyM693-Jun-10 1:38 
GeneralRe: Creating Events Pin
Roger Wright3-Jun-10 4:09
professionalRoger Wright3-Jun-10 4:09 
QuestionSystem.DBNull returned for whitespace strings Pin
GDavy2-Jun-10 22:07
GDavy2-Jun-10 22:07 
AnswerRe: System.DBNull returned for whitespace strings Pin
Pete O'Hanlon2-Jun-10 23:30
mvePete O'Hanlon2-Jun-10 23:30 
GeneralRe: System.DBNull returned for whitespace strings Pin
GDavy2-Jun-10 23:59
GDavy2-Jun-10 23:59 
GeneralRe: System.DBNull returned for whitespace strings Pin
Pete O'Hanlon3-Jun-10 0:23
mvePete O'Hanlon3-Jun-10 0:23 
QuestionProblem reading multiple bytes from serialport Pin
myreki2-Jun-10 21:35
myreki2-Jun-10 21:35 
AnswerRe: Problem reading multiple bytes from serialport Pin
Luc Pattyn3-Jun-10 2:14
sitebuilderLuc Pattyn3-Jun-10 2:14 
OK, let's give you some ideas:

1.
Correctly and reliably receiving serial data is orders of magnitude more difficult, than sending data. Sending is easy, all it takes is a Write() and it will happen eventually. Reading is difficult as you have to read at an appropriate moment, and you may or may not get troubled by data holding messages and messages being chopped to pieces by your reading code.

2.
you did not explain when and where you execute SerialPort.Read(). Is it in a button click handler? a timer tick handler? a DataReceived handler?

3.
When it is a command-response situation, your PC app sends a command to the peripheral; first it takes a few milliseconds for the entire command to leave the PC's serial port; then the peripheral will get those bytes one by one, and probably not do much until the entire command has been received. Then it needs to react to it (interpret command, execute command, maybe wait on the termination of an analog/digital conversion operation, etc), then construct a reply message, then start sending that.

4.
If your PC app sits waiting for the response, it probably wants to react as soon as possible. So having a polling loop or simply use the DataReceived event, will launch the reading code as soon as something got received, however that something will not be the entire reply as it takes about 8 msec for it to travel the serial channel (9600Bd is roughly 1 byte per millisecond). So what to do?

5.
Here are some schemes, none is perfect, you must decide which fits your situation most:
A. not event-driven: wait long enough, then read; as in write,sleep, read.
B. text communication: the serial driver knows about text lines, if you ask for a line of text, you will obtain all characters up to the first end-of-line (You can set the port's NewLine string). This only works for text, and requires your text not to contain an accidental end-of-line in the middle of the data.
C. event-driven: use DataReceived event, and in its handler sleep long enough to cover the time span from first byte to last byte, say 8 milliseconds. Disadvantages: your sleep time will be rounded up to probably 10 to 30 msec (see my timers article), and if your peripheral hesitates in the middle of its transmission, you still won't get it all.
D. use DataReceived event, and don't read until there are enough bytes available; then read. This will fail I guess, as there is no guarantee that each and every incoming byte will cause a DataReceived event.
E. Buffer it yourself: collect incoming data, independent of what you expect; then investigate what you have so far, and when it makes sense, consume it. When it does not, either wait for more, or discard and wait for more.

6.
To complicate matters, communication can go wrong. The other side may suddenly fail; the cable could become disconnected; a byte could get damaged; a parity error could occur; etc. You should code defensively in general, you should do even more so in communication. Therefore (E) above is the best option, not the easiest one.

7.
Conclusion
if you can rely on a unique character to indicate the end of a message (such as a CR or LF in a text string), then that is by far the easiest way to go, as now the driver is in charge of gathering messages. Otherwise, consider your own buffering, read everything, and locate, decode and process packets in your buffer; do not expect messages to always be received correctly without problems (unless you can afford to wait long enough because nothing is urgent and messages are few and far apart).

Smile | :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]

I only read formatted code with indentation, so please use PRE tags for code snippets.

I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).

QuestionNotifyPropertyChanged and ReadValue() not working Pin
Mycroft Holmes2-Jun-10 21:20
professionalMycroft Holmes2-Jun-10 21:20 
AnswerRe: NotifyPropertyChanged and ReadValue() not working Pin
Henry Minute2-Jun-10 22:14
Henry Minute2-Jun-10 22:14 
GeneralRe: NotifyPropertyChanged and ReadValue() not working Pin
Mycroft Holmes2-Jun-10 22:39
professionalMycroft Holmes2-Jun-10 22:39 
AnswerRe: NotifyPropertyChanged and ReadValue() not working - kludged Pin
Mycroft Holmes2-Jun-10 22:21
professionalMycroft Holmes2-Jun-10 22:21 
AnswerRe: NotifyPropertyChanged and ReadValue() not working Pin
GDavy2-Jun-10 22:25
GDavy2-Jun-10 22:25 
GeneralRe: NotifyPropertyChanged and ReadValue() not working Pin
Mycroft Holmes2-Jun-10 22:37
professionalMycroft Holmes2-Jun-10 22:37 
GeneralRe: NotifyPropertyChanged and ReadValue() not working Pin
GDavy2-Jun-10 22:53
GDavy2-Jun-10 22:53 
GeneralRe: NotifyPropertyChanged and ReadValue() not working Pin
Mycroft Holmes2-Jun-10 23:27
professionalMycroft Holmes2-Jun-10 23:27 
QuestionA Free Licensing System? Pin
Matt U.2-Jun-10 12:26
Matt U.2-Jun-10 12:26 
AnswerRe: A Free Licensing System? Pin
Richard Andrew x642-Jun-10 17:53
professionalRichard Andrew x642-Jun-10 17:53 
AnswerRe: A Free Licensing System? Pin
Johnny J.2-Jun-10 22:46
professionalJohnny J.2-Jun-10 22:46 
Question[C#] Propertygrid - Textbox and listbox for previous values Pin
canard292-Jun-10 9:28
canard292-Jun-10 9:28 
AnswerRe: [C#] Propertygrid - Textbox and listbox for previous values Pin
coder21k2-Jun-10 20:55
coder21k2-Jun-10 20:55 
AnswerRe: [C#] Propertygrid - Textbox and listbox for previous values Pin
Satya Priya Arya23-Aug-11 19:04
professionalSatya Priya Arya23-Aug-11 19:04 
QuestionMessage Removed Pin
2-Jun-10 8:10
dia 20102-Jun-10 8:10 
AnswerRe: calculating minutes in C# Pin
Luc Pattyn2-Jun-10 8:19
sitebuilderLuc Pattyn2-Jun-10 8:19 
GeneralRe: calculating minutes in C# Pin
Dan Mos2-Jun-10 8:32
Dan Mos2-Jun-10 8:32 

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.