Post by mackrackit on May 21, 2009 3:46:10 GMT -5
Example of serial I/O using Run Basic and Python on a Linux box. Should also work with windows, but on Linux there is the added benift of it working on a headless setup.
You will need pyserial installed along with Python.
Pyserial can be found here
pyserial.wiki.sourceforge.net/pySerial
Basic instructions are on the above page with examples included in the download.
Once you have pyserial working it is just a matter of calling a Python serial script using RB's SHELL command. I keep my Python scripts in RB's public directory.
Reading from a serial port...
I used a GPS unit for the serial data here. When the RB code is ran it will display one seconds worth of data from the GPS unit.
RB Code:
Python Code:
Anything that Python prints will be displayed in the RB program.
###############################################
In this example, pins 2 and 3 of the serial port will need to be jumped together. Anything sent from the RB program will be echoed back through the jumper then display in the RB program.
RB writes to a file ( this is the part I do not like, but can not figure out how to send a variable from RB to Python using RB's SHELL command ), then calls the Python script. The Python script reads the file and does a write/read to the serial port.
RB Code:
Python Code:
Maybe someone will find this useful.
You will need pyserial installed along with Python.
Pyserial can be found here
pyserial.wiki.sourceforge.net/pySerial
Basic instructions are on the above page with examples included in the download.
Once you have pyserial working it is just a matter of calling a Python serial script using RB's SHELL command. I keep my Python scripts in RB's public directory.
Reading from a serial port...
I used a GPS unit for the serial data here. When the RB code is ran it will display one seconds worth of data from the GPS unit.
RB Code:
g$ = shell$("/home/shop/rb101/public/python/GPS.py")
print g$
Python Code:
#!/usr/bin/env python
import serial
ser= serial.Serial('/dev/ttyS0', 4800, timeout=1)
#s = serial.Serial(0,timeout=1)
s = ser.read(1000)
print s
ser.close()
print "GPS"
Anything that Python prints will be displayed in the RB program.
###############################################
In this example, pins 2 and 3 of the serial port will need to be jumped together. Anything sent from the RB program will be echoed back through the jumper then display in the RB program.
RB writes to a file ( this is the part I do not like, but can not figure out how to send a variable from RB to Python using RB's SHELL command ), then calls the Python script. The Python script reads the file and does a write/read to the serial port.
RB Code:
input dVar$
open "/home/shop/rb101/public/python/test1.txt" for output as #f
print #f,dVar$
close #f
z$ = shell$("/home/shop/rb101/public/python/ser_loop.py")
print z$
Python Code:
#!/usr/bin/env python
import serial
print "START TEST"
f = "/home/shop/rb101/public/python/test1.txt"
FILE = open(f,"r")
fData = FILE.read()
VAR = fData
FILE.close()
ser= serial.Serial('/dev/ttyS0', 4800, timeout=1)
w = ser.write(VAR)
s = ser.read(100)
print s
ser.close()
print "END TEST"
Maybe someone will find this useful.