Linux bluetooth serial can only be opened once - linux

I have a bluetooth serial port and am trying to connect to it. I do:
sudo rfcomm bind 13 00:0A:3A:26:4A:86
and that seems to succeed. I then try to access it:
>>> f=file('/dev/rfcomm13','rw')
>>> f.close()
>>> f=file('/dev/rfcomm13','rw')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IOError: [Errno 16] Device or resource busy: '/dev/rfcomm13'
This seems to be consistent -- it can be opened once, but ever afterwards it is "busy" until I unbind and rebind it. Doing this in python shows the error most clearly, but it seems to happen everywhere.
It seems that the close syscall isn't cleaning up some key resource. From a quick skim of the driver source, it's probably a dlci channel or something similar, but I'm pretty vague on what those are.
Is there any way to open the connection multiple times per bind?
Thanks

Related

Read a dtoverlay controlled device via Python3?

How to read a dtoverlay controlled device, i.e. sensor via pyhon3?
I can read the device via a simple cat, for example...
# cat /sys/bus/i2c/devices/1-0077/iio\:device0/in_temp_input
27130
So I know the basic setup and such is good, sensor is at address 0x77, it is a BME280 sensor, etc.
I can also read the sensor via the various python3 libraries for such sensors, say the python library from Adafruit.
But I want to use the dtoverlay method of sensor control, i.e. read, and read the sensor from python3. This seemed obvious and straight forward, but apparently not, tried the following code and got the following error.
#!/usr/bin/python3
#
#
import os
#
theSensor=os.open('/sys/bus/i2c/devices/1-0077/iio\:device0/in_temp_input', os.O_RDONLY)
os.lseek(theSensor, 0, os.SEEK_SET)
print(os.read(theSensor, 2))
theSensor.close()
And the error...
# python3 BME280-OverLay.py
Traceback (most recent call last):
File "/root/BME280-OverLay.py", line 17, in <module>
theSensor=os.open('/sys/bus/i2c/devices/1-0077/iio\:device0/in_temp_input', os.O_RDONLY)
FileNotFoundError: [Errno 2] No such file or directory: '/sys/bus/i2c/devices/1-0077/iio\\:device0/in_temp_input'
Is there some trick to reading this specific device path via python3? The simple cat works.
In your initial cat command, you noticed that there's a \ inside the URL. That's an escape character. It might be there because you used autocompletion with the Tab key ; in this case bash adds it automatically, even though in fact cat doesn't need it, but deals with it.
python doesn't (deal with it). You'll have to feed open() with the clear path syntax.
By the way you can use a plain open() call and the with syntax :
with open('/sys/bus/i2c/devices/1-0077/iio:device0/in_temp_input', 'r') as fd:
temp = fd.read()
print(temp)
This way, the file gets closed before the print() call.
PS: the fact that the file you are trying to read is on a virtual filesystem would have no impact.

Reading registers using Pymodbus (Modbus RTU)

I am trying to communicate a Modbus slave device that uses Modbus RTU protocol, with RPi having pymodbus lib in python 3.8.
Here's a link for this library
http://riptideio.github.io/pymodbus/
My concern is when printing holding registers responses. I have read other posts regarding it and people have suggested using '.registers' with the print command.
How to read from Registers with pymodbus
At times, I get the value printed out but other times I see this error
Traceback (most recent call last):
File "/home/user123/VsCode/Modbus_Rtu_sample.py", line 65, in <module>
value = result.registers
AttributeError: 'ModbusIOException' object has no attribute 'registers'
Firstly, I want to know why does this works sometimes and not other times. as I can see in the pymodbus documentation, there's no attribute called registers then how does it work the first time? this is my code:
with ModbusClient(method='rtu', port='/dev/ttyUSB0', stopbits=1, bytesize=8 , parity='N', baudrate=9600, timeout=2) as client:
if not client.connect():
connection = client.connect()
connection = client.connect()
print("Your connection is set to",connection)
if client.connect():
read=client.read_holding_registers(address = 0x1000,count =4,unit=1)
value = read.registers
print(value)
#print(read.registers)
else:
print("Cannot connect to modbus slave")
client.close()
Is there any better lib/method/way to overcome this issue? all I want is to have stable read / write outputs for registers. later I want to call a class made from this so don't want to have a situation where sometimes I do get output and other times an error is thrown.

Bluetooth address is already in use - Raspberry pi

I want to connect my phone with the raspberry pi via bluetooth and for that, I'm using this code
from bluedot.btcomm import BluetoothServer
from signal import pause
def data_received(data):
print(data)
s.send(data)
s = BluetoothServer(data_received)
pause()
Sometimes It's working fine I'm able to connect my phone and send data but some times its shows error like
Bluetooth address B8:27:EB:DA:25:25 is already in use - is the server already running?
Traceback (most recent call last):
File "mypyscript/mainscript.py", line 8, in s = BluetoothServer(data_received)
File "/home/pi/.local/lib/python2.7/site-packages/bluedot/btcomm.py", line 358, in start
raise e
socket.error: [Errno 98] Address already in use
I copied this code from here

LIBUSB_ERROR_BUSY [-6] Sending a basic packet with Python 3.9 using LibUSB1

I am developing some software in python 3.9 and I am at the point where I have a device connected to my USB port and would like to send a basic packet to test the interface before I proceed. I am using this example to try and get my interface to work. I am not bothered about speed or byte count. I would like to see any response on the interface (But on reflection Im wondering if usb speed could be the issue):
import usb1
import usb.util
import os
import sys
import libusb
import usb.core
from usb import util
import math
dev = usb.core.find(idVendor=0x11ac,idProduct=0x317d)
with usb1.USBContext() as context:
handle=context.openByVendorIDAndProductID(
0x11ac,
0x317d,)
handle.claimInterface(0)
handle.setInterface(0)
data = bytearray(b"\\xf0\\x0f"* (int(math.ceil(0xb5db91/4.0))))
handle.controlWrite(0x40, 0xb0, 0xb5A6, 0xdb91, b"")
handle.bulkWrite(2,data,timeout=5000)
`
https://github.com/vpelletier/python-libusb1/issues/21
I have had a look in various forums for several days and cannot seem to get an answer that works. Here is the trace: Its worth noting that from time to time, this py file does run without error but does nothing, and I see no traffic traveling to the USB interface.
Can someone please help me configure a working example of how to send a packet to the interface? I have tried various things like detaching the kernel, setting configuration, etc.
For 4 days I have struggled with libusb01 & 10, after discovering libusb1, I have changed my wrapper and got a lot more success
I also see a lot of examples in forums like this one, and I always get the same response. Also Im curious as to where it appears that 0x40 is the endpoint(out)
Traceback (most recent call last):
File "/home/jbgilbert/Desktop/Packets/Backend_replace.py", line 16, in <module>
handle.claimInterface(0)
File "/usr/lib/python3/dist-packages/usb1/__init__.py", line 1213, in claimInterface
libusb1.libusb_claim_interface(self.__handle, interface),
File "/usr/lib/python3/dist-packages/usb1/__init__.py", line 133, in mayRaiseUSBError
__raiseUSBError(value)
File "/usr/lib/python3/dist-packages/usb1/__init__.py", line 125, in raiseUSBError
raise __STATUS_TO_EXCEPTION_DICT.get(value, __USBError)(value)
usb1.USBErrorBusy: LIBUSB_ERROR_BUSY [-6]
My device is a laptop, using lsmod reveals all devices linked to that particular end point, in this case because if the presence of a webcam I was unable to write to an available end point. Despite disabling the driver I had no avail and had to try to code on a machine that had less onboard accessories that proved more successful

"All executable devices in the domain are busy"

When I try to run a very simple waveform from the command line, I get the error shown below. I'm running something incredibly simple, and there is no way the GPP is completely utilized. Load is .04 on a 2 core machine without the waveform running.
This is a somewhat random error. Sometimes it works, usually it does not.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/redhawk/core/lib/python/ossie/utils/redhawk/core.py", line 1793, in createApplication
app = app_factory.create(name, initConfiguration, [])
File "/usr/local/redhawk/core/lib/python/ossie/cf/cf_idl.py", line 2026, in create
return _omnipy.invoke(self, "create", _0_CF.ApplicationFactory._d_create, args)
ossie.cf.CF.CreateApplicationError: CF.ApplicationFactory.CreateApplicationError(errorNumber=CF_ENOSPC, msg="Unable to launch component 'add_constant'. All executable devices (i.e.: GPP) in the Domain are busy")
The default settings for the GPP are for a multi core machine. For limited CPU resource machines, you can change the following settings to ignore the idle setting calculations.
reservered_capacity_per_component=0
thresholds:
cpu_idle = 0

Resources