Shop OBEX P1 Docs P2 Docs Learn Events
serial communication to basic stamp — Parallax Forums

serial communication to basic stamp

gregory12345gregory12345 Posts: 10
edited 2009-05-30 14:58 in BASIC Stamp
Hi, im working on a project and am using BS2sx and servo motors and serial data from Visual basic 6 from my PC. My servo needs a value from 500 to 3000 to define its position. when i use this code:
Main:
SERIN 16,240,[noparse][[/noparse]inputValue]
multiplier = 10
inputValue = inputValue * multiplier
PULSOUT 1,inputValue
PAUSE 20
GOTO main

If i enter the number 50 in my VB(500 in the stamp after the multiplication), then the servo should go to 0 degrees, but it doesnt. It just moves about 10 degrees on every click.

But if i change the code on the stamp to LOOP on the PULSOUT, the servo does turn to the desired position, but it is only able to send the data once from the VB form, so it does nothing on the second click onwards. The code:


Main:
SERIN 16,240,[noparse][[/noparse]inputValue]
multiplier = 10
inputValue = inputValue * multiplier
DO
PULSOUT 1,inputValue
PAUSE 20
LOOP
GOTO main

Can anyone help tell me where my error is? I need my VB to keep sending different positions for the servo on every click on the command button.

Comments

  • roadrunner3groadrunner3g Posts: 36
    edited 2009-05-26 06:33
    instead of using the DO LOOP , use a FOR NEXT instead.

    VB var byte
    Main:
    SERIN 16,240,[noparse][[/noparse]inputValue]
    multiplier = 10
    inputValue = inputValue * multiplier
    for VB = 0 to 20·········· 'DO
    PULSOUT 1,inputValue
    PAUSE 20
    next························ ·'LOOP
    GOTO main
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2009-05-26 06:40
    What you're trying to do is going to be very difficult with a BASIC Stamp, since the Stamp can't buffer incoming characters in the background or send servo pulses in the background (unless you use a ServoPAL). I think the easiest thing would be for your VB program to send a continuous stream of data, in the form: :<digit><digit><digit>, changing the transmitted value when the command button is pressed. The Stamp code would look something like this:

    DO
      SERIN 16, 240, [noparse][[/noparse]WAIT(":"), DEC3 inputValue]
      PULSOUT 1, inputValue * 10
      PAUSE 15
    LOOP
    
    
    


    -Phil

    BTW, when you screw up and forget to enter a subject, you can always edit the post using the pencil icon in the upper righthand corner. You don't need to start a new thread. Now you can delete your other post, using the "X" icon. Please do that now, before someone else responds to it. Then it will be too late. -P.

    Post Edited (Phil Pilgrim (PhiPi)) : 5/26/2009 6:46:56 AM GMT
  • gregory12345gregory12345 Posts: 10
    edited 2009-05-29 04:43
    thanks roadrunner, i have successfully tried using for, next instead of do loop, and the servo works just fine (changes every time the command button is click). I have 2 questions. First, i dont get why there has to be a 0 to 20, can you explain this please?
    the second is that, my servo works from the value of 500 to 3000, but the maximum decimal value by serial port is 255, do you know of any way to in put 500 until 3000 in VB and still get the decimal value in the stamp without using a "multiplier" like mine. Thank you very much.
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2009-05-29 05:34
    Hi is the data transmitted as byte values or ASCII character codes ( string ). If it is byte values for numbers between 500 and 3000 it would take a word value (two bytes) , one byte the HighByte and one byte the LowByte of the word value. Phil Pilgrims suggestion of the WAIT instruction really helps synchronize the incoming data and with the inclusion of a timeout you could be sure of updating the servo at 20mSec intervals

    eg (at 4800 baud)

    inputvalue Var Word

    Do

    SERIN 16,16884,20,time_out[noparse][[/noparse]WAIT([noparse]:)[/noparse],inputvalue.HighByte,inputvalue.LowByte]

    time_out:

    PULSOUT·1,·inputValue

    Loop

    If your transmitting ASCII values you would handle it more in line with Phil's code example

    Jeff T.
  • gregory12345gregory12345 Posts: 10
    edited 2009-05-29 07:17
    thanks for the reply jeff. I am transmitting ASCII, i tried using

    SERIN 16, 240, [noparse][[/noparse]DEC3 inputValue], but the same appeared on my VB that i cant send more than 255, maybe there is something wrong with my VB code ey? my VB code is :

    inputValue = cmdInput.Text
    MSComm1.Output = Chr$(inputValue)

    how can i change the setting on my VB to send more than 255?

    thanks
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2009-05-29 15:26
    Hi, I am not familiar with VB6 as it is so long since I last used it so all I say may not be accurate. The nice thing about the Stamp is that it can accept a string of ASCII character codes and convert them in to a single number using formatters . The formatter I am thinking of is the DEC formatter.

    So VB6 would look something like

    DIM inputValue As String
    inputValue = cmdInput.Text
    MSComm1.Output = inputValue

    If cmdInput.Text was "345" then VB6 would transmit the 3 ASCII character codes as 3 seperate bytes "51" "52" and "53" . The following command at the Stamp would place those characters into a single word value I have named indata

    SERIN 16,16884,[noparse][[/noparse]DEC3 indata]
    The better way to do it is to use a header for synchronizing and a terminating vbCR as follows

    DIM inputValue As String
    inputValue = ":" & cmdInput.Text & vbCR
    MSComm1.Output = inputValue

    at the Stamp

    SERIN 16,16884,20,time_out,[noparse][[/noparse]Wait(":"),DEC indata]

    I recommend the baud rate of 4800 inverted (16884)

    Another problem you will face is that everything transmitted to the Stamp is echoed back to the PC's buffer and mixed in with the next data set so before you transmit from the PC you need to do a clear buffer whatever that command might be

    check this link out for a few pointers http://forums.parallax.com/showthread.php?p=671804

    Jeff T.
  • gregory12345gregory12345 Posts: 10
    edited 2009-05-30 06:20
    Hi Jeff!

    I tried changing the code using the DEC formatter and it works perfectly! But i did not understand what "time_out" and the 20 before "time_out" was for, so i did not use it. But it still works. Now my servo move to both maximum positions from 500 to 3000.
    Thanks a milion
  • gregory12345gregory12345 Posts: 10
    edited 2009-05-30 06:22
    sorry because im a new member,can you tell me where i can mark this thread resolved?
  • FranklinFranklin Posts: 4,747
    edited 2009-05-30 14:58
    You can change the title but that is all you can do to mark resolved. You other question about the timedout and the 20 The basic stamp can only do one thing at a time so if it is waiting for serial input that is all it can do. If you tell it to wait for something that never happens it is happy to set there and wait...FOREVER (or until you reset it) the 20 and timeout tells it to wait 20 ms and if nothing is received go to a point in the program called timeout (or anything you wish to call it)

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
Sign In or Register to comment.