Shop OBEX P1 Docs P2 Docs Learn Events
BASIC STAMP II + USB Cx — Parallax Forums

BASIC STAMP II + USB Cx

RfRf Posts: 11
edited 2006-01-12 01:47 in BASIC Stamp
Hi guys,

I am a newbie with the Stamp so I need your help.
Using the Stampworks development board, I am trying to communicate with my Stamp to turn a led on/off following the suggestions from some of your postings of going from simple to complex.

The board came with a USB cable and it is my understanding that this is a serial kind of commnication as well. In my case, I am using COM8. When I send the following code to the EEPROM using the Parallax Basic Stamp Editor (COM8), it works like a champ but when I use VB .NET it doesn't. The .NET code is very simple and it uses a class that allows to send and read data via COM ports. If needed, I will post the VB code in future postings. Here is the code from the Parallax Editor:

'···· {$STAMP BS2}
'···· {$PBASIC 2.5}
·
command VAR Byte
ledOut4 PIN 4
x1····· VAR·· Nib
·
·
One:
FOR x1 = 1 TO 5
HIGH ledOut4
PAUSE 500
LOW ledOut4
PAUSE 500
NEXT
GOTO Main
·
Main:
·
command = 0
SERIN 16, 16780, [noparse][[/noparse]command]
PAUSE 500
BRANCH command, [noparse][[/noparse]One]
PAUSE 500
GOTO Main


Thank you in advance! smile.gif

Roger

Comments

  • allanlane5allanlane5 Posts: 3,815
    edited 2006-01-09 22:12
    If using COM8 and the BS2 IDE it works, then you've validated your hardware.

    The only thing wrong now must be your software... The VB .NET may help.

    Oh, and note when you open a serial port in Windows (using Hyperterm, for example) it has a nasty tendency to keep DTR active. DTR is the reset signal for the BS2. Now, the BOE board has a couple of capacitors to decouple the reset signal -- but when Hyperterm comes up, it still resets it once.

    I think you need to 'attach' the VB code to a message and let us see it.
  • RfRf Posts: 11
    edited 2006-01-09 22:37
    Hi Allan,

    Thank you in advance. Here is my VB .NET 2003 code. I have a form with three buttons: open port, Tx, and Read; and two textboxes, one to write and code to send and the other one to receive (read).

    Public Class Form1
    Inherits System.Windows.Forms.Form
    Dim setrs232 As New rs232


    #Region " Windows Form Designer generated code "

    Public Sub New()
    MyBase.New()

    'This call is required by the Windows Form Designer.
    InitializeComponent()

    'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    If disposing Then
    If Not (components Is Nothing) Then
    components.Dispose()
    End If
    End If
    MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.
    'Do not modify it using the code editor.
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents button2 As System.Windows.Forms.Button
    Friend WithEvents Button3 As System.Windows.Forms.Button
    Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
    Friend WithEvents Button4 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    Me.Button1 = New System.Windows.Forms.Button
    Me.TextBox1 = New System.Windows.Forms.TextBox
    Me.button2 = New System.Windows.Forms.Button
    Me.Button3 = New System.Windows.Forms.Button
    Me.TextBox2 = New System.Windows.Forms.TextBox
    Me.Button4 = New System.Windows.Forms.Button
    Me.SuspendLayout()
    '
    'Button1
    '
    Me.Button1.Location = New System.Drawing.Point(24, 24)
    Me.Button1.Name = "Button1"
    Me.Button1.Size = New System.Drawing.Size(104, 40)
    Me.Button1.TabIndex = 0
    Me.Button1.Text = "Open Port"
    '
    'TextBox1
    '
    Me.TextBox1.Location = New System.Drawing.Point(24, 80)
    Me.TextBox1.Multiline = True
    Me.TextBox1.Name = "TextBox1"
    Me.TextBox1.ScrollBars = System.Windows.Forms.ScrollBars.Both
    Me.TextBox1.Size = New System.Drawing.Size(696, 152)
    Me.TextBox1.TabIndex = 1
    Me.TextBox1.Text = ""
    '
    'button2
    '
    Me.button2.Location = New System.Drawing.Point(624, 24)
    Me.button2.Name = "button2"
    Me.button2.Size = New System.Drawing.Size(104, 40)
    Me.button2.TabIndex = 2
    Me.button2.Text = "Tx"
    '
    'Button3
    '
    Me.Button3.Location = New System.Drawing.Point(316, 24)
    Me.Button3.Name = "Button3"
    Me.Button3.Size = New System.Drawing.Size(104, 40)
    Me.Button3.TabIndex = 3
    Me.Button3.Text = "Close Port"
    '
    'TextBox2
    '
    Me.TextBox2.Location = New System.Drawing.Point(24, 248)
    Me.TextBox2.Multiline = True
    Me.TextBox2.Name = "TextBox2"
    Me.TextBox2.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
    Me.TextBox2.Size = New System.Drawing.Size(696, 144)
    Me.TextBox2.TabIndex = 4
    Me.TextBox2.Text = ""
    '
    'Button4
    '
    Me.Button4.Location = New System.Drawing.Point(608, 424)
    Me.Button4.Name = "Button4"
    Me.Button4.Size = New System.Drawing.Size(104, 40)
    Me.Button4.TabIndex = 5
    Me.Button4.Text = "Read"
    '
    'Form1
    '
    Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    Me.ClientSize = New System.Drawing.Size(736, 476)
    Me.Controls.Add(Me.Button4)
    Me.Controls.Add(Me.TextBox2)
    Me.Controls.Add(Me.Button3)
    Me.Controls.Add(Me.button2)
    Me.Controls.Add(Me.TextBox1)
    Me.Controls.Add(Me.Button1)
    Me.Name = "Form1"
    Me.Text = "Form1"
    Me.ResumeLayout(False)

    End Sub

    #End Region

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


    setrs232.Open()


    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    With setrs232
    .Port = 8
    .BaudRate = 2400
    .DataBit = 8
    .StopBit = Rs232.DataStopBit.StopBit_1
    .Parity = Rs232.DataParity.Parity_None
    .Timeout = 1500


    End With

    End Sub




    Private Sub button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button2.Click


    setrs232.Write(TextBox1.Text)

    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    setrs232.Close()

    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
    setrs232.Read(200)
    TextBox2.Text = setrs232.InputStreamString

    End Sub
    End Class
  • allanlane5allanlane5 Posts: 3,815
    edited 2006-01-09 23:18
    Hmm.
    I don't know where you got that SERIN baud selector of "16780". For a BS2, 9600 baud is selected with 16468.

    And in your VB code, you're selecting 2400 baud. No wonder they can't talk to each other. For a BS2, 2400 baud would be 16780 -- oh, look, there it is!

    Okay, so that's right. Hmm again. You might want to set DTREnable to FALSE (if there IS such a property).

    Now, you're aware the BS2 is only 'listening' when its actually IN the SERIN command, right? Also, that you've only told the BS2 to recieve ONE BYTE. You might want to use the "DEC" modifier in the BS2. That way, when you send "0 <cr>", it will convert it to a zero byte, and "1 <cr>" will convert to a '1' value so your Branch will work properly.

    Oh, and you probably want:

    Branch Command, [noparse][[/noparse]Zero, One]
    Zero:
    ' Etc....

    Because the first element IS the 'Zero' select one, the second element is the '1' select one.
  • RfRf Posts: 11
    edited 2006-01-09 23:32
    I will try your suggestions Allan, using DEC.
    I am kind of getting sick of fighting the USB connection and if that doesn't work, I will try to go through the DB9 and make the proper connections on the development board. I can't figure out why is not doing the trick. [noparse]:([/noparse]

    BTW there is a propery for DTR so I will add DTR = FALSE to my code and let's see if I have better luck.

    Thanks again!
  • RfRf Posts: 11
    edited 2006-01-09 23:39
    Just in case somebody is reading this thread, I just read on another place that the baud rate to program the BS2 is fixed to 9600. That is another thing to try when I get home from work. :=)
  • RfRf Posts: 11
    edited 2006-01-10 15:51
    I still having problems, so I will appreciate if one of you who have already become experts with the BS2 can help me out with my simple task of turning a led on/off using the USB cable coming with my kit communicating from .NET, C++, or C# (PCTOSTAMP)
    After learning the trick, I think I can take it from there.
    Is funny because SEROUT works great for me when I open hyperterminal but SERIN doesn't send anything from the PC to the BS2. I checked the speed (9600) and that looks correct. I tried different combinations with DTR and RTS but nothing helped.
    If I download the program for the led to the EEPROM and I just open the port from .NET the led starts flashing.
    Is terrible being a newbie.....! :=(

    Thank you again in advance!
  • allanlane5allanlane5 Posts: 3,815
    edited 2006-01-10 22:04
    Hmm. You are aware that you use the BS2 IDE to program the device, right? And once programmed, the program resides in EEPROM on the BS2, and runs at every power-up, so you don't have to 'download' the program again -- in fact, the BS2 wouldn't know what to do with the actual text of a program if you sent it down the serial port.

    If you used a 'SERIN' on the BS2, it should 'pend' forever on that SERIN until SOME data comes in to the BS2.
  • RfRf Posts: 11
    edited 2006-01-10 22:20
    Oh, no....I know that I am sending down the program to the EEPROM with the Editor. As a mater of fact , I always "Clear" the EEPROM with my CLEAR.BS2 program of course. I think that there are some questions about timing that I need to address. Jon posted a routine that helps escaping from the SERIN command when there is not data or the data didn't come on time to the processor. Under those circunstances, I think that the processor would hang. I need to read more about the topic.

    What bothers me is that the Editor works really well sending the program down to the BS2. lol

    Thanks a lot for your posting allan.
  • Microman171Microman171 Posts: 111
    edited 2006-01-11 04:35
    Jee I wish I got that far confused.gif I have no idea on how to attach the BSII to the computer. I have the HwkBrd and I don't want to rewire that. I program through the serial cable provided. what do i have to do?? What would be nice is if somebody could supply code for the new VB express edition and the PBASIC code smilewinkgrin.gif

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

    /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
    0............................................0
    0............................................0
    0.(Microman171@hotmail.com)..0
    0............................................0
    0............................................0
    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
  • Tom WalkerTom Walker Posts: 509
    edited 2006-01-11 14:09
    Just as a general note, and not to accuse, imply, point fingers, etc...
    The Stamp is programmed by the Parallax provided editor (also called the IDE...and yes, I'm glossing over any 3rd-party stuff using the Parallax libraries...). If you can program your Stamp using the IDE, then the cable and setup you have are good and any difficulty in communicating with your Stamp program is most likely an error in your code, either on the Stamp side (PBASIC) or on the PC side (VB, C, Delphi, .net, etc...). Remember that many languages on the PC side that have facility to communicate through the serial port raise the DTR line by default. This is a signal to the Stamp to reset itself (and STAY reset as long as the DTR is held high). The documentation of your PC programming language of choice should indicate how you can handle the DTR signal.
    If you haven't programmed the Stamp or do not understand how to write a program to DO something with serial data, then I would recommend some more study...the free "What's a Microcontroller?" course is high on my list of recommendations...

    Sorry if this sounded surly...

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Truly Understand the Fundamentals and the Path will be so much easier...

    Post Edited (Tom Walker) : 1/11/2006 2:45:20 PM GMT
  • Charlie JohnsonCharlie Johnson Posts: 147
    edited 2006-01-11 14:22
    I believe that VB.Net 2003 can not enumerate the USB ports, try your VB code on a standard COM port if you can.

    Charlie
  • Tom WalkerTom Walker Posts: 509
    edited 2006-01-11 14:34
    If I understand correctly, the driver for the FTDI chip makes any "enumeration" irrelevant. As far as the PC is concerned, it is just "talking" to a COM-type serial port. All identification, enumeration, etc... is handled by the driver.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Truly Understand the Fundamentals and the Path will be so much easier...
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2006-01-11 15:40
    Tom,

    ·· You are correct, once the VCP Driver is installed all applications accessing that port should only "see" a COM Port.· Now on your other statement regarding DTR, this only affects Stamp Modules not protected by the 2 .1uF capacitors we install on our development boards.· These capacitors prevent the ATN line from being held HIGH by DTR, although it doesn't rule out DTR as an issue for programming, necessarily.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • Charlie JohnsonCharlie Johnson Posts: 147
    edited 2006-01-11 15:47
    I was not at my PC when I made the post, I just tested the SerialPort.GetPortNames method with VB.Net express 2005 and it does in-fact enumerate the USB ports when a FTDI driver is running. However I do not know if the VB.NET 2003 3rd party serial port control does it the same way. VB.Net 2003 did not come with a serial control. I will try using the old mscomm control under VB.Net 2003 and see what happens.

    Charlie
  • RfRf Posts: 11
    edited 2006-01-11 21:15
    Hi guys,

    Thanks for this exchange of information. I am using a 2003 and the class I am using has the DTR property which I set as follows:

    With setrs232

    .Port = 8
    .BaudRate = 2400
    .DataBit = 8
    .StopBit = Rs232.DataStopBit.StopBit_1
    .Parity = Rs232.DataParity.Parity_None
    .Timeout = 1500
    .DTR=FALSE

    End With

    I know I have the correct COM port (which BTW I can change via OS to any port I want) because if I program the BS2 and open my port from VB .NET 2003, my leds start blinking. Besides, all my programs run using the Parallax Editor (cheers to parallax!).

    My major concern using SERIN is about timing since I know that the processor won't be waiting forever to receive my code. Any thoughts on that?

    Thanks a lot!

    Roger
  • allanlane5allanlane5 Posts: 3,815
    edited 2006-01-11 21:29
    1. You'll need to 'resend' data from VB to the BS2, if the BS2 'drops it on the floor' because it wasn't waiting for it.

    2. EVERYTHING sent to the BS2 over port 16 gets echoed (by the BS2 hardware) back to the sender. Be aware of this, and toss your echo before you process the BS2 reply.

    3. You can implement a simple 'ack' 'nack' protocol with the BS2. Start with a sync character (like an $01), send your data, then send a 'end' character (like an $02). The BS2 should send back a $03 when it gets a 'full' message, and send nothing or an $04 when it gets a 'garbled' message.
  • RfRf Posts: 11
    edited 2006-01-12 01:47
    rolleyes.gif

    Thanks Allan! I have to read more about hand-shaking. I don't have a clue how to implement the ack-nack routines.

    ·
Sign In or Register to comment.