Please help! Not able to read from a USB multimeter - python

I am still quite new to USB protocol, and I need help extracting data from a digital multimeter using a USB connection and Python on my Raspberry Pi. Here I have a very basic Python script for reading from a generic USB device, and note that the idVendor and idProduct variables are correct for the multimeter I am using:
import usb.core
dev=usb.core.find(idVendor=0x067b,idProduct=0x2303)
ep=dev[0].interfaces()[0].endpoints()[0]
i=dev[0].interfaces()[0].bInterfaceNumber
dev.reset()
if dev.is_kernal_driver_active(i):
dev.detach_kernal_driver(i)
dev.set_configuration()
eaddr=ep.bEndpointAddress
r=dev.read(eaddr,1024)
print(len(r))
When I try to execute the script while the multimeter is reading a voltage, I receive the following error message:
Traceback (most recent call last):
File "usbRead.py", line 6, in <module>
dev.reset()
File "/home/pi/.local/lib/python2.7/site-packages/usb/core.py", line 949, in reset
self._ctx.managed_open()
File "/home/pi/.local/lib/python2.7/site-packages/usb/core.py", line 113, in wrapper
return f(self, *args, **kwargs)
File "/home/pi/.local/lib/python2.7/site-packages/usb/core.py", line 131, in managed_open
self.handle = self.backend.open_device(self.dev)
File "/home/pi/.local/lib/python2.7/site-packages/usb/backend/libusb1.py", line 804, in open_device
return _DeviceHandle(dev)
File "/home/pi/.local/lib/python2.7/site-packages/usb/backend/libusb1.py", line 652, in __init__
_check(_lib.libusb_open(self.devid, byref(self.handle)))
File "/home/pi/.local/lib/python2.7/site-packages/usb/backend/libusb1.py", line 604, in _check
raise USBError(_strerror(ret), ret, _libusb_errno[ret])
usb.core.USBError: [Errno 13] Access denied (insufficient permissions)
------------------
(program exited with code: 1)
Press return to continue
My apologies if this is an amateur question, but I've been trying my best to solve the insufficient permissions problem, but every solution I could find is filled with jargon that I'm hopeless in understanding. There was one thread that discussed changing things in a "rule file", and when I tried that, I ended up completely bricking the Raspberry Pi I was using!
Please help a novice like me on how I can be able to get data from this USB multimeter. Thank you so much in advance! I can answer questions for additional information as best I can.

Related

VI_ERROR_TMO (-1073807339) on Anritsu OSA

I'm working on interfacing an old Optical Spectrum Analyzer Anritsu MS9710B with a RS232 connection. A year ago, I managed to comunicate with him, send SCPI command using pyvisa and receive data. Today, I execute the exact same code with libraries up to date and I get an error Timeout when I query IDN or anything. The RS232-USB drivers are ok, I manage to open a communication but a query or read fail.
I changed the parameter to "RS232C" on the OSA parameters and my communication parameters are the same between OSA and program. Following advices from the forum, I tried the connection with NI-VISA where I get the same error. I tried to change timeout parameter, write_termination and read_termination but there isn't any change. The manual is very hard to understand termination value, but when it worked I didn't add anything.
I don't know anymore what to do.
Any advices and help would be warmly welcome to fix my problem !
import pyvisa
from pyvisa.constants import StopBits, Parity
rm = pyvisa.ResourceManager()
print(rm.list_resources())
my_instrument = rm.open_resource('ASRL5::INSTR')
my_instrument.baud_rate=9600
my_instrument.data_bits=8
my_instrument.parity=Parity.even
my_instrument.stop_bits=StopBits.one
my_instrument.write('*IDN?')
print(my_instrument.read())```
'''
('ASRL5::INSTR',)
Traceback (most recent call last):
File "PremiereComm.py", line 26, in <module>
print(my_instrument.read())
File "C:\Program Files\Python38\lib\site-packages\pyvisa\resources\messagebased.py", line 486, in read
message = self._read_raw().decode(enco)
File "C:\Program Files\Python38\lib\site-packages\pyvisa\resources\messagebased.py", line 442, in _read_raw
chunk, status = self.visalib.read(self.session, size)
File "C:\Program Files\Python38\lib\site-packages\pyvisa\ctwrapper\functions.py", line 2337, in read
ret = library.viRead(session, buffer, count, byref(return_count))
File "C:\Program Files\Python38\lib\site-packages\pyvisa\ctwrapper\highlevel.py", line 222, in _return_handler
return self.handle_return_value(session, ret_value) # type: ignore
File "C:\Program Files\Python38\lib\site-packages\pyvisa\highlevel.py", line 251, in handle_return_value
raise errors.VisaIOError(rv)
pyvisa.errors.VisaIOError: VI_ERROR_TMO (-1073807339): Timeout expired before operation completed.
>>> '''

Timeout error when reading from USB Multimeter

I need to extract data from a digital multimeter using a USB connection and Python on my Raspberry Pi. Here I have a very basic Python script for reading from a generic USB device, and note that the idVendor and idProduct variables are correct for the multimeter I am using:
import usb.core
dev=usb.core.find(idVendor=0x067b,idProduct=0x2303)
ep=dev[0].interfaces()[0].endpoints()[0]
i=dev[0].interfaces()[0].bInterfaceNumber
dev.reset()
if dev.is_kernel_driver_active(i):
dev.detach_kernel_driver(i)
dev.set_configuration()
eaddr=ep.bEndpointAddress
r=dev.read(eaddr,1024)
print(len(r))
I have a rules file in the path /etc/udev/rules.d/99-usbftdi.rules. It has the following:
SUBSYSTEM=="usb", ATTRS{idVendor}=="0x067b", MODE="0666"
When I try to execute the file while I have the multimeter turned on connected to my Raspberry Pi, I get the following error message:
Traceback (most recent call last):
File "usbRead.py", line 14, in <module>
r=dev.read(eaddr,1024)
File "/home/pi/.local/lib/python2.7/site-packages/usb/core.py", line 1024, in read
self.__get_timeout(timeout))
File "/home/pi/.local/lib/python2.7/site-packages/usb/backend/libusb1.py", line 869, in intr_read
timeout)
File "/home/pi/.local/lib/python2.7/site-packages/usb/backend/libusb1.py", line 954, in __read
_check(retval)
File "/home/pi/.local/lib/python2.7/site-packages/usb/backend/libusb1.py", line 602, in _check
raise USBTimeoutError(_strerror(ret), ret, _libusb_errno[ret])
usb.core.USBTimeoutError: [Errno 110] Operation timed out
------------------
(program exited with code: 1)
Press return to continue

pygatt NotificationTimeout: None

I try to read characteristic data out of an BLE based Sensor. Therefore i use the pygatt module
import pygatt
adapter = pygatt.GATTToolBackend()
try:
adapter.start()
device = adapter.connect("2B:01:56:6C:F4:E6")
value= device.char_read("00007502-0000-1000-8000-00805f9b34fb")
finally:
adapter.stop()
Everytime i try to run this in my Linux console (with Python 2.7.16) i get an error :
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/pi/.local/lib/python2.7/site-packages/pygatt/backends/gatttool/device.py", line 17, in wrapper
return func(self, *args, **kwargs)
File "/home/pi/.local/lib/python2.7/site-packages/pygatt/backends/gatttool/device.py", line 40, in char_read
return self._backend.char_read(self, uuid, *args, **kwargs)
File "/home/pi/.local/lib/python2.7/site-packages/pygatt/backends/gatttool/gatttool.py", line 50, in wrapper
return func(self, *args, **kwargs)
File "/home/pi/.local/lib/python2.7/site-packages/pygatt/backends/gatttool/gatttool.py", line 593, in char_read
self.sendline('char-read-uuid %s' % uuid)
File "/usr/lib/python2.7/contextlib.py", line 24, in __exit__
self.gen.next()
File "/home/pi/.local/lib/python2.7/site-packages/pygatt/backends/gatttool/gatttool.py", line 191, in event
self.wait(event, timeout)
File "/home/pi/.local/lib/python2.7/site-packages/pygatt/backends/gatttool/gatttool.py", line 157, in wait
raise NotificationTimeout()
pygatt.exceptions.NotificationTimeout: None
I am completely lost on this error. Do you know how to fix this ?
Any help on this much appreciated. Thanks in advance...
Since I was facing the same problem and nobody out there seemed to have this problem...
Well, the problem was that the requested UUID characteristic didn't exist/was invalid. Make sure that you've typed it right or that you haven't clipped some part off when copying from putty or similar (Totally not speaking from experience! Of course!)
If you are pasting a characteristic UUID from the internet or a similar device, maybe your device don't have this UUID.
OR
This could (not tested, but should make sense) also happen if you write to an UUID which is non-writeable (read-only/subscribe-only).
If you're sure that everything is right and that you have triple-checked the UUID with a dedicated sniffer or simply a BLE Scanner on your Phone, then the device is probably closing the connection too fast.
TROUBLESHOOTING:
You can manually connect with the device and issue commands using:
gatttool -I
When you are inside the program, type:
connect aa:bb:cc:dd:ee:ff
And then, before the connection closes:
char-read-uuid xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
and analyse what happens. If it works there, it should also work in python.

How to solve "control message failed, win error: A device which does not exist was specified" error PyUSB

I'm trying to connect a POS receipt printer to python and have been using python-escpos to do so. I'm running windows 10.
My code in my file 'print.py' is:
from escpos.printer import Usb
p = Usb(0x0456,0x0808)
p.text('Hello World\n')
but when i run it i get the following error log:
Traceback (most recent call last):
File "C:\Users\Harry\AppData\Local\Programs\Python\Python36-32\lib\site-packages\usb\core.py", line 223, in get_interface_and_endpoint
return self._ep_info[endpoint_address]
KeyError: 1
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "print.py", line 5, in <module>
p.text('Hello World\n')
File "C:\Users\Harry\AppData\Local\Programs\Python\Python36-32\lib\site-packages\escpos\escpos.py", line 437, in text
self._raw(txt.encode())
File "C:\Users\Harry\AppData\Local\Programs\Python\Python36-32\lib\site-packages\escpos\printer.py", line 73, in _raw
self.device.write(self.out_ep, msg, self.timeout)
File "C:\Users\Harry\AppData\Local\Programs\Python\Python36-32\lib\site-packages\usb\core.py", line 940, in write
intf, ep = self._ctx.setup_request(self, endpoint)
File "C:\Users\Harry\AppData\Local\Programs\Python\Python36-32\lib\site-packages\usb\core.py", line 102, in wrapper
return f(self, *args, **kwargs)
File "C:\Users\Harry\AppData\Local\Programs\Python\Python36-32\lib\site-packages\usb\core.py", line 215, in setup_request
intf, ep = self.get_interface_and_endpoint(device, endpoint_address)
File "C:\Users\Harry\AppData\Local\Programs\Python\Python36-32\lib\site-packages\usb\core.py", line 102, in wrapper
return f(self, *args, **kwargs)
File "C:\Users\Harry\AppData\Local\Programs\Python\Python36-32\lib\site-packages\usb\core.py", line 225, in get_interface_and_endpoint
for intf in self.get_active_configuration(device):
File "C:\Users\Harry\AppData\Local\Programs\Python\Python36-32\lib\site-packages\usb\core.py", line 102, in wrapper
return f(self, *args, **kwargs)
File "C:\Users\Harry\AppData\Local\Programs\Python\Python36-32\lib\site-packages\usb\core.py", line 239, in get_active_configuration
bConfigurationValue=self.backend.get_configuration(self.handle)
File "C:\Users\Harry\AppData\Local\Programs\Python\Python36-32\lib\site-packages\usb\backend\libusb0.py", line 510, in get_configuration
100)
File "C:\Users\Harry\AppData\Local\Programs\Python\Python36-32\lib\site-packages\usb\backend\libusb0.py", line 593, in ctrl_transfer
timeout
File "C:\Users\Harry\AppData\Local\Programs\Python\Python36-32\lib\site-packages\usb\backend\libusb0.py", line 431, in _check
raise USBError(errmsg, ret)
usb.core.USBError: [Errno None] b'libusb0-dll:err [control_msg] sending control message failed, win error: A device which does not exist was specified.\r\n\n'
Does anyone know what i need to do to make this work.
I have installed libusb into my System32 and SysWOW64 folders, aswell as installing a libusb-32 driver for my printer with zadig.
The printer i'm using is a 'Hoin 58mm POS Printer'.
I would be grateful if someone could explain to me why i'm having this error and how to solve it!
Firstly, you can check this link for the complete thread.
As suggested by KM4YRI,
I ran into the same No backend available exception when trying to follow the tutorial on a Windows 7 machine. The following worked for me, per one of the StackOverflow replies:
1. Download the latest Windows binary: https://sourceforge.net/projects/libusb/files/libusb-1.0/libusb-1.0.21/libusb-1.0.21.7z/download
2. Unzip using 7zip into a temp dir
3. If on 64-bit Windows, copy `MS64\dll\libusb-1.0.dll` into `C:\windows\system32`. If on 32-bit windows, copy `MS32\dll\libusb-1.0.dll` into `C:\windows\SysWOW64`.
I've verified the above answer on Windows-10 64-bit version.
I had a tight experience with that error using at least 3 different USB Storage Devices. After hours of trials I ended up to a feeling that USB transmission errors are the cause of this error.
The USB bus will return it in a random pattern for a low quality connection as if a device remove has presumably happened.
In my scenario I was bulk writing sectors to a \\.\PhysicalDriveN device and I noticed that after a short period the connection reestablished and the file handle was still valid and going.
To overcome the error I've ended up with the following steps:
Seek To File Position;
Write Sector;
For any 433 error:
250 ms delay;
Seek To file Position, ignoring ERROR_NO_SUCH_DEVICE and ERROR_NOT_READY errors and trying for at least 1000 ms before giving up;
Read Sector, ignoring ERROR_NO_SUCH_DEVICE and ERROR_NOT_READY errors and retrying for up to 1000 ms before giving up;
Specially for my scenario: I've compared input buffer and read buffer and stop if matching;
Repeat the whole procedure up to 5 times.
So the idea here is the following: The device driver returns ERROR_NO_SUCH_DEVICE assuming a device remove scenario, but still in a pending state. As the problem is simply quality of physical connection, the link continues and the bus driver starts a renegotiation, in which case a ERROR_NOT_READY is temporarily returned. When the link is stable and negotiated, communication continues without the need to reopen the device handle.
I would recommend adding a sleep statement of a second or two in between the instantiation of the USB printer, and the attempt to print to it. e.g:
from escpos.printer import Usb
from time import sleep
p = Usb(0x0456,0x0808)
sleep(1)
p.text('Hello World\n')

"[Errno 13] Access denied" when connecting to USB

So I have been following these documentations so that I can print on my thermal printer through python.
I am running Ubuntu and have the pyusb module installed.
The printer is a Rongta RP58, and it's idVendor and idProduct were found through the lsusb command (0fe6:811e).
Just like in the instructions, I enter:
from escpos.printer import Usb
p = Usb(0x0fe6, 0x811e)
but the error appears
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/testerdell/.local/lib/python2.7/site-packages/escpos/printer.py", line 51, in __init__
self.open()
File "/home/testerdell/.local/lib/python2.7/site-packages/escpos/printer.py", line 62, in open
check_driver = self.device.is_kernel_driver_active(0)
File "/home/testerdell/.local/lib/python2.7/site-packages/usb/core.py", line 1061, in is_kernel_driver_active
self._ctx.managed_open()
File "/home/testerdell/.local/lib/python2.7/site-packages/usb/core.py", line 102, in wrapper
return f(self, *args, **kwargs)
File "/home/testerdell/.local/lib/python2.7/site-packages/usb/core.py", line 120, in managed_open
self.handle = self.backend.open_device(self.dev)
File "/home/testerdell/.local/lib/python2.7/site-packages/usb/backend/libusb1.py", line 786, in open_device
return _DeviceHandle(dev)
File "/home/testerdell/.local/lib/python2.7/site-packages/usb/backend/libusb1.py", line 643, in __init__
_check(_lib.libusb_open(self.devid, byref(self.handle)))
File "/home/testerdell/.local/lib/python2.7/site-packages/usb/backend/libusb1.py", line 595, in _check
raise USBError(_strerror(ret), ret, _libusb_errno[ret])
usb.core.USBError: [Errno 13] Access denied (insufficient permissions)
In the documentation it suggested to create a rule that grants access, but it hasn't worked for me.
sudo nano /etc/udev/rules.d/99-escpos.rules
is the command I use to edit the file, and this is what's inside
SUBSYSTEM=="usb", ATTRS{idVendor}=="0fe6", ATTRS{idProduct}=="811e", MODE="0664", GROUP="dialout"
after changing anything in that file, I run this code:
sudo service udev restart
I am not sure how else I can give USB access to python.
When using direct root access with
sudo su root
the system says "ImportError: No module named escpos.printer". I wouldn't want to enter the password for root access every single time.
Is there a problem with my udev rules, groups, mode, user permissions?
Any help is greatly appreciated! Thanks in advance :)
Change the mode from 664 to 666 in your udev rule and it will work.

Categories

Resources