Shop OBEX P1 Docs P2 Docs Learn Events
reading SERIAL port with SERIN — Parallax Forums

reading SERIAL port with SERIN

John MacJohn Mac Posts: 5
edited 2007-08-29 13:24 in BASIC Stamp
I'm just getting started with PBASIC, but prtty good at Visual Basic.· I wrote a PBASIC program to scan a servo back and forth to pan a webcam back and forth.· It also has the ability to step left, step right and center.· On the scan command it should scan back and forth until it sees a new command to do something else.· The command are simply 1(step left), 2(step right), 3(center), 4(scan).· It uses SERIN to get the command from the SERIAL prot written by a VB program with a GUI interface that shows the webcam and has 4 control buttons to send the commands to the SERIAL port.

So all this works fine, except the PBASIC code for scan does a SERIN read after each scan to see if it should continue scanning or do something else.· After a left/right scan I do another SERIN to see if a new command is present.· Problem is if I hit Center in teh VB program for instance during the scan, when the scan is done the SERIN command just waits for a command.· I put in a timeout in SERIN and if i send a VB command during the wait period the PBASIC accepts the command properly.

I am assuming this means that SERIN must see an "interupt" from the SERIAL port to read.· Sending a command then reading with SERIN doesn't work.

Is it possible to read a "pre loaded" command on the SERIAL port with SERIN?

This may be too confusing, so ask.

Thanks
John

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2007-08-29 04:44
    The problem you're running into is the single threadedness of the Stamp processor which means that it can only do one thing at a time. In particular, if it's not sitting at a SERIN statement waiting for something to get sent ... it doesn't see anything ... anything sent at any other time from the PC is just ignored completely. There is no way around this except that you can partially make up for this shortcoming by having the Stamp ask the PC for a command when the Stamp is ready. Specifically, the Stamp always sends a question mark ("?") to the PC when it's ready for a new command. After the SEROUT, you put the SERIN. Whenever the VB program receives a question mark, it responds by sending something. You can use a command of zero to indicate that nothing is to be done. The Stamp can wait a short period of time (like PAUSE 100) or not wait at all and just send the question mark again if it's a zero command.
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2007-08-29 04:53
    Hi John, the Stamp does not have a serial buffer so SERIN has to be ready and waiting when data is transmitted.

    This can be take valuable program time·which can be optimized. One way would be to transmit a "ready" command from the Stamp just before the SERIN.

    example

    SEROUT tx,baud,[noparse][[/noparse]"RDY",CR]

    SERIN rx,baud,100,timeout,[noparse][[/noparse]data]

    the RDY would be captured with the OnComm or DataReceived event depending on your version. Timeout gives VB time to respond, if no response the program continues.

    Jeff T.

    EDIT: which agrees with Mike's posting
  • John MacJohn Mac Posts: 5
    edited 2007-08-29 05:02
    Excellent suggestion
  • allanlane5allanlane5 Posts: 3,815
    edited 2007-08-29 13:24
    Another possibility is to spend an additional $7 or so to get a recieve buffer. This is an excellent link, which also gets you a 'hard' real time tick.

    http://www.rhombus-tek.com/co-processors.html

    The "Simple Multi-Tasking" processor provides a recieve-only serial buffer of 11 bytes, which works without needing a MAX-232 or anything.

    Post Edited (allanlane5) : 8/29/2007 1:31:10 PM GMT
Sign In or Register to comment.