'Setting a default "SERIAL PORT" for a serial port device in "LINUX"
I am Using a Python to access a serial port from ubuntu 20.4. I send and read data from the Connected serial port.
The code looks like :
import time
import serial
ser=serial.Serial("/dev/ttyACM0",115200)
ser.write(b'\xFA\x0B\x1B\x00\x00\x00\x00\x00\x00\xFB')
while True:
try:
#ser.write(command)
s2=ser.read(10).hex()
print(s2)
except KeyboardInterrupt:
ser.flushInput()
ser.flushOutput()
ser.close()
break
The issue is, whenever I try resetting the hardware device the "COM Port" Jumps to a new "port". So I need to manually change the port in the code frequently.
Is there a way to set a default "COM PORT" in Ubuntu for a serial port device (or) is there a way to automatically detect the "COM PORT" by my program!?
The thing I tried is:
import serial.tools.list_ports
import serial
print('Searching for ports...')
ports=serial.tools.list_ports.comports(include_links=False)
for port in ports:
if port.device=="/dev/ttyACM0" or port.device=="/dev/ttyACM1" or port.device=="/dev/ttyACM2":
ser=serial.Serial(port.device)
break
if ser.isOpen():
ser.close()
ser=serial.Serial(port.device,115200)
ser.flushInput()
ser.flushOutput()
print('Connected to :'+ser.name)
But this code doesn't provide the required output. The code sometimes doesn't select the correct "COM PORT".
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
