Computer to basic stamp using Visual Basic
DanFa
Posts: 3
Hi,
I am trying to create a program on my computer using Visual Basic. To send or receive·Data to a Basic stamp. I have already found a way of sending Data fine. But when I try receiving data I ether get the right data or I get some·weird data. If any one has any idea how to send or receive data please tell me.
This is the code I use to receive:
Dim A As String
SerialPort1.Open()
A = SerialPort1.ReadByte
SerialPort1.Close()
Label1.Text() = A
·
I am trying to create a program on my computer using Visual Basic. To send or receive·Data to a Basic stamp. I have already found a way of sending Data fine. But when I try receiving data I ether get the right data or I get some·weird data. If any one has any idea how to send or receive data please tell me.
This is the code I use to receive:
Dim A As String
SerialPort1.Open()
A = SerialPort1.ReadByte
SerialPort1.Close()
Label1.Text() = A
·
Comments
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
1 + 1 = Window
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
0............................................0
0............................................0
0.(Microman171@hotmail.com)..0
0............................................0
0............................................0
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
Dim A As Byte <
Declare as Byte
SerialPort1.Open()
A = SerialPort1.ReadByte
SerialPort1.Close()
Label1.Text() = A.ToString <---- Turn the Byte into a String
This way you are controlling the conversion and not VB. Just something to try.
Charlie
On the Form design page of the VB.Net IDE, double click the button and it will take you to the "on click' event where you can ten put in your code for processing the button click.
Charlie
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
I know how to use the buttons I just don't know how to hook the button to the command in the toolbox serial port
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
1 + 1 = Window
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
0............................................0
0............................................0
0.(Microman171@hotmail.com)..0
0............................................0
0............................................0
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
You need to add the serial control to your form yhe same way you add a button. The control will not be visible when you run your program, but it will appear when developing. You then need to configure the control's properties for your setup.
Think of the form as a container that holds various controls. The serial control allows the form and it's other controls to access the serial port. Since you want a button to access the serial port, you have to add code to one of the buttons "events" (such as being clicked). Double clicking the button in the editor will allow you to add code to the button's Click event. This is where you add the code that talks to the serial port control that you added.
You will want to:
1. Open the port
2. Read from / Write to the port
3. Close the port
Take a look at the code samples above, and see if it makes sense.
But I am still having problems. When I try receiving the number 123 or 12 it's fine. But if I try 1234 or 321 it displays 210 and 65
This is the code for the basic stamp
DO
DEBUG 123
PAUSE 100
LOOP
And this is the code for Visual basic
Dim A As Byte
SerialPort1.Open()
A = SerialPort1.ReadByte
SerialPort1.Close()
Label1.Text = A.ToString
I'm thinking it might be a kind of problem, like if you were to try sending 123 to the debug terminal with out the DEC.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
1 + 1 = Window
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
0............................................0
0............................................0
0.(Microman171@hotmail.com)..0
0............................................0
0............................................0
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
1. The form I am using (There is a hidden label)
2. These are the settings that I don't know how to set
Just for everybodys information I'll make a tutorial on how to connect the BASIC stamp to the VB.NET IDE (once I know how ) in the projects forum.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
1 + 1 = Window
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
0............................................0
0............................................0
0.(Microman171@hotmail.com)..0
0............................................0
0............................................0
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
There are more parameters for serial port control in Windows than on a BS2. You should be able to use the default settings, or if you need to change anything, the properties you want to pay attention to are (in order on the properties box):
Name
BaudRate
DataBits
Parity
PortName
StopBits
You might see port settings for the PC abbreviated like 9600,8N1, which means:
BaudRate: 9600
DataBits: 8
Parity: None
StopBits: 1
These settings are the equivalent of BS2 baudmode setting of 16468 with the SERIN and SEROUT commands. So after you figure out your BS2 settings, you can change the settings in VB.Net to match.
DanFa:
On the BS2, try:
Main:
SEROUT 16, 16468, 100, [noparse][[/noparse]"This is a test...", CR] ' baudmode: 9600,8,N,1
Pause 500
GOTO Main
Depending on your results, you might also try replacing the CR with LF (CR = Carriage Return, LF = Line Feed New Line). It makes a difference in some cases in what the PC recognizes.
Is there something wrong in the code?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
1 + 1 = Window
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
0............................................0
0............................................0
0.(Microman171@hotmail.com)..0
0............................................0
0............................................0
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
· loByte = theLong \ 256
· hiByte = theLong Mod 256
The backwards division operator is for integer division.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcn7/html/vbcondatatypes.asp
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
1 + 1 = Window
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
0............................................0
0............................................0
0.(Microman171@hotmail.com)..0
0............................................0
0............................................0
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
BS2 Code
Main:
SEROUT 16, 16468, 100, [noparse][[/noparse]"This is a successful test...", LF] ' baudmode: 9600,8,N,1
PAUSE 500
GOTO Main
VB.NET Code
1. Within the Form1 Events Load Event
SerialPort1.Open() ' Opens the Serial Port when the form is loaded
2. Within the Form1 Events FormClosed Event
SerialPort1.Close() ' Closes the Serial Port when the form is closed
3. Within the Button Events Click Event
Dim MyMessage As String ' Creates a string variable named MyMessage
MyMessage = SerialPort1.ReadLine ' Reads from the serial port until a NEWLINE character appears, assign values to MyMessage
MessageBox.Show(MyMessage) ' Displays a pop-up message box with the string value MyMessage
NOTES
1. If you aren't certain how to place the code within the correct events, look at the pull-down menus at the top of the VB.NET code editor window. You can double click the form to get the code editor window.
2. Place the commented commands included above between the Private Sub & End Sub lines for the specified events. You can include the comments, they will help you remember what the code is doing.
***BONUS QUESTION***
Why does the following BS2 Code cause the VB.NET Form to hang indefinitely?
BS2 Code
Main:
SEROUT 16, 16468, 100, [noparse][[/noparse]"This is a successful test...", CR] ' baudmode: 9600,8,N,1
PAUSE 500
GOTO Main
Post Edited (Kevin Wood) : 1/22/2006 1:29:30 AM GMT
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
eg it turns yellow without telling me whats wrong or how to correct it. Whats going on?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
1 + 1 = Window
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
0............................................0
0............................................0
0.(Microman171@hotmail.com)..0
0............................................0
0............................................0
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
"Reads up to the System.IO.Ports.SerialPort.Newline value in the input buffer."
So, if you use the CR, it never sees a LF newline character, and it just keeps waiting for one. I'm not certain what the conceptual difference is between LF & CR, but if you look at each version of SEROUT in the debug window, the output with LF is staggered, whereas the out put from CR isn't.
Post Edited (Kevin Wood) : 1/22/2006 1:33:14 AM GMT
I believe CR and LF come from typewriter technology -- though the Debug Window actually works like a typewriter in that when you send a CR (return carriage to left) you get a line-feed as well.· Not all terminals behave in this manner; in Hyperterminal, for example, you have to tell it to add LF to CR if that's what you want.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
True terminals have to have the ability to write on the same line over and over again
...Encrypted messages were decrypted one letter at a time...multi passess over the same line until the entire line was decoded.
...Control codes didn't print, but they used "space": the tty could backup over the control codes to print messages...
One example, from my days with the B5200, 15 bell codes, followed by 15 back spaces followed by "change reel ......"
And the reason, well, the B5200 could have as many as 30 reel drives on it, and it could (and did) demand a number of reels to be changed darn near the same time...
... being the intern, I got to change the reels, so I modified the original code so it would only ouput 1 line per reel request, where as the original codeput 15 blank lines, the request line, and then 15 more blank lines.. (so you could tare off the paper and take it with you)
As CRT's become more affordable and popular, and so program didn't need recoding (or recompiling) to take advantage of the CRT's screen: They Simply force the desired response at the CRT's "interpeter" level; hence the number of different protocalls for end of line on various terminals that are use.
I have learned, over the years, to identify the "EOL" (End of Line) charactor. (such as a CRLF, CR or LF, and including a null (0) charactor.)
Many such main frames still in usage today only use CR, LF or 00, not CRLF to indicate end of line.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Just tossing my two bits worth into the bit bucket
KK
·
but with vb.net 2005 if you set the serialport1.newline = 10 it will not work, you need to set serialport1.newline = vbLF for it to work... go figure.
If you send a CR from the STAMP as your line terminator, you need to change serialport1.newline = vbCR
Just more info for the vb.net serial problem.
Charlie
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
1 + 1 = Window
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
0............................................0
0............................................0
0.(Microman171@hotmail.com)..0
0............................................0
0............................................0
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
Please fill free to modify/change the code to suit your needs and post any more insight into the new VB.Net 2005 SerialPort control
Jon Williams also provided an updated BS2 file that conforms to the PBASIC 2.5 Style Guidelines --- Thanks Jon
Charlie
Post Edited (dy8coke) : 1/31/2006 6:54:03 PM GMT