Shop OBEX P1 Docs P2 Docs Learn Events
stuck — Parallax Forums

stuck

MTSkullMTSkull Posts: 4
edited 2008-03-23 19:27 in BASIC Stamp
I am creating a C# application that will send a short command string to a BS2, which will then control motors and senors to do stuff. I am stuck on how to get the stamp to respond to the data being sent. I am sending 5 hex bytes starting with hex00 followed by 4 more hex bytes(1 to 9) that will eventually tell the stamp what to do. Also in the print it routine, the calling application can read the debug print out, but would SerOut be more appropriate there?

Thanks

' {$STAMP BS2}
' {$PBASIC 2.5}


'Serial Comms COmmands
SI              PIN     0               ' serial input
FC              PIN     1               ' flow control pin

#SELECT $STAMP
  #CASE BS2, BS2E, BS2PE
    T1200       CON     813
    T2400       CON     396
    T9600       CON     84
    T19K2       CON     32
    T38K4       CON     6
#ENDSELECT

Inverted        CON     $4000
Open            CON     $8000
Baud            CON     T38K4 + Inverted

'Serial Imput Variables
ExternInput     VAR     Byte
StartCol        VAR     Byte
StartRow        VAR     Byte
StopCol         VAR     Byte
StopRow         VAR     Byte

'Variables for Print function
idx             VAR     Word            ' current location number
phrase          VAR     Nib             ' current phrase number
char            VAR     Byte            ' character to print

'Response Codes
Pickup          DATA    0,"Pickup",CR
DropOff         DATA    0,"Drop Off",CR
MovingX         DATA    0,"Moving X",CR
MovingY         DATA    0,"Moving Y",CR
MovingZUp       DATA    0,"Moving Z Up",CR
MovingZDn       DATA    0,"Moving Z Down",CR
Completed       DATA    0,"Completed",CR
BigErr          DATA    0,"Big Error",CR
LittleErr       DATA    0,"Little Error",CR




Main:

  ExternInput = 255

  DO
    SERIN SI\FC, Baud, [noparse][[/noparse]ExternInput]         ' receive one byte
'    DEBUG letter                        ' display on screen

    IF (ExternInput = 0)THEN
      GOSUB Is_True
      GOSUB Operate_Gantry
    ENDIF


    PAUSE 1000                          ' wait one second
  LOOP                                  ' repeat forever
  END

Get_Move_Command:
  SERIN SI\FC, Baud, [noparse][[/noparse]StartCol]
  SERIN SI\FC, Baud, [noparse][[/noparse]StartRow]
  SERIN SI\FC, Baud, [noparse][[/noparse]StopCol]
  SERIN SI\FC, Baud, [noparse][[/noparse]StopRow]
  RETURN

Operate_Gantry:
'just send this data back for now, later do stuff here
  FOR phrase = 1 TO 11                   ' Print blocks one by one
    LOOKUP (phrase - 1),
    [noparse][[/noparse]MovingX, MovingY, MovingZDn, Pickup, MovingZUp, MovingX ,MovingY, MovingZDn, DropOff, MovingZUp, Completed], idx
    GOSUB Print_It
    PAUSE 1000                          ' Pause for 5 seconds
  NEXT

  RETURN

Print_It:
  DO
    READ idx, char                      ' Get next character
    idx = idx + 1                       ' Point to next location
    IF (char = 0) THEN EXIT             ' If 0, we're done with block
    DEBUG char                          ' Otherwise, transmit it
  LOOP
  RETURN                                ' Return to the main routine



Comments

  • PJAllenPJAllen Banned Posts: 5,065
    edited 2008-03-22 16:02
    ·Use the WAIT Qualifier:
    Get_Move_Command:
      SERIN [i]pin[/i], [i]baudmode[/i],[noparse][[/noparse]WAIT (00), StartCol, StartRow, StopCol, StopRow]
      RETURN
    

    It'll wait·to receive a·$00, which prompts the stamp to take in the four following bytes as command data.

    You could add a timeout for the WAIT, too.
  • MTSkullMTSkull Posts: 4
    edited 2008-03-22 17:57
    I think I see what I might be doing wrong. I wanted to use the serial port on the "board of education" to communicate with the stamp. It looks like those connect to SIn and SOut. So I guess my question is, can I talk to the chip using the on board serial port, or do I need to setup a p0, p1 3wire serial communications port via the input output pins?

    I found this snipit in the PBasic Guide book, it echoes back what I send to it but then it should start transmitting all of the string data, which never seems to happen.

    
    SERIN 16, $4054, [noparse][[/noparse]DEC1 lines]
    
    'i modified to...
    '4k = inverted, 84 = 9600 baud
    SERIN 16, $4084,[noparse][[/noparse]WAIT (00), StartCol, StartRow, StopCol, StopRow] 
    
    
    



    Thanks
    Brian
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2008-03-22 18:04
    You can use the BoE serial port, but I'd make a cable that only has TX, RX, and GND; none of the hand-shaking connections.
    An extension cable:
     
    [b]M    F[/b]  
    [u][/u] 
    2 --- 2
    3 --- 3
    5 --- 5
    
    

    PS -- BS2 baudmode for 9600, inverted, 8-N-1·is: 16468 ($4054)

    PPS -- I think that it should be "true", too, and that's: 84 ($54).··Is your PC/C-program sending "inverted"?

    PPPS -- PBASIC Help informs that at speeds > 2400 baud that the Stamp might not cope with Qualifiers, so you might have to change that, too.· I have a GPS project and its data comes in at 4800bps and I use Qualifiers (WAIT and STRs) with it just fine.· It waits for the appropriate sentence/header and then starts accepting data, much the way you're trying to.

    Post Edited (PJ Allen) : 3/22/2008 6:45:36 PM GMT
  • MTSkullMTSkull Posts: 4
    edited 2008-03-23 18:24
    Thanks for the great help, using the above I got it working. [noparse][[/noparse]sarcasm] I had also inadvertently introduced another bug, since i figured learning this would not be hard enough on it's own.[noparse][[/noparse]/sarcasm] rolleyes.gif
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2008-03-23 18:27
    So, tell·us how you turned it around.
  • MTSkullMTSkull Posts: 4
    edited 2008-03-23 18:46
    Apparently the original bit of code you gave me was working fine all along. If you notice all of my Data strings, I shuffled them around to start with a zero. In the "Print_It" routine it specifically says in the comments to read characters until I hit a zero then stop. So if I had not been dumb and shifted the zero to the front I would not have had a problem. It wasn't until I went to the code snippet I copied that bit out of and compared it to my modifications that I caught it. Incidentally, I got the Serin to work via this...
    SERIN 16, $54,[noparse][[/noparse]WAIT (00), StartCol, StartRow, StopCol, StopRow]
    


    Which brings to mind a question, is there a way to step through the code line by line while it is executing?
  • Mike GreenMike Green Posts: 23,101
    edited 2008-03-23 19:27
    There is no built-in way to step through code. In fact, there's really no built-in debugging support in the usual sense. The DEBUG / DEBUGIN statements are just modified SERIN / SEROUT statements. Since they use handshaking, you can pause the output, but that's all. It would be simple to have a subroutine that pauses if a switch is set and continues when a button is pressed then released. You could put GOSUBs between statements of interest. It would add a bit of execution overhead time, but could be useful sometimes.
Sign In or Register to comment.