Jetson AGX Orin: tty device usable only once before failing - barcode-scanner

I am using a barcode scanner as part of a project, everything works correctly until I exit the program then I can't communicate anymore with the barcode scanner. This holds true for whatever program I'm running, be it one of my own or just using screen to monitor the transmissions. As soon as I exit, the only way to make the scanner work again is to unplug and replug.
The scanner (this one) is always mounted correctly (usually at /dev/ttyACM0) and communicates by SSI over USB CDC.
I’ve tried monitoring with pyserial’s miniterm and with screen /dev/ttyACM0 9600 but the same problem arises (f.e. screen just says [screen is terminating])
Mind you, everything works well on another computer so I believe it might be an issue with the Jetson rather than the scanner.
In the project I’m trying to run, I use pyserial to interact with the device. Here is an extract of the code to give you an idea of how I use it:
import serial
serial_port = "/dev/ttyACM0"
baud_rate = 9600
with serial.Serial(serial_port, baud_rate, timeout=0.1) as device_serial:
device_serial.flush()
while True:
try:
# read a line from the serial port
barcode_byte_string = device_serial.readline()
if len(barcode_byte_string) > 0:
# convert the byte string to a string and strip the newline character
barcode = barcode_byte_string.decode("utf-8").rstrip()
# publish the barcode to the topic
self.publish_barcode(barcode, serial_port)
except serial.SerialException as e:
# exit with error code 1. This will cause the application to be restarted.
sys.exit(1)
except Exception as e:
break

Related

Program Stops Without Reason (RasPi, Linux, Python3)

First, thank for fixing my post. I'm still not sure how to include a sketch. I've been reading posts here for many months, but never posted one before.
My headless RasPi is running two sketches of mine, one reads data from a pm2.5 sensor (PMS7003) and the other is the program listed above that sends information to another Pi, the client, that turns on a pm2.5 capable air filter. (I live in California) The program that reads the PMS7003 sorts the data, called max_index, into one of six categories, 0 thru 5 and saves the current category to a text file. I'm using the 'w' mode during the write operation, so there is only one character in the text file at any time. The server program listed above reads the text file and sends it to a client that turns on the air filter for categories above 2. The client sends the word "done" back to the server to end the transaction.
Until you mentioned it, I didn't realize my mistake, clientsocket.recv(2). I'll fix that and try again.
So, the listener socket should go outside the while loop, leaving the send and receive inside???
Troubleshooting: I start the two programs using nice nohup python3 xxx.py & nice nohup python3 yyy.py. The program that reads the PMS7003 continues running and updating the text file with current category, but the server program falls out of existence after a few days. top -c -u pi reveals only the PMS7003 program running, while the server program is missing. Also, there's nothing in nohup.out or in socketexceptions.txt and I tried looking through system logs in /var/log but was overwhelmed by information and found nothing that made any sense to me.
Since writing to the socketexceptions.txt file is not in a try/except block, the crash might be happening there.
import socket
import time
index = " "
clientsocket = ""
def getmaxindex():
try:
with open('/home/pi/pm25/fan.txt','r')as f:
stat = f.read() #gets max_index from pm25b.py
return(stat)
except:
with open("/home/pi/pm25/socketexceptions.txt",'a')as f:
f.write("Failed to read max index")
def setup(index):
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR,1)
s.bind(("192.168.1.70", 5050))
except:
with open("/home/pi/pm25/socketexceptions.txt",'a')as f:
f.write("Failed to bind")
try:
s.listen(1)
clientsocket, address = s.accept()
clientsocket.send(index)
rx = clientsocket.recv(2)
if rx == "done":
clientsocket.close()
except:
with open("/home/pi/pm25/socketexceptions.txt",'a')as f:
f.write("Failed to communicate with flient")
while True:
index = getmaxindex().encode('utf-8')
setup(index)
time.sleep(5)
enter code here
It is unknown what program is supposed to do and where exactly you run into problems, since there is only a code dump and no useful error description (what does "stop" mean - hang or exit, where exactly does it stop). But the following condition can never be met:
rx = clientsocket.recv(2)
if rx == "done":
The will receive at most 2 bytes (recv(2)) which is definitely not enough to store the value "done".
Apart from that it makes not real sense to recreate the same listener socket again and again, just to accept a single client and exchange some data. Instead the listener should only be created once and multiple accept should be called on the same listener socket, where each will result in a new client connection.

Python Multithreading with pynput.keyboard.listener

I am building a self-driving rc car. The car is controlled by a raspberry pi (client) that sends image data to my computer (server) and the computer processes the image frames and responds to the car with what to do (all using python sockets). This works perfectly well. I am now trying to add a key listener to python so I can manually control the car WHILE all of the socket interractions are happening. I would like to use multithreading to do so. Here is how I believe it should work:
import cv2
from pynput import keyboard
from Server import Server
###Keyboard Listener###
def keyPress(key): #send keypress to client
server.sendCommand((str(key)))
with keyboard.Listener(on_press=keyPress) as listener: #new listener thread
listener.join() #activate thread
###Server/ client interaction###
host, port = '10.78.1.195', 8000 # home
server = Server(host, port) #server object
server.connect() #connect
while server.isOpened(): #do while the server is open
frame = server.getStreamImage() #get an image from the client each frame
server.sendCommand("0") #send a command to the server (arbituary for now, but will be a neural network ouotput
cv2.imshow("t", frame) # show image
# cv2.imshow("tttttt", nnInput) # show image CONFIGURE PERSPECTIVE TRANSFORM AFTER CAMERA MOUNT
if cv2.waitKey(1) == ord('q'): #quit if q pressed
server.sendCommand('q') # tell client to close
server.close() # break if 'q' pressed
cv2.destroyAllWindows() #close opencv windows
If you would like any of the client or server code, I would be happy to show it. I didn't inlude it because it's working perfectly.
SO, in my mind, the while loop and the keyboard listener should operate in parallel, and I'm not sure why they are not. With this configuration, the keypresses are tracked, but the server/client interactions never begin. I have tried reformatting the code and I can't seem to get the operations to happen parallel.
If there is a simpler or less resource intensive way to do this, I am open to new ideas. This is just what makes the most sense to me. I really just want the code to be as clean as possible.
My python version is 3.7. I am running Ubuntu 19.10. pynput is version 1.4.5.
If I can provide any additional info, please ask. Thank you very much!
Instead of using the with statement to initialise your listener try:
listener = keyboard.Listener(on_press=keyPress)
listener.start()
The with statement is blocking the main thread.
Using start instead of with / join you create a non-blocking thread allowing the main loop to start.

Pyserial: termios.error: (22, 'Invalid argument') while reading from virtual serial port

Is there any posibility to read from virtual serial ports without mentioned error?
I'm trying to reach data sent from symbol/zebra barcode scanner li4278. Device is working in simple com port emulation mode and it's under /dev/usb/hiddev0 as serial port.
I'm sure that emulation works fine because CuteCom can read from it, and also this simple pyton script works too:
defaultDevice = '/dev/usb/hiddev0'
inDev = open(defaultDevice, 'rb')
while True:
inBytes = inDev.read(1)
for x in inBytes:
print(x)
But anytime I'm trying to read it using pyserial with such a minimal code like this:
with serial.Serial('/dev/usb/hiddev0', timeout=1) as ser:
x = ser.read()
print(x)
the same error occurs: termios.error: (22, 'Invalid argument'). It's like it can't read from virtual serial ports.
And no, setting args to rtscts=True, dsrdtr=True doesn't work.
I have the same '22, invalid argument' error.
Linux Mint 18, 64 bit, Python 3.7, pyserial 3.4.
Anyone knows what's the thing?

Change Window upon flashdrive detection Tkinter

So far I have a GUI but right now I'm having a problem on how to change the window upon usb insertion. I tried pyudev for monitoring devices but whenever I add it in my GUI code, it doesn't run even though these codes worked when separated. Also I want to list the files in the GUI so that the user can select which file to print. Thank you in Advance! :)
This is my code for detection of usb devices.
import pyudev
context = pyudev.Context()
monitor = pyudev.Monitor.from_netlink(context)
monitor.filter_by(subsystem='usb')
for device in iter(monitor.poll, None):
if device.action == 'add':
print('{} connected'.format(device))
if device.action == 'remove':
print('{} removed' .format(device))

Win32api for serial communication in excel VBA

I have data acquisition devices I would like to pull information from. I've started a small project in Python 3.5 using pyserial to communicate to a device. I can send commands and receive data.
import serial
ser = serial.Serial()
ser.port = 'COM1'
ser.baudrate = 9600
ser.parity = PARITY_NONE
ser.timeout=.5
ser.open()
ser.write(b'#02\r')
print(ser.readline())
ser.close()
This sends a command to retrieve data in the buffer, and when I use the readline command, I pull in data.
b'>-999999-999999-999999-999999 -999999\r'
I've created an excel sheet to host data tables and test criteria which I am judging performance of our machines on. This was initially for manual user input, but I decided I'd try and see if I can automate this directly in excel. I've poured over several webpages, found several companies that would ask for payment for code- etc. I've finally settled on work done by The Scarms which uses the WIN32API to deal with serial I/O vs. the original mscomm32.ocx driver.
I've been able to bring his files into my project, and used the sample code to start. I can send a message, and visually verify it from the device I'm communicating through, but I don't get any reply from my end data acquisition device.
strData = "#02\r"
lngSize = Len(strData)
lngStatus = CommWrite(intPortID, strData)
The variable strData is a string. When sending a message using pyserial, it's prefaced by "b" which (to my knowledge) signals it to be encoded to bits before sent through the serial port.
I've been trying to look through the modCOMM code that gets added to VBA from the code provided by the link above, but I can't seem to get an input at all. Am I sending the information incorrectly using the WIN32API?
How do I send this command over the bus properly in order to get a response from the end device?
The end device in question is an Advantech ADAM 4017+.

Resources