Windows Interface to send/recieve data with Protoboard
tae2010
Posts: 38
Hello there,
I wanted to write a VB.net program that will interface directly with my USB Protoboard so that I can read sensor data from my board. I also want to be able to send data to the board.
Does anyone know how to get started with the basics to communicate with the board? I realize that the board has to be preprogrammed with some SPIN code first, but then I want to have interaction with a PC.
Thanks
I wanted to write a VB.net program that will interface directly with my USB Protoboard so that I can read sensor data from my board. I also want to be able to send data to the board.
Does anyone know how to get started with the basics to communicate with the board? I realize that the board has to be preprogrammed with some SPIN code first, but then I want to have interaction with a PC.
Thanks
Comments
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Leon Heller
Amateur radio callsign: G1HSM
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Ken Gracey
Parallax Inc.
Follow me at http://twitter.com/ParallaxKen for some insider news.
Your VB never has do any propeller spin preprogramming, if that was what you were thinking.
You can program using the propeller tool over the serial line as you develop your spin code. Once your spin code is running, you can switch over to VB and have your VB code open the serial line and start using it. Once your propeller code is stable, you can write it to EEPROM and let the propeller boot from that. So your VB never has do any propeller spin preprogramming.
The only tricky thing you'll run into is that your VB code has to release the serial port whenever you need to use the propeller tool to send new propeller code down the serial line to the propeller. Aside from that, once the propeller tool code download is complete, the serial line is yours to do whatever you want with.
Then it's up to you to develop sender-listener routines on both ends, maybe using FullDuplexSerial on the propeller end, as Lean suggested.
On a recent project I just needed to control 4 binary outputs. I used the Parallax Serial Terminal object so I could interact with a simple terminal program, then I added a VB.net program to use the same interface.
Here is a very simple SPIN program that would get you started. It would allow me to set the output states and see the current states.
·For a more·advanced interface·protocol,·I'd consider using some sort of CRC to ensure you have good data.
If you start with a good protocol, you can then add more message/command types later without interfering with your existing ones.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jim Fouch
FOUCH SOFTWARE
I would like to·travel the same road·with You, tae2010.
My Transportation will be the PPDB, which is an·AWESOME product by the way.
I will be using VB express 2010,(cuz free is good for me right now), VB is·what I've been playing with since 3.0..
and I do mean PLAYING with, as it is just a Hobby for Me. I Have lots of Respect·for the Pro's that write code,
for a living. They make My Hobby Possable, Thank You Very Much..
Anyways,
tae2010 how far have You gone in this?
Check this link out http://forums.parallax.com/showthread.php?p=671804
I know it is for the stamp, but it is was somewhat helpfull.
This link may have some answers in it...
http://forums.parallax.com/showthread.php?p=879440
Have You·studied·some of the·Serial Comm demonstrations from the Propeller Tool?,
The fine folks here will have the ones I have missed posted shortly,
A·Small list here. There are many more than this.
HelloFullDuplexSerial.spin, ·(Found in the Propeller Tool Example.Help.PE kit, Objects lesson #6)
Parallax Serial Terminal Demo.spin,· (found in the Propeller Tool Example.help.library)
PE Kit Tools Propeller to PC Terminal Communication· (found in the Object Exchange, Protocol object list)
Eight talking Cogs.spin (found on the OBEX, Fun object list)
TerminalLedControl.spin,·(found it in the Propeller Tool Example.Help.PE kit, objects lesson #6)
This last one is of special interest to Myself, as it is exactly what I wish to do, Except I wish to use Labels and
Check Boxes to send the desired Pin States back and forth, Instead of Serial Terminal or HyperTerminal..
It's not that I don't like the Parallax Serial Terminal, it is a very good thing for debuging and such, I just want
the User interface options that are VB.
I have been able to·SEND all manner of data FROM Propeller TO VB, However, I have had no luck at all trying
to·RECIEVE data on the Propeller FROM VB.
My current attempt in VB, is·a Container with 8 CheckBoxes, linked to labels and TextBoxes that display the
"Binary" of the·CheckBoxes, like if·CheckBox #1,3,5,and·7 are Checked then the·Label will display (10101010)..
Then, with any luck and some fine code examples, I should be able to SEND the Label.Text on to the Propeller.
Then from there,·divide, multiply, add , and throw in a few of these for good measure, %<=#>+=^&} and,
Probably one or two of these just to be sure, @~~$_^ ... And I should be able to have complete control over
the Pins and Leds in question.(0..7),(8..15),ect,
I even have a Button that Toggle's all of the Pin States at once, for no reason at all, other then it will be so
cool to·Blink·some or all·of the leds at once...
At any rate, Please forgive(or at least be patient with) this rambling of a Reply, And good luck in this Quest.
I·'am sure·the Wonderfully Talented People here will help to·put the right Spin on this
http://www.theabramgroup.com/basicstamp/
·
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jim Fouch
FOUCH SOFTWARE
@Ttailspin: Ive been coding with VB.net for a couple years now, and this'll be my third Windows application with VB.net, so Im not new to the language, Im just new to the serial communication with the Propeller and the SPIN code. Ive done serial communication in the past a few times, but very limited and not USB, so Id like to expand on that.
I havent actually started coding on this just yet, Im just trying to get an idea of how to begin with the serial/usb communications. Once I get something going, I'll be sure to share my code to help anyone who needs it along.
My application will be very simple to begin with: all I want to do initially is to read some sensor data from the board and display it on screen. So nothing too fancy, as Ive seen already.
Id like to ask, is it possible to program multiple SPIN codes into the Propeller? Or does it need to be one massive program?
Thanks.
You may learn more by starting from a blank form in vb.net and adding some text boxes and buttons and start to add some code. Then ask here for help if things are not working.
Alternatively, the entire source code of my program is here smarthome.viviti.com/build go to step 11.
To start simple, you need to set up the serial port. (these are code fragments)
and I found some problems outputting and inputting strings (bit 7 problems) so I did it with arrays
and set up the serial port
and then put a timer on the form and polled the serial port
and to read some bytes
and to send a byte
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
www.smarthome.viviti.com/propeller
Be very careful with BytesToRead and the SerialPort.Read method. SerialPort.Read always returns the number of byte read. You should check the return value for the byte read and use it to enumerate the receive buffer, not BytesToRead.
I like to handle the parsing above in the DataReceived event handler or maybe another method or class. Generally, the handler is a little more complex because I'm looking for specific chunks of data (packets).
The other thing you'll need to handle is updating the UI if you're building a traditional Win Form application. Sorry, for the C#...
Post Edited (Mike G) : 5/11/2010 1:37:37 AM GMT
The above examples are great for sending data TO the PC, and I am sure the information is there, but.
I just can't seem to get it thru My thick skull, on how to RECIEVE data FROM the PC TO Propeller...
I can SEND "Hello World" and "10101010" and all kinds of four letter words out TO the PC, FROM the·Propeller.
But just can't seem to Recieve the data·as expected·with·FullDuplexSerialPlus.spin or Parallax Serial Terminal.spin
·This is the VB part I use to try and send what the Text Box (TxtSend.Text) contains...
I use the same ·_port. setup·as I use to Recieve the "Hello World"s FROM the PROP, in other words,
This·Button (CmdSend), is on the same Form as the (CmdRecieve) Button that I know works...
The Spin Code... welllll.. She aint so nice..
It does flash the leds connected to pins(9) and (10) very nicely, just as bright and flashy as I could want...
I just happen to want Pin(15)·and/or·Pin(8) or any other selected Pin( )·to be flashy·at·My whim...
I am just going to throw My hands in the air, declare it can't be done,
leave this code laying around, and see what happens...
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon McPhalen
Hollywood, CA
It's almost like your code, except for a few things -
1. it uses single characters to flag particular actions, not multicharacter strings
2. it declares a list of constants for the flags, because these must match between Windows and the propeller
3. it receives everything inside the continuous loop, but discards unknown characters
It looks to me like you're almost there!
Take a look at David B's stuff above or one of the FullDuplex objects.
Post Edited (Mike G) : 5/12/2010 3:45:25 AM GMT
That is one fully loaded Serial Terminal Program, It will save Your com settings and load them on start up and everything...
Pretty amazing, at least to a newbie like Myself, I swear She used every Keyword and Command available in VB.
The Commenting is exellent, and I learned a ton from it, and still see a ton more to learn from the samples.
However, I thought that it might be overkill for Me right now, and thought I could start with something a little less sophisticated.
thanks again, I don't think I would have found that site anytime soon.
@David B, Thank's for that Code snippet, I wonder if I made the right choice in using "Case" instead of "If and ElseIf",
will it make a difference?, I thought that "Case" would look pretty. I like the idea of single char's instead of Strings,
seems like I would have more Control over the individual Pins... hmmm...
@Mike G, Thank You, and You are absolutly right, That is indeed Spin Language 101, I did not Declare or initialize a Pointer,
Or a Buffer for that matter, I bet it would pay off, if I actually did Declare them...Sigh.. please be patient, some handholding may
be Required for some of the harder stuff, Like declaring variables, and finding the AnyKey on My keyboard...
I will School Myself on Byte, Word, and Long. Maybe actually paying attention this time..
I did think that a byte would let me have a number(integer) from 0 to 255, and as long as I stayed in those bounds all would be well.
Just for fun, I added a Case : "0" toggle(14), to see if the Variable "WhatToDoWithIt" was being made equal to 0,
It did not seem to make a difference, it just went to other :, and pins (9) and (10) still do all the flashing...
Anyways, Thanks again to all, and I will keep Grinding and Chipping away at the samples and tutorials.
The leds WILL flash at My whim...
@Mike G, Thanks again, Thats it, thats what I was trying to do for now, Your snippet worked great...once.
heh, when I ran it, it would let Me Pick anyone of the Pins and·Toggle it 50 times as expected,
Then it would only Toggle Pin(23), and only a reset would allow a new Pin choice...
So I thought along the lines of needing to clear the RxBuffer, when a new Pin is wanted. and Behold, there it was,
Parallax Serial Terminal.RxFlush, To My utter amazement, it worked, I did'nt even have to threaten My PPDB
With My cup of coffee.(It never fails to amaze Me how much coffee a bread board can hold..), Anyways here it is.
Rough as it is, it will serve as a great starting point to educate Myself further in this endeaver..
And·a VERY SMALL snippet of VB code, this is just for the Send Button, Add a TextBox and its good to go.
·Thanks again to all.
I will see what kind of trouble, err.. refinements I can make from all this new knowledge I have gained...
Post Edited (Ttailspin) : 5/12/2010 8:14:55 PM GMT
Thanks.
@Ttailspin, cool - didn't know you had to flush the buffer.
Post Edited (Mike G) : 5/12/2010 6:24:42 PM GMT
After the PC program sends a code to the propeller that requests some data value, the PC would then be expecting a series of bytes that represent a word, or long, or array of values.
So the propeller's response to that code would be to send exactly those bytes, in the order that the PC will re-aassemble them into a word, or long, or array of values.
In the example snippet of code I attached earlier in this thread, you can see references to a helper function I wrote called "sendlong(value)" to do just that - it sends the 4 bytes of a long in the same order that the PC reassembles then into a long in the format used by your PC language, VB, in your case.
There are some Rules to follow, the main one right now is obvious to some, but very easy to miss when You decide to not see it.
You must send Data exactly as it is Declared in DAT. (I.E. sending led8 will flash led 15, where as sending LED8 WILL flash led 8)
In other words, Check Your Spelling, including and especially capitilization...
I know this can be fixed with all the various String Manipulators that are available from the OBEX, But keeping in the spirit of KISS,
I will just say, "Check Your Spelling for now.." ·Duh, it's Visual Basic, use a Masked TextBox...
You Must start the Spin Program before opening the PC Serial Port, I'm not making that up, it's just the way it is...
You must SET MATCHING BAUD RATES, This example uses 57600 Baud, it could easily be changed to 115200, or 2400, or ????
No matter, As long as BOTH the Propeller, and the PC are using the same Baud Rates.
Oh and just to be clear, I am using the Prop Pro Development Board, I have jumpers from Pins(8..15) out to the Leds(8..15),
And also some jumpers from Pins(0..7) out to the Push Buttons(0..7), but these have nothing to do with anything...YET..
I see many If ina( )'s in My future...
@Mike G, Thanks again for the strcomp tip, that is really where the magic happens in this code..
and by the way, I really didn't know that "Flushing" the buffer was the way to go either, I just know that it is working,
Maybe not the bestest or the cleanest, but I think I can roll up a phattie with what I have here, and refine, refine, refine...
Post Edited (Ttailspin) : 5/12/2010 11:41:39 PM GMT
The code I have now is:
Im using the Serial Terminal for now to get the Propeller code perfected.
What Im trying to do is change the code so that I send a string to the board, such as "SEND_ON", and then the the board will allow the strings to send TO the PC as the Pins go high and low. (The Text is simply "ON", "OFF") But the Problem is that I try to send text to the board through the terminal so that I can see if it receives or not, but the board continuously sends text via the Serial port, so I am unable to type a command.
Im totally lost as to how I can send a command to the board to enable/disable the board sending data to the pc (and switching those pins high/low), instead of it just sending data nonstop.
Post Edited (Mike G) : 5/13/2010 2:01:24 AM GMT
From the code I posted, I was really trying to enable/disable the data that was being sent from the Propeller to the PC. So through my command to the board, it should turn this Sub Function on and off:
So Im thinking maybe something along the lines of:
I think you're struggling with conditional statements. Look up the "If" statement in the Propeller help file.
In your example, once you enter that repeat loop, you're in it forever.
Post Edited (Mike G) : 5/13/2010 2:13:39 PM GMT
Heres an example of some Pseudo code (plus code):
Press "w" to flash LED 16
Press "b" to stop
Press "s" to flash LED 23
Press "b" to stop
You must press "b" to get out of the loop.
Or
Press "w" to flash LED 16
Press "s" to flash LED 23
press "enter" for idle mode
The key is the use of FullDuplexSerial since the method rxcheck does not block.
Edit: this will display key press values
Post Edited (Mike G) : 5/15/2010 3:07:54 AM GMT
I have to comment everything in My own kind of babble, It may not be the best style, but it sure helps me to walk thru it.
Anyways, to Recap what I've done so far...
Made Visual Basic do this>>> http://forums.parallax.com/attachment.php?attachmentid=49017
Then I asked for help on this forum and wound up with this...
All of My comment's are My own, Regard them at Your own Peril...
Maybe this will help or Maybe not.