Shop OBEX P1 Docs P2 Docs Learn Events
Stamp intermittently responding to serial input — Parallax Forums

Stamp intermittently responding to serial input

maldridgemaldridge Posts: 37
edited 2011-08-11 15:20 in BASIC Stamp
I have written the following program to parse and act on serial data recieved from a program running on a PC. The data arrived in the following format.
~! 00 00 00 00 00 00 00 00

The "~!" is a serial header that the stamp should wait on until it recieved before continuing program exicution. Each set of zeros represents the intensity of one channel (program controlls lights).

the problem comes in that the stamp only responds intermittently. e.g. the outputs may switch on, but take several cycles to turn off. Since the serial data is updated every 100ms, I would like the stamp to react that fast. By my calculations, the stamps clock speed should give it well over 84ms room after processing the data in the serial string before it needs to be ready to process again.

I think that some of my trouble comes in not fully understanding PBASIC arrays. Are they 0 indexed? Or does the index initialize randomly.

Here is the program:
' {$STAMP BS2}
' {$PBASIC 2.5}

'Program: Lighting Firmware.bs2
'Author:  Michael Aldridge
'Version: 1.0.5
'Purpose: To parse an incomming serial string from Vixen
'         and relay the instructions to an SSR grid.


restart:
'--------------------Initialization--------------------------
dat VAR Byte(8)
link CON 16
baud CON 84
index VAR Nib
'-----------------------Main Loop----------------------------

DO
SERIN link, baud,500,timeout, [WAIT("~!"),STR dat\8]
FOR index = 0 TO 8
IF dat(index)>0 THEN HIGH index
IF dat(index)=0 THEN LOW index
NEXT
LOOP

'-------------------Timeout Condition------------------------

timeout:
FOR index = 0 TO 7
LOW index
NEXT
GOTO restart

For those who know the program, I am using Vixen 2.1.1.0 with the generic serial plugin and a text header of "~!" (without quotes).

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-07-15 12:53
    Your datastream example shows ASCII zeroes. Are those really ASCII, or does each pair of zeroes represent one byte of binary-coded data? (Your program assumes the latter.) PBASIC arrays are zero-based. But your loop runs from 0 to 8, which covers nine elements. I'm sure you meant 0 to 7 instead.

    -Phil
  • maldridgemaldridge Posts: 37
    edited 2011-07-15 13:09
    yes, I did mean 0-7; I did the 0-8 just in case.

    The ASCII zeros in my example are what my serial terminal captured when I sent sample data from the program to the terminal instead of to the controller. I assumed that the terminal was translating binary data into a human readable format. I actually based that off of information I got from the people who wrote the software.

    Unfortunately, changing the 8 to a 7 still has not helped the operation of the program. If anyone would like to check the program operation for themselves and confirm my observations, the program can be found here. It's a live program that just runs out of the folder you unzip it into.

    At the moment, I am testing just with LED's connected to P0-P2. I assume that if it works on the first three lines, it will work on the other 5.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-07-15 14:10
    You could save time in your program by replacing the IFs with:
    OUT0(index) = dat(index) MAX 1
    

    I'm not entirely sure the MAX 1 is necessary. I put it there assuming the LSB of the argument -- not its non-zeroness -- is assigned to the pin.

    You will need to initialize DIRL at the top of your program to set the affected pins to outputs first.

    -Phil
  • maldridgemaldridge Posts: 37
    edited 2011-07-15 14:24
    I replaced the if's with the statement you provided and no I can't get any output on any pins. I was sure to add this at the top of the program:
    DIRS = %1111111100000000
    

    Perhaps my own stupidity has messed up something here. Any thoughts?
  • stamptrolstamptrol Posts: 1,731
    edited 2011-07-15 14:45
    The Stamp can only receive at 9600 baud under the best of circumstances. That usually means no extra work (ie loading an array or doing STR conversions).

    Can you drop to 4800 baud and do a test? Otherwise, your program looks the same as what I use. I don't think you'll actually process at the 100 mS rate, but the WAIT modifier will allow the Stamp to catch the next available string when its ready.

    If you have to stay at 9600, a BS2sx will get you there easily.

    Cheers,
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-07-15 15:19
    It should be
    DIRS = %0000000011111111
    

    The LSB corresponds to pin 0, not the MSB.

    -Phil
  • maldridgemaldridge Posts: 37
    edited 2011-07-15 15:28
    From what I understand, the lower transmit rate won't allow me to keep the 100ms event spacing. Are my calculations correct? Otherwise, I will happily drop the transmitting rate to a lower speed.
  • $WMc%$WMc% Posts: 1,884
    edited 2011-07-15 18:34
    Drop down to 9600 Baud and work your way up.
    '
    The BS2's don't have an input buffer.So it takes some cool code to make it run at higher Baud Rates.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-07-15 19:51
    9600 baud should work fine with the SERIN statement you're using, since you're not doing any data conversion. But you do need to change that DIRS statement.

    -Phil
  • maldridgemaldridge Posts: 37
    edited 2011-08-10 17:15
    I had to take some time off of this project to get some stuff squared away for high school in the fall (robotics has some crazy entrance requirements!) but I have now updated the code to reflect all the changes stated here.

    Unfortunately, it still only operates intermittently. Also, I'm not sure why, but for some reason the channels are all being shifted up one pin.

    Also, because I can't find any complete/easy to comprehend resource for the serin baud constant, can anyone tell me what it should be for 4800, 8, n, 1
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2011-08-10 20:06
    Also, because I can't find any complete/easy to comprehend resource for the serin baud constant, can anyone tell me what it should be for 4800, 8, n, 1

    "???"

    PBASIC Help presents that - in tables - in the SERIN entry.
    How could it be any plainer?

    Think different.
  • maldridgemaldridge Posts: 37
    edited 2011-08-11 06:20
    PBASIC Help?

    I'm on a mac and I couldn't get the help file to even open.
  • Mike GreenMike Green Posts: 23,101
    edited 2011-08-11 07:18
    The same information is in the Basic Stamp Syntax and Reference Manual which you can download here. If you're using MacBS2, I believe you have to download this Manual for use as a help file. If you upgrade to Lion, you won't be able to use MacBS2 since that requires Rosetta which is absent from Lion.
  • maldridgemaldridge Posts: 37
    edited 2011-08-11 09:29
    yeah, that's frustrating in Lion, but there are workarounds. I got the help file, thanks for the tip. I hadn't realized just how far out of date my copy of MacBS2 was!
  • john_sjohn_s Posts: 369
    edited 2011-08-11 15:20
    I'd say drop the ~ at WAIT for ! instead. That's a bit crude but always worked for me when I did modems with BS2.
    Start at the lowest bps (say 2400) and work up to higher rates from there...
Sign In or Register to comment.