Headless Serial I/O

new BookmarkLockedFalling
mackrackit
Full Member
***

mackrackit Avatar

Posts: 231

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:

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.
Dave[br][br][url]http://www.mackrackit.com:8888[/url][br]
zoomkat
New Member
*

zoomkat Avatar

Posts: 31Female

Post by zoomkat on May 21, 2009 22:43:16 GMT -5

Another possible variation that might also work is to make a small stand alone application to run using Freebasic instead of python. The below compiles to a 37kb exe for use with apache but something similar could be made input/output via a file rather than environmental variables. The below is test code for web I/O with an ssc-32 servo controller via apache.


Dim As String qs, dat
Dim idx As Integer

qs = Environ("QUERY_STRING")
if qs = "" goto nodata

again:
idx = Instr(qs, "-")
Mid(qs, idx, 1) = "#"
if idx > 0 goto again

qs = qs

Open Com "COM5: 9600,N,8,1,BIN,CD,CS,DS,RS" For Binary As #1
print #1,, qs
Sleep 200
dat = Input$(loc(1), #1)
Close #1

if dat = "" goto nodata
if dat <> "" goto gotdata

nodata:
Print "status: 204"
Print
Print
goto fini

gotdata:
Print "Content-type: text/html"
Print
print "<html><body>"
Print dat
Print "</body></html>"
goto fini

fini:
end

davos1
Guest

Guest Avatar

mackrackit
Full Member
***

mackrackit Avatar

Posts: 231

Dave[br][br][url]http://www.mackrackit.com:8888[/url][br]
mackrackit
Full Member
***

mackrackit Avatar

Posts: 231

Post by mackrackit on May 23, 2009 5:34:29 GMT -5

davos1hit the nail on the head.

Here is the write/read loop updated. Through Python, RB can read and write directly to the serial port.

RB Code:

input dVar$
print "SYS ARG TEST"
exec$ = "/home/shop/rb101/public/python/ser_loop_2.py "+dVar$
A$ = shell$(exec$)
print A$

Python Code:

#!/usr/bin/env python
import serial
import sys
for arg in sys.argv:
ser= serial.Serial('/dev/ttyS0', 4800, timeout=1)
w = ser.write(arg)
s = ser.read(100)
print s
ser.close()
print "END TEST"

Thanks for the help!
Dave[br][br][url]http://www.mackrackit.com:8888[/url][br]