Why is /dev/rfcomm0 giving PySerial problems?
- by Travis G.
I am connecting my Ubuntu box to a wireless readout setup over Bluetooth. I wrote a Python script to send the serial information through /dev/rfcomm0. The script connects fine and works for a few minutes, but then Python will start using 100% CPU and the messages stop flowing through.
I can open rfcomm0 in a serial terminal and communicate through it by hand just fine. When I open it through a terminal it seems to work indefinitely. Also, I can swap the Bluetooth receiver for a USB cable, and change the port to /dev/ttyUSB0, and I don't get any problems over time.
It seems either I'm doing something wrong with rfcomm0 or PySerial doesn't handle it well.
Here's the script:
import psutil
import serial
import string
import time
sampleTime = 1
numSamples = 5
lastTemp = 0
TEMP_CHAR = 't'
USAGE_CHAR = 'u'
SENSOR_NAME = 'TC0D'
gauges = serial.Serial()
gauges.port = '/dev/rfcomm0'
gauges.baudrate = 9600
gauges.parity = 'N'
gauges.writeTimeout = 0
gauges.open()
print("Connected to " + gauges.portstr)
filename = '/sys/bus/platform/devices/applesmc.768/temp2_input'
def parseSensorsOutputLinux(output):
return int(round(float(output) / 1000))
while(1):
usage = psutil.cpu_percent(interval=sampleTime)
gauges.write(USAGE_CHAR)
gauges.write(chr(int(usage))) #write the first byte
#print("Wrote usage: " + str(int(usage)))
sensorFile = open(filename)
temp = parseSensorsOutputLinux(sensorFile.read())
gauges.write(TEMP_CHAR)
gauges.write(chr(temp))
#print("Wrote temp: " + str(temp))
Any thoughts?
Thanks.
EDIT: Here is the revised code, using Python-BlueZ instead of PySerial:
import psutil
import serial
import string
import time
import bluetooth
sampleTime = 1
numSamples = 5
lastTemp = 0
TEMP_CHAR = 't'
USAGE_CHAR = 'u'
SENSOR_NAME = 'TC0D'
#gauges = serial.Serial()
#gauges.port = '/dev/rfcomm0'
#gauges.baudrate = 9600
#gauges.parity = 'N'
#gauges.writeTimeout = 0
#gauges.open()
gaugeSocket = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
gaugeSocket.connect(('00:06:66:42:22:96', 1))
filename = '/sys/bus/platform/devices/applesmc.768/temp2_input'
def parseSensorsOutputLinux(output):
return int(round(float(output) / 1000))
while(1):
usage = psutil.cpu_percent(interval=sampleTime)
#gauges.write(USAGE_CHAR)
gaugeSocket.send(USAGE_CHAR)
#gauges.write(chr(int(usage))) #write the first byte
gaugeSocket.send(chr(int(usage)))
#print("Wrote usage: " + str(int(usage)))
sensorFile = open(filename)
temp = parseSensorsOutputLinux(sensorFile.read())
#gauges.write(TEMP_CHAR)
gaugeSocket.send(TEMP_CHAR)
#gauges.write(chr(temp))
gaugeSocket.send(chr(temp))
#print("Wrote temp: " + str(temp))
It seems either Ubuntu must be closing /dev/rfcomm0 after a certain time or my Bluetooth receiver is messing things up. Even when the BluetoothError arises, the "connected" light on the receiver stays illuminated, and it is not until I power-cycle to receiver that I can reconnect.
I'm not sure how to approach this problem. It's odd that the connection would work fine for a few minutes (seemingly a random amount of time) and then seize up.
In case it helps, the Bluetooth receiver is a BlueSmirf Silver from Sparkfun. Do I need to be trying to maintain the connection from the receiver end or something?