WvZ wrote: ↑Fri Jul 10, 2020 2:32 pm
I have a RS845 to USB adapter and the USB is plugged into my Pi ZeroW. I am trying to find out what the port is called so that I can use it in a pymodbus script.
I also get a file in the /Dev folder called 'ttyACM0'.
/dev/ttyAMA0 is normally a device which is exposed on the Pi's 40-way connector so should be present regardless of whether USB is connected or not, would not normally be your USB device.
You can start with 'lsusb' which will show what devices are connected to USB.
You can then 'ls /dev' and look to see what changes happen when your USB device is inserted or removed. Or you can run a program to do all that for you -
changes.py - Python 2 and Python 3 compatible
Code: Select all
#!/usr/bin/python
import os
import time
def GetUsbList():
return os.popen("lsusb").read().strip().split("\n")
def GetDevList():
return os.listdir("/dev")
def Changed(old,now):
add = []
rem = []
for this in now:
if not this in old:
add.append(this)
for this in old:
if not this in now:
rem.append(this)
return add, rem
try:
print("Monitoring for USB changes and changes in /dev directory")
usbOld = GetUsbList()
devOld = GetDevList()
while True:
time.sleep(1)
usbNow = GetUsbList()
devNow = GetDevList()
usbAdd, usbRem = Changed(usbOld,usbNow)
devAdd, devRem = Changed(devOld,devNow)
if len(usbAdd) + len(usbRem) + len(devAdd) + len(devRem) > 0:
print("-------------------")
t = time.strftime("%Y-%m-%d %H:%M:%S - ")
for this in usbAdd : print(t + "Added : " + this)
for this in usbRem : print(t + "Removed : " + this)
for this in devAdd : print(t + "Added : /dev/" + this)
for this in devRem : print(t + "Removed : /dev/" + this)
usbOld = usbNow
devOld = devNow
except KeyboardInterrupt:
print("")
Then, for example when plugging in then removing a HL340 USB-to-RS232 cable ...
Code: Select all
pi@Pi4B:~/tmp $ python changes.py
Monitoring for USB changes and changes in /dev directory
-------------------
2020-07-11 14:58:28 - Added : Bus 001 Device 013: ID 1a86:7523 QinHeng Electronics HL-340 USB-Serial adapter
2020-07-11 14:58:28 - Added : /dev/ttyUSB0
-------------------
2020-07-11 14:58:29 - Added : /dev/serial
-------------------
2020-07-11 14:58:37 - Removed : Bus 001 Device 013: ID 1a86:7523 QinHeng Electronics HL-340 USB-Serial adapter
2020-07-11 14:58:37 - Removed : /dev/serial
2020-07-11 14:58:37 - Removed : /dev/ttyUSB0
Note that if a device does not have a driver it may appear in 'lsusb' but not create any /dev device. For example, for an FTDI serial cable which doesn't have standard FTDI VID/PID ...
Code: Select all
pi@Pi4B:~/tmp $ python changes.py
Monitoring for USB changes and changes in /dev directory
-------------------
2020-07-11 15:00:34 - Added : Bus 001 Device 014: ID 0403:bd90 Future Technology Devices International, Ltd PICAXE Download Cable [AXE027]
-------------------
2020-07-11 15:00:39 - Removed : Bus 001 Device 014: ID 0403:bd90 Future Technology Devices International, Ltd PICAXE Download Cable [AXE027]