-
Get the BlueSmirf,
-
connect the RTD and CTS
-
connect also the TX and RX so you’ll get back whatever you send.
-
Get the python script below,
it just opens thread that loops on a routine that looks for incoming data and prints it out, after that it sends a write command
- Run the script, the first time you run it works fine:
Script output:
c:\test>serio.py
Connecting
Connected OK
Startign thread that reads from serial port
Send some data
Waiting
>
CONNECT,00081B83A8D5
--- exit ---
if I run it a second time the scripts is looping over and over on sending out the characteres
c:\tests>serio.py
Connecting
Connected OK
Startign thread that reads from serial port
Send some data
If I exit the script and try to use the HyperTerminal to double check I can also see that the BlueSmirf is not responding…
Below you’ll find the python script I have used, that it turns out to work fine with other serial and bluetooth devices as my nokia phone…
Please tell me what is going on!
Thanks
XChip
#please fix this line below, I had to add extra spaces otherwise the system wouldn't accept my post
#/ usr / bin / env p y t h o n
import sys, os, serial, threading, time
try:
True
except NameError:
True = 1
False = 0
class Redirector:
def __init__(self, seri):
self.serial = seri
def Start(self):
"""connect the serial port to the tcp port by copying everything
from one side to the other"""
self.alive = True
self.thread_read = threading.Thread(target=self.reader)
self.thread_read.setDaemon(1)
self.thread_read.start()
#self.writer()
def reader(self):
"""loop forever and copy serial->socket"""
while self.alive:
try:
data = self.serial.read(1) #read one, blocking
n = self.serial.inWaiting() #look if there is more
if n:
data = data + self.serial.read(n) #and get as much as possible
if data:
print ">", data
except serial.SerialException, msg:
print msg
#probably got disconnected
break
self.alive = False
def writer(self, data):
self.serial.write(data) #get a bunch of bytes and send them
if __name__ == '__main__':
ser = serial.Serial()
ser.port = 15
ser.baudrate = 31250
ser.rtscts = False
ser.xonxoff = False
ser.timeout = 1 #required so that the reader thread can exit
print "Connecting"
try:
ser.open()
except serial.SerialException, e:
print "Could not open serial port %s: %s" % (ser.portstr, e)
sys.exit(1)
print "Connected OK"
print "Startign thread that reads from serial port"
r = Redirector(ser)
r.Start()
print "Send some data"
r.writer("Anyone there?")
print "Waiting"
time.sleep(4)
print "\n--- exit ---"