Page 1 of 1
P3B+, Get the USB port name
Posted: Mon Jul 02, 2018 11:05 am
by Shawn1913
Hi all,
I'm developing an application which communicating with many
RS232-Communicable external devices, so I have to use a
USB HUB to extend Pi's limited 4 USB ports, and then use USB-TO-RS232 converter to connect to external devices.
My application can enumerates all available USB serial port which like:
Code: Select all
/dev/ttyUSB0, /dev/ttyUSB1... /dev/ttyUSB*
My question is, How I can tell the
relationship of the above name which used in program code (/dev/ttyUSB0) to a physical USB Port, for example, when I plug in an external device into one of PI's USB serial port, I can quickly know this port's name, and can use it in my application.
Re: P3B+, Get the USB port name
Posted: Mon Jul 02, 2018 11:14 am
by DougieLawson
Do your devices have a unique serial (or other uniquely identifying data)?
Take a look with lsusb -v | grep -i 'serial'. If they have that you can use a udev rule to give each device a known name (possibly, based on the serial number).
Re: P3B+, Get the USB port name
Posted: Mon Jul 02, 2018 11:29 am
by epoch1970
If you are actually interested in the port the device is plugged in:
If you look at a device with udevadm, you will see its device path corresponds to a physical port on the Pi. It's a bit obscure and the path is long but if you connect repeatedly a device from one port to another you can see where the difference is.
That difference is repeatable, so your next step is usually to write a udev rule that catches "your USB adapter added to port X" and create an alias with a meaningful name in /dev/.
You can create directories if you want to, so you could end up with things like this:
- /dev/ttyUSB0 -> /dev/mydevice_lower_left, or
- /dev/ttyUSB1 -> /dev/mydevices/upper/left/ttyUSB1
Then in your program, no "/dev/mydevices/" means nothing of interest is connected, and otherwise you know what is connected where.
Re: P3B+, Get the USB port name
Posted: Tue Dec 31, 2019 8:06 am
by ayavilevich
I found that the devices are available by their path via: /dev/serial/by-path/
Re: P3B+, Get the USB port name
Posted: Fri Jul 10, 2020 2:32 pm
by WvZ
Good day
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. The script writer for a pi 3+ had "/Dev/ttyUSB0' but that does not work with me.
I noticed by plugging in and out that when the USB is plugged in a folder called 'serial' is created with two folders namely, by-id and by-path. I also get a file in the /Dev folder called 'ttyACM0'. If i open by-id and select properties - it says target file ../../ttyACM0.
If i use ttyACM0 I'm getting faults from my script. Can someone help me identify what i must type into the port = ??? part of my script:
client = ModbusClient(method='rtu', port='
/dev/ttyUSB0', baudrate=115200)'
If it helps, I'm trying to get my Pi Zero W to talk to my Epsolar Tracer solar charge controller to log solar data. (see website link:
http://www.solarpoweredhome.co.uk/)
Thank you in advanced for any help.
Re: P3B+, Get the USB port name
Posted: Sat Jul 11, 2020 2:04 pm
by hippy
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]
Re: P3B+, Get the USB port name
Posted: Sat Jul 11, 2020 2:13 pm
by WvZ
Hi, Hippy.
Thank you for this. Where do I type in Isusb? In a terminal command?
Also, how do I run the program you supplied? Must I copy paste and save it as a py file and then run it in terminal?
Thanks, again, for your help. Much appreciated!
Re: P3B+, Get the USB port name
Posted: Sat Jul 11, 2020 2:21 pm
by hippy
WvZ wrote: ↑Sat Jul 11, 2020 2:13 pm
Thank you for this. Where do I type in Isusb? In a terminal command?
Also, how do I run the program you supplied? Must I copy paste and save it as a py file and then run it in terminal?
Yes, to both.
Re: P3B+, Get the USB port name
Posted: Sat Jul 11, 2020 2:24 pm
by hippy
hippy wrote: ↑Sat Jul 11, 2020 2:04 pm
WvZ wrote: ↑Fri Jul 10, 2020 2:32 pm
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
Just noticed that's /tty
ACM0 not the /tty
AMA0 my brain converted it to so, /dev/ttyACM0 is probably what you need to use.
Perhaps detail the faults you get when you try to use that.
Re: P3B+, Get the USB port name
Posted: Mon Jul 13, 2020 7:01 am
by WvZ
It worked - it shows that my serial USB device is /dev/ttyACM0
It seems to be connecting. Now I need to figure out why it is saying
solarVoltage = float(result.registers[0]/100.0]
AttributeError: 'ModbusIOException' object has no attribute registers. I know the code is fine as it works perfectly on pycharm on my windows laptop. I'll do some digging. Again, thank you so much for your help. It is much appreciated.
Re: P3B+, Get the USB port name
Posted: Mon Jul 13, 2020 7:48 am
by WvZ
I've seen this error is likely due to incorrect driver on Pi. Github seems to be offline at the moment but I'll follow those instructions to hopefully get my project up and running.
Re: P3B+, Get the USB port name
Posted: Mon Jul 13, 2020 9:40 am
by hippy
WvZ wrote: ↑Mon Jul 13, 2020 7:01 am
solarVoltage = float(result.registers[0]/100.0]
AttributeError: 'ModbusIOException' object has no attribute registers.
Looks like 'result' is getting a 'ModbusIOException' object put in it rather than the anticipated data list. That suggests something went wrong in the routine which returned the result list.
Just before your "solarVoltage=float(...)" line you can add a "print(result)" which might help reveal what the error relates to.
Re: P3B+, Get the USB port name
Posted: Tue Jul 14, 2020 1:24 pm
by WvZ
I've got it up and running!! If I could please bother you for a new problem I'm encountering.
I needed to install the driver for the epsolar USB serial connection. In terminal I input git clone
https://github.com/kasbert/epsolar-tracer.git
This put the downloaded folder into /home/pi/epsolar-tracer/xr_usb_serial_common-1a. I then used 'make' and 'insmod ./xr_usb_serial_common.ko'
Then I needed to remove the CDC-ACM driver by following:
# rmmod cdc-acm
# modprobe -r usbserial
# modprobe usbserial
# insmod ./xr_usb_serial_common.ko
I then needed to blacklist the ttyACM0 as it kept reappearing after reboot. I changed the permissions so I could execute sudo echo blacklist cdc-acm > /etc/modprobe.d/blacklist-cdc-acm.conf. After reboot, the ttyACM0 is correctly not showing but I need to cd back into the /home/pi/epsolar-tracer/xr_usb_serial_common-1a folder and re-run the insmod ./xr_usb_serial_common.ko command.
Is this because of where the folder and .ko file are located? If so, can I simply cut and paste it? If so, where must I paste it to (perhaps /dev/?). If I cannot simply cut and paste it, please could you help me with the commands to correctly move it.
If the problem is something else entirely, any help would be greatly appreciated.
Thank you, again. I really appreciate your help!
Re: P3B+, Get the USB port name
Posted: Tue Jul 14, 2020 2:11 pm
by hippy
I'm not familiar with keeping modules in place across re-boots but you can probably install it every start-up by inserting the command in '/etc/rc.local' before the ''exit 0' -
Code: Select all
insmod /home/pi/epsolar-tracer/xr_usb_serial_common-1a/xr_usb_serial_common.ko
It is likely there are 'udev' and other solutions but I know even less about those.
Re: P3B+, Get the USB port name
Posted: Tue Jul 14, 2020 4:52 pm
by WvZ
Thank you - this worked. Well, I was a bit scared of messing with that file (rc.local scares me) so I used crontab -e and an @ reboot statement to run the command on startup.
The script is correctly starting my solarlogging.py script (also done from crontab -e), however it does not open a terminal window. I think this is handy as I do VNC into the pi from time to time and would like it if the terminal window was open and printing the info there as well as writing to CSV file (which it does in the background). This is not a biggy but thought it worth asking as some preliminary googling didn't specifically answer this question. I saw examples of one terminal talking to another terminal window and printing there. Perhaps that is the solution?
One other thing I noticed is that it seems the script sometimes writes to csv and sometimes does not. Is there anything I am supposed to add to the python script (?Bash?) so that when opened by crontab -e it reliably writes to csv?
Thanks, All. I really appreciate all the help I've been getting (and so speedily). Definitely 5-Stars for raspberrypi.org/forums!!