Shop OBEX P1 Docs P2 Docs Learn Events
Serial port communication with the PC and settings — Parallax Forums

Serial port communication with the PC and settings

Spiral_72Spiral_72 Posts: 791
edited 2011-02-23 19:45 in BASIC Stamp
Good evening...... I'm running a BS2px with a Parallax USB-Serial converter on my Serial port-delete Win7 machine. The converter has worked great up to this point. I can program through, debug screen through and do all functions necessary with the BS.

When I do an ID in the BS editor I get: COM3 Basic Stamp 2px24, Version v1.0, Loopback NO, Echo YES

Now my question, that I've been unable to find an answer for:

I'm writing code for RX/TX communications between the BS2 and PC. The current project is simple.

PC: Sends "SVA" a token to indicate servo A, followed by a 16bit number e.g. decimal 01000
BS2: Receives the token and 16bit number (decimal 01000) and sends a confirmation "SVA OK"
PC: Pulls the following data off the serial port: "SVA01000^SVA OK"

The "^" is a non-numeric stop character to let the BS know the number has finished sending.
SERIN 16,16780,[WAIT("SVA"),DEC servo1]

Am I seeing the Echo YES function in this and that's why all transmitted data is being read back into my PC? I think so. Is there any way to turn Echo OFF?? Is determined by how the connector is wired? Does the Stamp still program after turning Echo OFF?

I've been all over the manuals, the converter settings, Google and the forums. I'm not seeing the answer. Perhaps I'm asking the wrong question.

Thank you once again,

Comments

  • Spiral_72Spiral_72 Posts: 791
    edited 2011-02-21 20:14
    Sending CR (ASCII 13) or LF (ASCII 10) after the transmission doesn't work either :(
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2011-02-22 05:18
    Hi , if you use the programming port , P16 , for serial communication then your PC side program must be programmed to take care of the echo. If you use the pins P0 to P15 and use a Rs232 to TTL converter between PC and Stamp there is no echo to worry about.

    Jeff T.
  • Spiral_72Spiral_72 Posts: 791
    edited 2011-02-22 05:28
    Hi , if you use the programming port , P16 , for serial communication then your PC side program must be programmed to take care of the echo. If you use the pins P0 to P15 and use a Rs232 to TTL converter between PC and Stamp there is no echo to worry about.

    Jeff T.

    No kidding?? Well..... Smile. I guess I should have asked here first. Thank you unsoundcode. I learned something valuable this morning.

    Time to rework my code it looks like.
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2011-02-22 05:44
    Hi, which program are you using on the PC side for your TX/RX comms.

    Jeff T.

    Not only are the characters ehoed back but they will remain in the serial port buffer until purposefully removed or the port is closed. Knowing how many characters are transmitted tells you how many are echoed back and how many to remove. It is also worth mentioning that the characters echoed back take time to arrive at the PC which is dependent on baud rate and packet size , it might take 1 or 2 milliseconds for each byte to arrive back at the PC at a baud of 9600. This may mean pausing the PC program briefly to ensure the transmission lines are clear and ensure the echo has completely arrived at the buffer for deletion.
  • Spiral_72Spiral_72 Posts: 791
    edited 2011-02-22 10:11
    My PC program is currently written in Pure Basic to work out my ideas. It will be written in C++ as soon as my issues are all worked out. At the moment, I'm learning.

    One thing I don't understand is, I used Tera Term (open source terminal program) to verify the BS program and the communications. Tera Term does not show the echoed characters..... but my PC/Basic program does.

    "Not only are the characters ehoed back but they will remain in the serial port buffer until purposefully removed or the port is closed"
    Removed?? As in read off the port/buffer by my PC program right? I don't know of any other purges, unless that is my inexperience showing.

    I'll see about filtering the echoed characters tonight..... that is unless I can come up with another alternative, like finding a problem. It sounds like the echo is there to stay though unless I use P0-15 for communications.
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2011-02-22 11:06
    Hi Spiral_72 , while your on basic check this link

    http://forums.parallax.com/showthread.php?96973-VB-Express-to-Stamp-Template

    As with anything there are drawbacks but it has a lot going for it , for a GUI it works really well I have used VB with the Bs2px and the serial exchange is pretty fast. The applications themselves are by no means slow and unless you are into something extremely complex or graphic intensive its not going to appear much slower than a C++ app. These same routines are also suited for the Propeller with the smallest of modification. You could be up and running with VB Express in a very short period of time.

    Although the syntax is different C# mainly uses the same methods and objects as VB so that transition would not be too difficult.

    Clearing the serial port buffer is as you say a case of reading the characters, VB has additional commands that also "discard" the buffer.

    Jeff T.
  • $WMc%$WMc% Posts: 1,884
    edited 2011-02-22 18:08
    Spiral_72
    '
    You can use the DEBUG and DEBUGIN statements to talk to a PC. Just close the DEBUG window and open the program port with your own serial program.(PureBasic in your case,COM3)
    '
    The DEBUG command is super short to use.
    '
    Code
    ' WaltsGUI_DEBUGIN.BS2
    ' This program demonstrates the ability to accept user input from the
    ' Liberty made GUI terminal or any other terminal that sends ASCII code to the serial port
    ' This works with WaltsStart_Stopbtns.bas --LibertyBASICv4.30
    ' {$STAMP BS2e}
    ' {$PBASIC 2.5}
    myNum VAR Word
    x VAR Byte
    servo1 PIN 15
    servo2 PIN 13

    Main:
    DO
    DEBUGIN NUM myNum ' retrieve word from WaltsGUI and place in myNum VAR Word
    PAUSE 30
    IF myNum = 1001 THEN GOSUB LEDon
    'IF myNum = 11001 THEN GOSUB Mov_FWD
    IF myNum = 1010 THEN GOSUB LEDoff
    'IF myNum = 11010 THEN GOSUB Mov_stop
    IF myNum = 11011 THEN GOSUB LED2on
    IF myNum = 11111 THEN GOSUB LED2off
    LOOP
    LEDon:
    HIGH 0
    PAUSE 10
    DEBUG "LED is on",CR
    RETURN
    LEDoff:
    LOW 0
    DEBUG "LED is off",CR
    RETURN
    LED2on:
    HIGH 1
    RETURN
    LED2off:
    LOW 1
    RETURN
    END
    '
    Set the serial stuff up in your PureBasic code. Something like
    "open "COM3:9600,n,8,1,ds0,cs0,rs" for random as #comm"
    '
    Don't forget to use a TrapClose in you PureBasic program to close the COM port when your done with it.Else you'll have to restart the PC to open it again.
  • Spiral_72Spiral_72 Posts: 791
    edited 2011-02-22 19:08
    aaaaaaHAHAHA! IT WORKS! Thank you all.

    I send a token, then a 16-bit servo position from the PC.... Then I have to send a non-numeric byte to indicate "No more digits will follow"...... I already said that I know.

    So what I did was on the PC, send the token, position and stop byte. Then I READ from the serial port until the stop byte is found..... effectively clearing the port except for the BS's confirmation message.

    And it works. Now the PC accepts a typed servo position, sends it to the BS, the BS moves the servo in position, then waits for the next position.


    Man that was painful, but I learned a lot. Now to hot glue a PING on the servo and add some more code :)


    $WMc%:
    I have never used DEBUGIN or DEBUG before. It looks like I need to look it up. Don't tell me I did this the hard way! :)
  • Spiral_72Spiral_72 Posts: 791
    edited 2011-02-22 19:13
    The source for your information or use. It's not a final version, but help yo-self.

    My BS2 program is outputting messages to an LCD to keep me informed, but you don't need the LCD.
    serial servo4.bpx

    The PC side is written in Pure Basic. The file should be renamed to ".PB" extension. The forums won't let me upload a .PB file without zipping it.
    serial servo4_pb.txt
  • Spiral_72Spiral_72 Posts: 791
    edited 2011-02-23 19:45
    I've got the PING hotglued to the servo and it generates a polar plot!!

    It works but just barely. Sometimes it flakes out and I get bogus data across the serial port (like values as high as 6 million) so I still have work to do on the PC end. I can do a 60 degree sweep and it'll plot the distances :)

    COOL!
Sign In or Register to comment.