I am attempting to get the printer status by writing to a printer character device under /dev , but it will not let me write to the /dev device. I have a C program that works outside of Balena, but not in my Balena container.
I have a Balena container that has an attached thermal printer… that works to print PDF files using CUPS and lp command.
However, to detect if the printer is out of paper, I cannot use CUPS, but I have to drop to a lower level… and have to read/write using the device under /dev.
But that gives me an error code “22” of “Invalid argument” when I try to write to the device using a C program.
My C program works on my non-containerized pi. But in a Balena container I get the error code 22.
This feels like a permissions problem, but my Balena container application is a single container, that means it is “privileged”, correct?
I’m pretty sure I’m using the correct character device /dev/bus/usb/001/004 , as shown by the commands below.
I am able to open the device, but when I try to write to it I get the error.
Is there some other device or permission I’m missing?
Thank you for your help.
# lsusb
...
Bus 001 Device 004: ID 0dd4:0205 Custom Engineering SPA
...
# ls -l /dev/bus/usb/001/004
crw-rw-r-- 1 root lp 189, 3 Mar 11 12:07 /dev/bus/usb/001/004
# usb-devices
...
T: Bus=01 Lev=01 Prnt=01 Port=02 Cnt=03 Dev#= 4 Spd=12 MxCh= 0
D: Ver= 2.00 Cls=00(>ifc ) Sub=00 Prot=00 MxPS=64 #Cfgs= 1
P: Vendor=0dd4 ProdID=0205 Rev=03.34
S: Manufacturer=CUSTOM Engineering S.p.A.
S: Product=VKP80III
S: SerialNumber=VKP80III PRN Num.: 0
C: #Ifs= 2 Cfg#= 1 Atr=c0 MxPwr=2mA
I: If#= 0 Alt= 0 #EPs= 2 Cls=07(print) Sub=01 Prot=02 Driver=(none)
I: If#= 1 Alt= 0 #EPs= 2 Cls=08(stor.) Sub=06 Prot=50 Driver=usb-storage
...
This is an excerpt from my C program:
fd = open("/dev/bus/usb/001/004", O_RDWR | O_EXCL);
if (fd < 0) {
printf("Failed opening device\n");
return -1;
}
result = write(fd, (const void *)"\x10\x04\x14", 3);
if (result < 0) {
printf("Failed writing printer status command: line %d\n", __LINE__);
printf("errno: %d\n", errno);
perror("Error");
close(fd);
return -1;
}
The write() fails with this output from errno and perror()
Failed writing printer status command: line 91
Errno: 22
Error: Invalid argument