Shop OBEX P1 Docs P2 Docs Learn Events
BS2 SEROUT accurate settings? — Parallax Forums

BS2 SEROUT accurate settings?

ChronikChronik Posts: 8
edited 2010-11-11 17:30 in BASIC Stamp
Hi,

i did search but found nothing on this specifically.

I am trying to reset a HSA-TCU with BS2 activity kit. (Via COM port on the TCU)

This TCU can be resetted via HyperTerminal by sending the following list of commands:

ENTER
ENTER
y

I am using the SEROUT command, but it seems that the most responsive way to use that command is with Pin 16.

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

X VAR Word
X=16
Y CON 16468

DO
  IF (IN3 = 1) THEN
    HIGH 10
    LOW 14
    SEROUT X, Y, [CR]
    PAUSE 1500
    SEROUT X, Y, [CR]
    PAUSE 2000
    SEROUT X, Y, [121]
    PAUSE 500
  ELSE
    LOW 10
    HIGH 14
    PAUSE 250
  ENDIF
LOOP


The thing is that this code does work, but i sometimes have to do run it (press the button that activates the code to send commands to the TCU) several times (~3-4 times)

Do you guys have any idea on how i could improve the program to make it work on the first time?!

Thanks

Comments

  • stamptrolstamptrol Posts: 1,731
    edited 2010-11-08 07:43
    You can put all three characters in one SEROUT statement. The long pauses between characters may also be letting the TCU misinterpret the incoming data.

    Also, does the TCU interpret <Enter> as only the CR? Many devices interpret it as CR,LF.
  • ChronikChronik Posts: 8
    edited 2010-11-08 08:04
    Hi,

    thanks for the reply.

    We have no problem reseting the TCU with HyperTerminal and a PC with the delays i entered in the program.

    The TCU does seem to receive <ENTER> as only the CR. (i verified it by monitoring TCU's COM2 with H.T. and it does cycle thru the menus with the actual code). But maybe the problem comes when sending the "y" (121) without a previous LF.

    if i wanted to do it in one line, would it be:

    SEROUT X, Y, 1000, [CR, CR, LF, 121]

    (i am mostly worried about the 121 that is ASCII..)

    Thanks
  • ChronikChronik Posts: 8
    edited 2010-11-08 12:22
    i would like to add that on HT i see:

    ANSIW, and 9600-8-N-2.

    (stop bit is 2).
  • Mike GMike G Posts: 2,702
    edited 2010-11-08 20:26
    You might try replacing "IF (IN3 = 1) THEN" with the BUTTON command.
  • stamptrolstamptrol Posts: 1,731
    edited 2010-11-09 05:27
    You note that the device uses 9600-8-n-2 which is not a standard Stamp offering.

    When you're using HTerm, do you change the stop bits to 2?
  • Mike GMike G Posts: 2,702
    edited 2010-11-09 06:31
    I didn't test this but if you look at the code, there 250 ms pause in the false condition block (IF IN3 = 1) inside the DO...LOOP. So I image the STAMP is paused most of the time or IN3= 0. That would explain why it takes a few button presses to execute the true condition block.

    Try using the BUTTON command.
  • ChronikChronik Posts: 8
    edited 2010-11-09 07:16
    Thanks for the replies,

    The HIGH/LOW 14 and HIGH/LOW 10 activates a red or a green LED to tell me when it is actually sending the commands. With 250ms pause, the program does run pretty much everytime i press the button.

    stamptrol : yes in HT i do set stop bit to 2.

    I can use COM2 from the TCU unit to monitor the unit's response to the BS2 commands, and it does go thru no problem, except for the "y" i have to send... It seems i have to send "y" at least 3 times to make it work.

    Thanks again
  • ChronikChronik Posts: 8
    edited 2010-11-09 11:01
    i am having a lot of problem reseting the TCUs, even tho i send the commands 3-4 times in a loop.

    I am monitoring everything via HT and com2, and the TCU does receive the inputs from BS2, but doesnt want to reset.

    Via HT, : ENTER + ENTER + "y" and it works everytime like a charm.

    Thanks
  • stamptrolstamptrol Posts: 1,731
    edited 2010-11-09 12:21
    The point is, the Stamp does not send 2 stop bits. It sends 8-N-1 ( 1 stop bit).

    It is only by a fluke of the repeated bits that you are seeing the correct reset.

    Can the TCU be set to receive one of the standard Stamp communication set ups?
  • ChronikChronik Posts: 8
    edited 2010-11-09 13:32
    i doubt we can modify the TCU so that it works with 1 stop bit instead of 2.
    I thought i could maybe bypass the process by sending only one character.. but i guess that sending it ASCII (y=121) is done in 3 data transfer... is that right?

    also , i just read:
    [QUOTE=Programming and customizing the BASIC Stamp computer By J. Scott Edwards (ref:http://books.google.ca/books?id=mwQksHdB0zUC&lpg=PA154&ots=J1dmZVMGvz&dq=bs2%20stop%20bits&pg=PA154#v=onepage&q=bs2%20stop%20bits&f=false) ]
    Although the BS2's Serout instruction does not directly support multiple stop bits, it does allow you to specify pacing in milliseconds. Pacing is a delay between frames of data, so it amounts to pretty much the same thing as multiple stop bits. For example, 2400-bps serial data sent with 1 ms pacing amounts to about 3.4 stop bits. There's one stop bit built into the data frame, plus a 1-ms delay, which amounts to about 2.4 additional bit times.
    When a specification calls for multiple stop bits, its's really saying "this is the minimum delay between frames that this device can handle," so it's OK to exceed that amount.
    [/QUOTE]

    so can i set the last SEROUT to SEROUT X,Y,2 [121] and hope this to work? because i think i did try it at some point.
  • stamptrolstamptrol Posts: 1,731
    edited 2010-11-10 12:49
    The stop bits are part of EVERY character sent. Each character expected includes a Start bit, 8 data bits, 2 stop bits.

    What you might be able to do is "bit-bang" the correct sequence ( cr, cr, y) by turning an output on and off at a speed equivalent to 9600 baud. It would take a bit of time and experimentation, but isn't too hard.
    There's lots of info on the web regarding RS-232 which may get you on your way.

    Try writing a short loop so the Stamp sends out one character to be read by HyperTerminal.
  • ChronikChronik Posts: 8
    edited 2010-11-11 12:33
    Thanks for the reply,

    i really dont know where to start writing a short loop to try to simulate the "y"... do you have any clues how could i do that?

    Thanks
  • stamptrolstamptrol Posts: 1,731
    edited 2010-11-11 13:46
    Just Google "Asynchronous Serial Communication" and you'll see lots of examples.

    Basically, the serial line is held high. When it changes to low, that tells the receiver its the beginning of the Start bit.
    The next 8 bits go either high or low, depending if they are 1's or 0's.
    There is no parity in 8-N-2, so after the 8th data bit, the serial line stays low for a period equal to two bit-lengths to represent the 2 Stop.

    So, to send a "y" which is ASCII 121 or 01111001, the following would happen:

    The serial line is held high. It drops to zero for one bit length. Then, the serial line is switched on and off for each of the 8 data bits (either 1 or 0). Finally, the serial line will be held Low for two bit lengths to represent the 2 Stop bits. Theres a bit of a complication because each bit is represented by high=0, low=1 which is mentally opposite to what we expect.

    What's making your job harder is that, normally, the stamp uses a 10-bit length to send a character. If you were using 8-N-1, that would be 1 Start, 8 Data, 1 Stop. If you were using 7-E-1, it would be 1 Start, 7 Data, 1 Parity, 1 Stop which is also a total of 10 bits.

    When you go to 8-N-2, now all the frames are 11-bit, which was a pretty popular thing, but just not supported by the Stamp. There are some chips similar to Stamps which can do 8-N-2.
  • ChronikChronik Posts: 8
    edited 2010-11-11 17:30
    Thanks again for all the info.

    so it means that sending over and over ASCII 121 i end up getting a good 8-N-1 "y".

    How could i simulate an High/low state with the stamp?

    And what are the other chips that would handle 8-N-2 communication?

    Thanks again for your time!!
Sign In or Register to comment.