Shop OBEX P1 Docs P2 Docs Learn Events
Computer to basic stamp using Visual Basic — Parallax Forums

Computer to basic stamp using Visual Basic

DanFaDanFa Posts: 3
edited 2006-01-31 17:09 in BASIC Stamp
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

·

Comments

  • Microman171Microman171 Posts: 111
    edited 2006-01-19 19:43
    If you figure this command out please tell me. Get te VB express edition and there is a serial port command in the toolbox. BTW the code is very much the same as VB6. I'm not entirely sure on how to hook up the button the that command.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    1 + 1 = Window

    /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
    0............................................0
    0............................................0
    0.(Microman171@hotmail.com)..0
    0............................................0
    0............................................0
    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
  • Charlie JohnsonCharlie Johnson Posts: 147
    edited 2006-01-19 19:46
    You might want to try

    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
  • Charlie JohnsonCharlie Johnson Posts: 147
    edited 2006-01-19 19:50
    Microman171,

    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 WilliamsJon Williams Posts: 6,491
    edited 2006-01-19 20:57
    If you're using the programming port you have to remember that any bytes you send to the Stamp will be echoed back by the hardware -- you have to filter these bytes from your input stream.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • Microman171Microman171 Posts: 111
    edited 2006-01-19 22:09
    dy8coke said...
    Microman171,

    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

    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
    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
  • Kevin WoodKevin Wood Posts: 1,266
    edited 2006-01-20 00:14
    Microman171,

    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.
  • DanFaDanFa Posts: 3
    edited 2006-01-20 01:24
    Thank you for the information! I learnd some things I didn't know.

    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.
  • Microman171Microman171 Posts: 111
    edited 2006-01-20 03:55
    Thanks everyone for all your help I can now use the serial port with the BS2 and VB.NET. DanFa: Instead of using debug learn to use the serout command for output and the Serin command for Input (From the VB script)

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    1 + 1 = Window

    /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
    0............................................0
    0............................................0
    0.(Microman171@hotmail.com)..0
    0............................................0
    0............................................0
    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
  • Microman171Microman171 Posts: 111
    edited 2006-01-20 04:23
    I've got it all sussed except for one thing... The settings for the serial port: I have it all there plus some extra

    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 wink.gif) in the projects forum.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    1 + 1 = Window

    /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
    0............................................0
    0............................................0
    0.(Microman171@hotmail.com)..0
    0............................................0
    0............................................0
    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
    1046 x 728 - 77K
  • Kevin WoodKevin Wood Posts: 1,266
    edited 2006-01-20 07:06
    Microman171,

    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.
  • Microman171Microman171 Posts: 111
    edited 2006-01-20 20:46
    I've changed all of the setting to work but now everytime I push read it crashes (VB.NET) this is the code I'm useing:
    Public Class Form1
    
        Dim A As Byte
    
        Private Sub Ready_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Ready.Click
            SerialPort1.Open()
            A = SerialPort1.ReadByte
            SerialPort1.Close()
            Label1.TextAlign = A.ToString
        End Sub
    End Class
    
    




    Is there something wrong in the code?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    1 + 1 = Window

    /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
    0............................................0
    0............................................0
    0.(Microman171@hotmail.com)..0
    0............................................0
    0............................................0
    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2006-01-20 20:57
    Crashes, or just gives an error? If the serial port is in use by the Stamp IDE (i.e., you have the Debug Window open) then your VB program won't be able to open that port with anything else. If you look in the Project section you'll find a little BS1-to-VB6 project I posted; it traps serial port errors like that -- you should be albe to adapt that code to .NET.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • DanFaDanFa Posts: 3
    edited 2006-01-20 23:42
    Thank you all so much! I was doing lots of things wrong. most of them you all pointed out, and then one I found. I was trying to send a word sized number with a byte size variable. So it looks like I will be doing some studing to learn how to send larger numbers. Thanks again!
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2006-01-20 23:59
    It's simple: break Words (I uses Longs in VB) into two bytes:

    · loByte = theLong \ 256
    · hiByte = theLong Mod 256

    The backwards division operator is for integer division.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • Kevin WoodKevin Wood Posts: 1,266
    edited 2006-01-21 02:05
    Here is some info from MSDN. See the Summary link at the botton of the page for a quick overview.

    msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcn7/html/vbcondatatypes.asp
  • Microman171Microman171 Posts: 111
    edited 2006-01-21 02:24
    Can I please have a link to the projects section I cant find it. Also the stamp is programmed and un-hooked to the program. I don't understand what's wrong.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    1 + 1 = Window

    /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
    0............................................0
    0............................................0
    0.(Microman171@hotmail.com)..0
    0............................................0
    0............................................0
    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
  • Kevin WoodKevin Wood Posts: 1,266
    edited 2006-01-21 08:42
    Okay, for those interested, here is something to work with.


    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 WilliamsJon Williams Posts: 6,491
    edited 2006-01-21 15:43
    VB.NET is probably·looking for a linefeed (10)·character, not a carriage return (13).· The IDE Debug window ads LF to and CR it sees so some mistakenly thing that a linefeed is being sent as well.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • Microman171Microman171 Posts: 111
    edited 2006-01-21 19:44
    Thanks everyone for there help on this. one last problem... When I debug it my form comes up so I push read. Then what happens is there is an error on
    MyMessage = SerialPort1.ReadLine
    

    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 WilliamsJon Williams Posts: 6,491
    edited 2006-01-21 20:18
    You really need top open the VB help file (assuming you installed it) -- try pressing [noparse][[/noparse]F1] when you get an error.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • Kevin WoodKevin Wood Posts: 1,266
    edited 2006-01-22 01:28
    Well, I guess Jon Williams wins the Bonus Round! The pop-up help blurb states that this method

    "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
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2006-01-22 04:55
    Do I get a cookie?

    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
  • Kaos KiddKaos Kidd Posts: 614
    edited 2006-01-23 20:00
    Jon... you are so close...
    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
    ·
  • Charlie JohnsonCharlie Johnson Posts: 147
    edited 2006-01-23 21:12
    Playing around with the serialport control, I found that the newline is set by default to Linefeed.. whitch is ASCII 10 in normal circumstances,
    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
  • Microman171Microman171 Posts: 111
    edited 2006-01-24 18:56
    pretty soon I will have my project up and running. Man have I learn't a lot of VB code recently. At the moment I'm working on the timer form control. I'm on a roll roll.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    1 + 1 = Window

    /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
    0............................................0
    0............................................0
    0.(Microman171@hotmail.com)..0
    0............................................0
    0............................................0
    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
  • Charlie JohnsonCharlie Johnson Posts: 147
    edited 2006-01-31 17:09
    Here is a working VB.Net 2005 example for use with the Nuts and Volts article by Jon Williams -- NV89· The olny problem I have with the code is that after you connect, and try to upload string data as your first action, it will occasionly hang.· This is a VB.Net issue that I am still trying to figure out.

    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
Sign In or Register to comment.