How to connect using Python
MOJO
Posts: 3
in BASIC Stamp
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?
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
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?
' {$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?
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
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.
"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