3

I am using PySerial on a Raspberry Pi 3b to recieve data from an external bluetooth device (FireFly. I am able to get a connection to the device, and receive data. But when I close the connection via close(), the device doesn't seem to disconnect properly from the Raspberry (this is partly indicated by the green light at the Firefly not changing led colors after turning off the connection. Below is the code I'm trying to get to work. I have tried using both PySerial 2.7 and PySerial 3.3.0 aswell as python 2.7.9/10 and python 3.4. None of these changes seems to matter.

import serial
import sys
from timeit import default_timer as timer
port = serial.Serial(
        baudrate=115200,
        parity=serial.PARITY_NONE,
        stopbits=serial.STOPBITS_ONE,
        bytesize=serial.EIGHTBITS,
        timeout=1

    )
port.port = '/dev/rfcomm1'
port.open()

sys.stdout.flush()
time = timer()
while timer()-time < 10:
    print(port.read())

port.flush()
port.flushInput()
port.flushOutput()
port.close()

If I try using minicom to recieve data it works flawlessly unless I've previously used the python script, in which case it doesn't recieve any data except in some cases where it will recieve data after about 5 minutes.

Below is a screenshot of after the script has been executed and finished. The FireFly is still connected, which causes the program not to be able to run again until the device is restarted.

1

Any help would be greatly appreciated :)

3

You're not properly disconnecting the device. In fact, you're not disconnecting the device at all. Your FireFly device is connected to your RPi, not your Python application.

Your script is reading a serial port on our device, and then closing it. This is a very similar concept to opening a file. If you have code that opens a file, writes to it, and then closes it.

# Essentially, what you're doing is:
f = open("file.txt", "w")
f.write("hello, world")
f.close() # You wouldn't expect this to delete your file, right?

The simplest way to actually disconnect the FireFly device would probably be using a system call.

bluez-test-device disconnect xx:xx:xx:xx:xx:xx
| improve this answer | |
  • "Your FireFly device is connected to your RPi, not your Python application" -> Begs the question why minicom apparently disconnects properly. Using bluez seems like having a toolbox with only a hammer in it, but if pyserial is missing some features, then a hammer will work (isn't there a python interface to bluez? Perhaps the OP is using the wrong thing to start with...). – goldilocks May 11 '17 at 16:51
  • I have no idea. I've honestly never dealt with python+bluetooth. Bluetooth 3.0 (what I assume this is) connects as an interface for a serial device. So any application that can read and write serial data can easily communicate. – Jacobm001 May 11 '17 at 16:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.