|
Re: VB or any other GUI programmers
Ahhhh serial port protocols.... happen to enjoy these and theyre very cool once theyre done! My latest packetized data over serial control system was the remote terminal for an AGV (automated guided vehicle) that replenishes machines with parts at the company i work for. ANYHOW...
the basic components of any packetized system are, of course,the preamble, the data bytes and then the last thing is the checksum, which can be any number of bytes you want depending on how ridiculus you want to get. One byte is more than sufficient however. the easiest way to do a simple serial packetized system is to use fixed length packets, highly reccommended, meaning every packet you use has the exact same length. for you, you'll have your 8 pwm bytes, whatever else you want, and 1 checksum byte at the end. your preamble is not 100% required but makes the packet much more robust.
on the vb end, i'll give you a rough detail but if you have any further detail questions just post again... i'm also assuming your using VB6... oh i wish microsoft never switched to .NET... *sigh*...
First off, just search the MSDN library for how to open a serial port using the MSComm active x control, i always just copy the initialization code from msdn and modify it for what i need. be sure to set it up with the right baud rate and to put it in binary mode.
Make the scale on your sliders 0->255, and make up an array of bytes that is as big as the number of bytes long your packet is. assign the integer value (don't know if sliders put out floating point or int, i think integer but just be safe) of the slider to its place in the array of bytes.
after you've collected all your data, go ahead and calculate your checksum. you can do this by adding up all of your data bytes and puttiing that sum in a long type variable. after your done summing, divide by 256 (to get the number into a nice, manageable, one byte size) and assign that value to the checksum location in your array.
finally, the easy part, loop through the array and send it using the comm control.
On the recieving end of things, since you didnt mention it i assume that you know how to use kevin's serial driver and all. Make an array of unsigned chars in your software and read in the characters. start capturing the bytes to your array as soon as you see your preamble value, or if you used a multibyte preamble, wait for that. sum your data bytes in a variable just like you did in vb as you recieve. once you've recieved the right number of bytes (fixed length packets remember), then divide by 256 like you did before and see if you get the same checksum as was transmitted to you. if it matches, transmit some sort of acknowlidgement back to your vb app... i suggest using the ascii code for ACK so its one byte.... similarly, if the checksums dont match, send a NCK (also a ascii code). remember, your vb program must be written to listen for these. in the event of a nck, or a timeout, you should retransmit.
ok whew that was a lot of typing, hope that helps anybody that needed it, and i'll help if theres other questions, just post here.
-q
__________________
Electrical Engineer Illini
1024 | Programmer '06, '07, '08 | Driver '08
|