Shop OBEX P1 Docs P2 Docs Learn Events
SERIN question — Parallax Forums

SERIN question

WeedogtWeedogt Posts: 6
edited 2006-09-18 08:35 in BASIC Stamp
I have a question that someone probably has run into before:

I'm using SERIN to send commands to a BS2sx from a PC. I send a single character "b" to start execution and then I would like to send a single character· "c" to stop.

Starting is easy, I just do like this:
'Wait for command to start
command:
SERIN 16, 16624, [noparse][[/noparse]c]
IF c<>98 THEN command   'Invalid command
'Execution starts..

To stop I have tried this (placed in a loop that is repeated once every second):

'Look for stop command
SERIN 16, 16624, 300, jump, [noparse][[/noparse]c]  'Look for command, with timeout
IF c=99 THEN command   'Stop command received, stop execution
jump:
'Continue execution...

It works but the stop command must come exactly when·the BS2sx·looks for it. The PC must send it over and over again for one second to make sure that·execution stops (goes to "command".

Can I make this smarter? That is, the PC can send the "c" only once and still be sure to stop execution?

·

Comments

  • Kevin WoodKevin Wood Posts: 1,266
    edited 2006-09-18 01:36
    Here a couple of example programs that you can experiment with.

    The first receives an input, performs a task 1 time, and goes back to looking for another input.

    The second is a modification of the first. It continues looping a task until it receives a stop command.

    I'm not a PBasic expert, so these probably aren't the most efficient algorithms. If anybody has improvements, please post them.
  • WeedogtWeedogt Posts: 6
    edited 2006-09-18 04:20
    Thanks Kevin,
    The "serin_sample2.bs2" program has a structure similar to my program. But (as far as I can tell) it has the same problem as mine = the stop command must come within a short period of time.

    Your code:
      SERIN INPUT_PIN, BAUDMODE, TIMEOUT2, WhatNext, [noparse][[/noparse]Command] ' TLabel is WhatsNext
      IF Command = "z" THEN Main
      RETURN
    

    BStamp has limitations; First of all it has no buffer for serial indata. Secondly it has not real interrupt. In all, it's not able to do what I want (react directly to an asyncronous serial command).

    Or am I wrong? Perhaps POLLIN could be used in some clever way?

    I would, of course, be possible to add a UART with buffer (like MAX3100) but I'd rather not do that as I try to keep it simple.
    ·
  • Mike GreenMike Green Posts: 23,101
    edited 2006-09-18 05:46
    You could use POLLIN, POLLMODE, and POLLRUN to detect the start bit of a character from the PC (a high to low transition) and restart the program. The Stamp program, when it does its SERIN, would ignore any character except the start character and try again. If you use a stop character with all low order zero bits (like "@") and a low speed (like 2400 Baud), you'll increase the width of the zero pulse and the chance of the Stamp catching it the first time.
  • Kevin WoodKevin Wood Posts: 1,266
    edited 2006-09-18 08:35
    How are you sending the command from your pc? Are you doing manually, or is there a program that sends it automatically?

    I can't help you with poll mode, but for the example that I posted, changing the TIMEOUT2 value will adjust howw long the BS2 waits for the stop command before going on to WhatNext.
Sign In or Register to comment.