0

I'm using a CH341-UART Converter for sending some string data to USB port. How can I find the COM port number for connected device? For example in this program:

import serial      
ComPort = serial.Serial('COM24') # open COM24
ComPort.baudrate = 9600 
ComPort.bytesize = 8  

COM24 is my port. But how can I find the right com port number?

2

Under linux your devices have not meta-names like com1 or so. Your usb-adapter is added to the /dev-directory with a driver specific name. The most usb-uart adapter use the name /dev/ttyUSB* where the * is a number beginning at 0. The best way to get this name is to view the changes of kernel messages via dmesg before and after plugin of the adapter. You should get something like that:

usb 4-2: ch341-uart converter now attached to ttyUSB0

So you have to use '/dev/ttyUSB0' instead of com24.

But access to a device node is restricted to the root user. So you have to workaround this.

  • yes ... i find my device slug by dmesg | grep tty thank you – Arash Hatami Jan 30 '16 at 20:47
1

Many Linux distros (typically Debian and derivatives) have a /dev/serial/by-id directory. This shows the full "hardware" name of the serial USB device. Watch this directory while plugging and unplugging to identify your device.

Create a symbolic link between that full name and some short convenient name in the user's home directory. That full path (/home/me/myserial) is the "com port" to use in programs. This is stable across plug/unplug/reboot because of the symlink.

Example:

my@host:~$ ls -lA /dev/serial/by-id
total 0
lrwxrwxrwx 1 root root 13 Jun 11 07:21 usb-FTDI_FT232R_USB_UART_ANZ1SMDF-if00-port0 -> ../../ttyUSB2

my@host:~$ ln -s /dev/serial/by-id/usb-FTDI_FT232R_USB_UART_ANZ1SMDF-if00-port0 /home/me/myserial

This creates a symbolic link : /home/me/myserial that points to the USB/Serial converter whenever it is plugged into the host.

  • One other noteworthy thing: The user name should be in the dialout group to avoid having to be root to use the serial port. – guitarpicva Jun 25 '18 at 12:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.