My pimatic is running on a VirtualBox virtual machine on debian. Recently I added a Arduino Nano with NanoCUL firmware. It was totaly random what serial device would be connected to ttyUSB0 and ttyUSB1.
There are several solutions to make symlink to the correct device with udev, but none of them work for my situation.
ProductID , VendorID, serialnumber all are the same for every device, even the USB ports would be different every know and then.
Eventually I wrote a python script that would talk to the serial to find out what firmware is loaded. When the script is starte it will (in my case) create 2 symlinks in the /dev directory:
NanoCUL -> /dev/ttyUSB0
ESPimaticRF -> /dev/ttyUSB1
Perhaps someons with the same issues could have benefits with this script:
import glob
import serial
import time
import os
os.chdir("/dev")
devices = [
# format: ["name for the device", baudrate, "command to send", "expected answer"]
["ESPimaticRF", 115200, "PING", "ERR unknown_command"],
["NanoCUL", 38400, "V", "nanoCUL868"]
]
def TestSerial (port, name, baudrate, send, reply):
ser = serial.Serial(port)
ser.baudrate = baudrate # set Baud rate to 9600
ser.bytesize = 8 # Number of data bits = 8
ser.parity = 'N' # No parity
ser.stopbits = 1 # Number of Stop bits = 1
ser.timeout = 5 # timeout 5 seconds
# Must given Arduino time to rest.
# Any time less than this does not seem to work...
time.sleep(1.5)
print("testing port: " + str(port) + " with baudrate: " + str(baudrate) + " for name: " + name)
ser.write(send+"\r\n")
c = ser.readline()
ser.close()
if c.find(reply) >= 0:
print(name + " gevonden op port " + port)
return True
else:
return False
i = 0
while i < len(devices):
found = False
for file in glob.glob("ttyUSB*"):
if found == False:
print("start loop voor: "+ file)
found = TestSerial(file, devices[i][0], devices[i][1], devices[i][2], devices[i][3])
if found == True:
os.symlink("/dev/" + file, "/dev/" + devices[i][0])
print("maak symlink op " + file + " naar " + devices[i][0])
else:
print(devices[i][0] + " al gevonden, skip port")
i += 1
When using this script, keep in mind to put correct values for your own situation into the devices array.

