I have a lab equipment (CAEN DT1470ET) connected via USB to a Ubuntu 20.04 machine. I have installed the graphical interface software from the manufacturer and it connects properly with the instrument. However, when I try to do it with Python, I can't.
This is my code:
import serial
import time
CAEN = serial.Serial(
port = '/dev/ttyACM0',
baudrate = 9600,
parity = serial.PARITY_NONE,
stopbits = 1,
bytesize = 8,
xonxoff = True,
timeout = 1,
)
command = 'BD:0,CMD:MON,PAR:BDFREL\r\n'.encode('ASCII')
print(f'Sending: {command}')
CAEN.write(command)
time.sleep(1)
print(f'Reading...')
print(CAEN.read(111))
The problem is in the line CAEN.read
. It always goes to the timeout so it always prints b''
(i.e. empty answer), and if the timeout=1
line is removed it stays there indefinitely.
If I run udevadm info -r -q all /dev/ttyACM0
I get:
$ udevadm info -r -q all /dev/ttyACM0
P: /devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:1.0/tty/ttyACM0
N: ttyACM0
L: 0
S: serial/by-path/pci-0000:00:14.0-usb-0:1:1.0
S: serial/by-id/usb-CAEN_SPA_NIM_Desktop_HV_Power_Supply-if00
E: DEVPATH=/devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:1.0/tty/ttyACM0
E: DEVNAME=/dev/ttyACM0
E: MAJOR=166
E: MINOR=0
E: SUBSYSTEM=tty
E: USEC_INITIALIZED=17958516201
E: ID_BUS=usb
E: ID_VENDOR_ID=21e1
E: ID_MODEL_ID=0003
E: ID_PCI_CLASS_FROM_DATABASE=Serial bus controller
E: ID_PCI_SUBCLASS_FROM_DATABASE=USB controller
E: ID_PCI_INTERFACE_FROM_DATABASE=XHCI
E: ID_VENDOR_FROM_DATABASE=Intel Corporation
E: ID_VENDOR=CAEN_SPA
E: ID_VENDOR_ENC=CAEN\x20SPA
E: ID_MODEL=NIM_Desktop_HV_Power_Supply
E: ID_MODEL_ENC=NIM\x2fDesktop\x20HV\x20Power\x20Supply
E: ID_REVISION=0100
E: ID_SERIAL=CAEN_SPA_NIM_Desktop_HV_Power_Supply
E: ID_TYPE=generic
E: ID_USB_INTERFACES=:020201:0a0000:
E: ID_USB_INTERFACE_NUM=00
E: ID_USB_DRIVER=cdc_acm
E: ID_USB_CLASS_FROM_DATABASE=Communications
E: ID_PATH=pci-0000:00:14.0-usb-0:1:1.0
E: ID_PATH_TAG=pci-0000_00_14_0-usb-0_1_1_0
E: ID_MM_CANDIDATE=1
E: DEVLINKS=/dev/serial/by-path/pci-0000:00:14.0-usb-0:1:1.0 /dev/serial/by-id/usb-CAEN_SPA_NIM_Desktop_HV_Power_Supply-if00
E: TAGS=:systemd:
I have already set the permissions to the serial port with sudo chmod 666 /dev/ttyACM0
. What am I doing wrong?
Many device access problems can be resolved through group membership changes.
Specifically, if
ls -l
shows that the group permissions (the second "rwx
" triplet) is "rw
" (e.g."-rw-rw----
"), then, adding oneself to the group that owns the device will grantrw
access.Here's how:
This allows you membership in the group that can
rw
the device, but there is one more step.To make all your processes members of the new group, logout and login. Group memberships are set up at
login
time.To create a single process in the new group (for testing, prior to logout/login):
or, just type the group name. See
man newgrp
.In the end all the problem was that I was missing a
$
character in the beginning of each command, so the command to send would beI discarded syntax errors because in that case I was expecting some answer from the device saying that there is an error in the command, but it seems that for this mistake the answer is just silence and this got me confused. (Also I was unaware of the missing
$
.)