Can't connect to device via RFCOMM because 'Resource Busy' - python

I am trying to open a connection with a Lego EV3 brick, whose serial port is /dev/tty.EV3-SerialPort, I am on mac 10.6.8. I get a Resource busy when I do this, yet when I use other API's to connect (writing to the serial port not through pyserial) it does not show an error. I would like to find a way to get around this error. Why is it busy, all other bluetooth applications are disabled. Here is my code:
test.py:
import serial
import time
ser = serial.Serial('/dev/tty.EV3-SerialPort', 19200, timeout=1) # open first serial port
ser.close()
ser.open()
time.sleep(1)
ser.close()
print "closed"
Here is the error it outputs:
File "test.py", line 7, in <module>
ser.open()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/serial/serialposix.py", line 289, in open
self.fd = os.open(self.portstr, os.O_RDWR|os.O_NOCTTY|os.O_NONBLOCK)
OSError: [Errno 16] Resource busy: '/dev/tty.EV3-SerialPort'
A popup also presents itself saying:
A Bluetooth serial failure has occurred.
Failed to open an RFCOMM serial channel.
Check if authentication needs to be enabled in your device

I have been able to communicate with the EV3 by just opening the serial port as a file rather than using the serial module.
with open('/dev/tty.EV3-SerialPort', 'w+', 0) as bt:
See https://bricks.stackexchange.com/a/4257/3498 for a complete example.

I used exactly your example and works to me, check out (in my case the connection name is different as shown below:).
EV3 = serial.Serial('/dev/tty.EV3-N1-SerialPort', 19200, timeout=1)

Related

Bluetooth serial connection between Raspberry Pi and Windows 10 times out after a short while

I want to set up serial Bluetooth communication between a Raspberry Pi 3 with integrated Bluetooth module and a Windows 10 machine. The raspi is the server and runs a python script, that handles the connection and the data transmission. I have set it all up, following several tutorials in the internet and its working really good on the server side. I have tested the server with a serial terminal on my android phone and it was working as expected: All the messages were transferred correctly and it stayed connected as long as i wanted. Testing with my windows machine, on the other hand, caused a lot of problems. I have solved most of them and now i can connect my windows machine, over a serial terminal, to the raspi. My problem now is, that, after a short while, the connection dies, usually after 10 to 40 seconds, and i could not find a fix for that. The error message on windows is: cannot open COM4 and the error message on the raspi is: bluetooth.btcommon.BluetoothError: [Errno 110] Connection timed out.
Here is my python script, id that is actually the problem:
import serial, bluetooth, subprocess, select
serialPort0 = serial.Serial(
port = '/dev/serial0',
baudrate = 115200,
parity = serial.PARITY_NONE,
stopbits = serial.STOPBITS_ONE,
bytesize = serial.EIGHTBITS,
timeout = 1
)
server_sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
server_sock.bind(("", bluetooth.PORT_ANY))
server_sock.listen(1)
port = server_sock.getsockname()
uuid = "94f39d29-7d6d-437d-973b-fba39e49d4ee"
bluetooth.advertise_service(server_sock, "bluetoth_server", service_id=uuid,
service_classes=[uuid, bluetooth.SERIAL_PORT_CLASS],
profiles=[bluetooth.SERIAL_PORT_PROFILE],
# protocols=[bluetooth.OBEX_UUID]
)
print("Waiting for connection on RFCOMM channel", port[1], ", addr", port[0])
client_sock, client_info = server_sock.accept()
print("Accepted connection from", client_info)
client_sock.setblocking(0)
while True:
if not serialPort0.isOpen:
raise Exception("Serial port closed unexpected")
break
ready = select.select([client_sock], [], [], 0.5)
if ready[0]:
blu_data = client_sock.recv(4096)
if not blu_data:
break
serialPort0.write(blu_data)
ser_data = serialPort0.readline().decode("utf-8")
client_sock.send(ser_data)
client_sock.close()
server_sock.close()
It reads data from a serial port and from a serial Bluetooth port and sends it to the other one respectively.
I am at a point, where i don't know what else to try and I would really appreciate some help. Thank you in advance.
Your description related to timeout would be pointed to
/etc/bluetooth/main.conf
Bluetooth configuration file.
You will need to uncomment the pairabletimeout=0 that will prevent the timeout problems.
You may want to see Raspberry Bluetooth configuration (recent answer) for details.
Bluetooth Serial Configuration

How to read and write from a COM Port using PySerial?

I have Python 3.6.1 and PySerial Installed. I am trying the
I am able to get the list of comports connected. I now want to be able to send data to the COM port and receive responses back. How can I do that? I am not sure of the command to try next.
Code:
import serial.tools.list_ports as port_list
ports = list(port_list.comports())
for p in ports:
print (p)
Output:
COM7 - Prolific USB-to-Serial Comm Port (COM7)
COM1 - Communications Port (COM1)
I see from the PySerial Documentation that the way to open a COM Port is as below:
import serial
>>> ser = serial.Serial('/dev/ttyUSB0') # open serial port
>>> print(ser.name) # check which port was really used
>>> ser.write(b'hello') # write a string
>>> ser.close() # close port
I am running on Windows and I get an error for the following line:
ser = serial.Serial('/dev/ttyUSB0')
This is because '/dev/ttyUSB0' makes no sense in Windows. What can I do in Windows?
This could be what you want. I'll have a look at the docs on writing.
In windows use COM1 and COM2 etc without /dev/tty/ as that is for unix based systems. To read just use s.read() which waits for data, to write use s.write().
import serial
s = serial.Serial('COM7')
res = s.read()
print(res)
you may need to decode in to get integer values if thats whats being sent.
On Windows, you need to install pyserial by running
pip install pyserial
then your code would be
import serial
import time
serialPort = serial.Serial(
port="COM4", baudrate=9600, bytesize=8, timeout=2, stopbits=serial.STOPBITS_ONE
)
serialString = "" # Used to hold data coming over UART
while 1:
# Wait until there is data waiting in the serial buffer
if serialPort.in_waiting > 0:
# Read data out of the buffer until a carraige return / new line is found
serialString = serialPort.readline()
# Print the contents of the serial data
try:
print(serialString.decode("Ascii"))
except:
pass
to write data to the port use the following method
serialPort.write(b"Hi How are you \r\n")
note:b"" indicate that you are sending bytes

python-escpos QS Printer not printing

My QS-5801 printer doesn't print out. My PC is running on Windows 10, have Python 2.7 and python-escpos installed. I tried this but it's not working.
>>>from escpos.printer import Serial
>>>ser = Serial('COM3', 9600, timeout=1)
Serial printer enabled
>>>ser.text('hello world') #nothing happened
The printer itself is ESC/POS compatible. I'm connecting to it via USB (prolific USB-to-Serial Comm PORT (COM3)).
This is actually the expected behaviour for your snippet. The text remains in the printer's buffer until a line feed \n is sent to terminate the line of text.
Assuming that your printer is correctly listening on COM1, you probably just want to modify your snippet to include a newline-
from escpos.printer import Serial
ser = Serial('COM3', 9600, timeout=1)
ser.text("Hello world\n")
The python-escpos README contains an example of this usage.

Error while connecting HC-05 bluetooth module with python via bluetooth

I have been trying to connect my hc 05 bt module with my laptop and to achieve a communication with Idle.
At first i connected my bt device manually from laptop's bluetooth settings and then I was able to get the module's bt address by using the following code:
import bluetooth
target_name = "HC-05"
target_address = None
nearby_devices = bluetooth.discover_devices()
for bdaddr in nearby_devices:
if target_name == bluetooth.lookup_name( bdaddr ):
target_address = bdaddr
break
if target_address is not None:
print "found target bluetooth device with address ", target_address
else:
print "could not find target bluetooth device nearby"
I received the bluetooth address as "98:D3:31:70:7D:2D"
Now i wrote the following code to connect HC-05 via python..
bd_addr = "98:D3:31:70:7D:2D"
port = 20
sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM )
sock.connect((bd_addr, port))
sock.send("1")
sock.close()
Now i browse through my laptop's bluetooth comm port settings and found 2 things
1st...
COM19
Direction--- Incoming...
Name--- HC-05
2nd...
COM20
Direction--- Outgoing
...
Name--- HC-05 'Dev B'
Hence I chose COM20 as I want to transmit data from Python.
When I run this code i get error:
Traceback (most recent call last):
File "C:\Python27\TestBluetooth.py", line 26, in <module>
sock.connect((bd_addr, port))
File "C:\Python27\lib\site-packages\bluetooth\msbt.py", line 72, in connect
bt.connect (self._sockfd, addr, port)
IOError: A socket operation failed because the destination host was down.
I tried both COM19 and COM20 but got the same error.
My Bluetooth is connected via TX pin to arduino's RX pin and the arduino is connected to my PC so there is no error of COM port sharing that occurs sometimes.
Also when I connect my bluetooth module with laptop bluetooth and open Bluetooth Serial terminal and transmit data from there it works fine.
So there's some problem in my understanding and writing of Python code.
Please help...

Error in working with pyserial module of python

import time
import serial
# configure the serial connections (the parameters differs on the device you are connecting to)
ser = serial.Serial(
port='/dev/ttyS0',
#port='/dev/ttyACM0',
baudrate=115200,
parity=serial.PARITY_ODD,
#stopbits=serial.STOPBITS_TWO,
#bytesize=serial.SEVENBITS
)
ser.isOpen() # returns true
time.sleep(1);
ser.write("some_command \n")
ser.close()
I have a embedded board. It has a serial port which is connected to my computer. I am running above script to access this serial port and run some board specific commands.
My problem
I open my serial port (using minicom in linux) separately and then run above script, It works. If I don't open serial port separately, Script doesn't work.
try
ser.write("some_command \n".encode())
alternatively try
ser.write(bytes(b"some_command \n"))

Categories

Resources