Click here to Skip to main content
15,895,084 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralRe: VB2010 Serial Port Tutorial? Pin
Simon_Whale12-Jan-12 6:26
Simon_Whale12-Jan-12 6:26 
GeneralRe: VB2010 Serial Port Tutorial? Pin
JDBravo12-Jan-12 6:38
JDBravo12-Jan-12 6:38 
AnswerRe: VB2010 Serial Port Tutorial? Pin
Luc Pattyn12-Jan-12 6:30
sitebuilderLuc Pattyn12-Jan-12 6:30 
GeneralRe: VB2010 Serial Port Tutorial? Pin
JDBravo12-Jan-12 6:46
JDBravo12-Jan-12 6:46 
GeneralRe: VB2010 Serial Port Tutorial? Pin
Luc Pattyn12-Jan-12 6:57
sitebuilderLuc Pattyn12-Jan-12 6:57 
GeneralRe: VB2010 Serial Port Tutorial? Pin
JDBravo12-Jan-12 7:17
JDBravo12-Jan-12 7:17 
GeneralRe: VB2010 Serial Port Tutorial? Pin
JDBravo13-Jan-12 6:11
JDBravo13-Jan-12 6:11 
AnswerRe: VB2010 Serial Port Tutorial? Pin
Luc Pattyn13-Jan-12 6:58
sitebuilderLuc Pattyn13-Jan-12 6:58 
Hi,

that looks pretty good.

Here are some comments on your design and coding style:

1.
if you have independent functions in the peripheral, it would be better to have a "command language" so you can send individual commands, and don't have to repeat values that already are in effect. An example, using ASCII for simplicity could be:
L0 = led off
L1 = led on
M0 = motor off
M1 = motor on
etc.

2.
if you need combinations of data values (your current LEDMSGOUT), you should really isolate that functionality and put it in a separate method, maybe even a separate class. So every time you repeat yourself (as in LEDMSGOUT = 40 + LEDOUT1 + LEDOUT2) your reflex should be: this ought to be a method. Reason: less code, better readability, easier to change when the interface is modified, etc.

3.
there is a nice XOR operator, ideal to toggle LED states for instance. (LED = LED Xor 1)

4.
when combining bits logically (rather than really adding numbers), it is better/cleaner/safer to use the OR operator, hence LEDMSGOUT = 40 Or LEDOUT1 Or LEDOUT2

5.
Why do you use uppercase for comments? IMO it makes them harder to read...


And then some pointers on multi-byte receive:


1. your code is somewhat ready for it, but it probably doesn't work well.

2. you may think of messages, the serial port doesn't care at all, all it knows are individual bytes. For instance, if you send several 10-byte messages and then read the input buffer at arbitrary points in time, you might get 4, then 12, then 3, then ... bytes. That is what makes receiving much harder.

3. the main problem is timing: DataReceived will (or may) fire as soon as one byte is coming in; it does not care about the number of bytes received, and is unaware of message lengths. It may however also be a bit late. Also, your sender in general may insert transmission delays right in the middle of a message when more important things need to be handled first (stepper motor probably has higher priority interrupts).

4. There are a couple of approaches one can take:

4a. by far the easiest is: use textual, not binary data, and define the language as having a message terminator (a special char that won't appear inside any message), then set NewLine to that char, and just do ReadLine (probably in a separate thread or backgroundworker, in a loop; i.e. don't use DataReceived event at all). The obvious special character is \n itself (careful, the default value of SerialPort.NewLine is system dependent, and may well be "\n\r" i.e. CRLF.

4b. a hack, works fine if there is a guaranteed gap in between messages: insert a delay (Thread.Sleep) that is sufficient to receive all bytes of a message, and only then actually read the incoming data. This enforces message alignment, provided you do it right (which includes: don't read more than one message at a time). However it is a hack, once it goes wrong, it may stay out of sync forever.

4c. read whatever is available (as you do now), however inside the event handler, copy it to a message buffer, and only signal the app when a full message is present (That basically is what ReadLine does for text).

4d. what would NOT be good is always handling one character at a time; that would work at low baud rates, it would be too much processor load at high comm rates.

5. you should design and code defensively, i.e. also consider comm problems may occur, such as a character getting lost or damaged. The general approach is to define a "protocol", probably message oriented, probably with message validation (some kind of checksum), and most likely with a mandatory acknowledge/come-again reply.

Smile | :)
Luc Pattyn [My Articles] Nil Volentibus Arduum

AnswerRe: VB2010 Serial Port Tutorial? Pin
JDBravo13-Jan-12 10:39
JDBravo13-Jan-12 10:39 
AnswerRe: VB2010 Serial Port Tutorial? Pin
Luc Pattyn13-Jan-12 13:51
sitebuilderLuc Pattyn13-Jan-12 13:51 
Questionvb.net access controls by name Pin
alejx9-Jan-12 16:10
alejx9-Jan-12 16:10 
AnswerRe: vb.net access controls by name Pin
Luc Pattyn9-Jan-12 16:28
sitebuilderLuc Pattyn9-Jan-12 16:28 
AnswerRe: vb.net access controls by name Pin
Abhinav S9-Jan-12 21:28
Abhinav S9-Jan-12 21:28 
AnswerRe: vb.net access controls by name Pin
Simon_Whale9-Jan-12 21:52
Simon_Whale9-Jan-12 21:52 
QuestionADF Scanning Pin
ivo759-Jan-12 6:37
ivo759-Jan-12 6:37 
AnswerRe: ADF Scanning Pin
Simon_Whale9-Jan-12 12:37
Simon_Whale9-Jan-12 12:37 
GeneralRe: ADF Scanning Pin
ivo7510-Jan-12 2:23
ivo7510-Jan-12 2:23 
GeneralRe: ADF Scanning Pin
Richard MacCutchan10-Jan-12 2:39
mveRichard MacCutchan10-Jan-12 2:39 
QuestionCreate Guitar Tunner using Vb.net Pin
Mangore759-Jan-12 4:44
Mangore759-Jan-12 4:44 
AnswerRe: Create Guitar Tunner using Vb.net Pin
Simon_Whale9-Jan-12 4:55
Simon_Whale9-Jan-12 4:55 
GeneralRe: Create Guitar Tunner using Vb.net Pin
Mangore759-Jan-12 18:04
Mangore759-Jan-12 18:04 
GeneralRe: Create Guitar Tunner using Vb.net Pin
Simon_Whale9-Jan-12 21:49
Simon_Whale9-Jan-12 21:49 
GeneralRe: Create Guitar Tunner using Vb.net Pin
Mangore759-Jan-12 22:52
Mangore759-Jan-12 22:52 
GeneralRe: Create Guitar Tunner using Vb.net Pin
Dave Kreskowiak10-Jan-12 2:07
mveDave Kreskowiak10-Jan-12 2:07 
QuestionLogin Form Pin
Taherhamdy7-Jan-12 19:39
Taherhamdy7-Jan-12 19:39 

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.