Shop OBEX P1 Docs P2 Docs Learn Events
How to connect using Python — Parallax Forums

How to connect using Python

I have the following:
import serial
ser = serial.Serial(
port='COM16'
)

ser.isOpen()
ser.write("' {$STAMP BS2}\r\n")
ser.write("' {$PBASIC 2.5}\r\n")
ser.write("TxPin CON 5\r\n")
ser.write("Baud2400 CON 396\r\n")
ser.write("SEROUT TxPin, Baud2400, [12]\r\n")
ser.write("SEROUT TxPin, Baud2400, [\"Display this\"]\r\n")
ser.close()


And I can't seem to get my display to work? Any suggetions?

Comments

  • Why I are sending a code listing to the display? Based on the PBASIC, you need to open the COM port at 2400 baud, send a 12 (0x0C), then write to it.
  • KeithEKeithE Posts: 957
    edited 2018-08-15 04:40
    Maybe MOJO thinks that there's a BASIC interpreter running on the BASIC Stamp which would tokenize that code?

    If you are using a BASIC Stamp I would recommend visiting this page:

    https://www.parallax.com/product/bs2-ic

    There is downloadable software, and documentation to guide you through the whole development process.

    But maybe you should take a photo or describe your setup in text so that we know what you are working with. It's not even clear that you have a BASIC Stamp. Maybe you found some sample code for how to talk with a serial LCD for the BASIC Stamp, and you are trying to port it to Python on a PC?
  • Attached is a picture of my microcontroller, I'm not worried about all the rest of the functionality right now, just trying to be able to run a Python script that will change the LCD Display. Here is a copy of my PBASIC Script that works to change my LCD panel:

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

    '
    [ I/O Definitions ]

    TxPin CON 5
    Baud2400 CON 396
    SEROUT TxPin, Baud2400, [12] 'clear screen
    SEROUT TxPin, Baud2400, ["MOJO is Awesome"]

    When I run this through the PBASIC Interpreter (assuming it on COM9), it correctly tells the LCD to Clear the screen and then change the text to "MOJO is Awesome". Now can I talk directly to the microcontroller using Python the same way as the PBASIC Interpreter.

    Here is a response from Parallax:

    The Homework Board can send data to the computer, and parse data from it, over the USB serial connection, using 'SERIN' and 'SEROUT' statements on pin '16'. Any computer program or library able to read from, and write to, the serial port can communicate with the Homework Board, including Python's pySerial library.

    ― David Carrier
    Parallax Inc.


    So I was wondering what special magic I need to send to it to get it to respond the same way?
    2980 x 1677 - 2M
  • There's a fundamental misunderstanding of the PBASIC compilation process here. The PBASIC compiler does not send source code to the BASIC Stamp. Rather, it compiles the program into binary tokens and sends those to be stored in the Stamp's EEPROM. So transmitting program text to the Stamp from your Python program will not work.

    What you need to do is write one PBASIC program that contains a loop that waits for text from virtual pin 16, sends that text to your LCD screen, then loops back to wait for more text.

    -Phil
  • Thanks for the clarification Phil, is there a way (in python), that I can have multiple PBASIC scripts (.bs2's) and tell the PBASIC Complier (maybe "pbasic.exe -r bob.bs2") to run a certain script?
  • GenetixGenetix Posts: 1,742
    edited 2018-08-15 19:58
    MOJO,

    The BS2 doesn't run scripts, it runs a "tokenized" BASIC code that is stored in the EEPROM.

    Programs that interact with a PC using SERIN and SEROUT.
    SERIN has a WAIT option that can be used to look for a special code or character.
    Pin 16 is for the programming or Debug port that connects to the PC.

    Be aware though that the BS2 is slow, can only do 1 thing at a time, and only has 26 bytes of RAM.

    I am trying to find an example program for you to use as a reference.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2018-08-15 20:32
    MOJO wrote:
    Thanks for the clarification Phil, is there a way (in python), that I can have multiple PBASIC scripts (.bs2's) and tell the PBASIC Complier (maybe "pbasic.exe -r bob.bs2") to run a certain script?
    Yes, that should work, assuming that you can initiate other programs from Python. The command would be:

    "c:\Program Files (x86)\Parallax Inc\Stamp Editor v2.5.3\stampw.exe" /nodebug /noprompts /download filename.bs2

    This assumes that your version of stampw.exe is located where mine is. It also assumes that backslashes in Python are not escapes. Since Python works under multiple OS's, the backslashes may need to be forward slashes or else escaped: \\.

    -Phil
Sign In or Register to comment.