XUELK-AN-008: Adding a serial port with an USB to serial converter

From DAVE Developer's Wiki
Jump to: navigation, search
Info Box
SBC Lynx-top.png Applies to SBC Lynx

History

Version Date XUELK version Notes

1.0.0

October 2018 2.0.1 First release

Introduction

SBC Lynx has two serial port connector for mutliprotocol UARTs. Sometimes, further serial port can be required for particular applications.

This application note describes how to add another serial port using standard cable like the Manhattan PL2303 USB to serial converter

USB serial converter kernel driver

XUELK kernel 2.0.1 already integrates the PL2303 driver.

Once plugged on USB host connector, the kernel driver recognizes the device

root@sbc-lynx:~# [ 7995.031735] usb 1-1: new full-speed USB device number 4 using ci_hdrc
[ 7995.196888] pl2303 1-1:1.0: pl2303 converter detected

tty

If the kernel driver is present, the kernel creates the ttyUSBx device node, e.g.

root@sbc-lynx:~# [   42.091835] usb 1-1: new full-speed USB device number 2 using ci_hdrc
[   42.259706] pl2303 1-1:1.0: pl2303 converter detected
[   42.290004] usb 1-1: pl2303 converter now attached to ttyUSB0

where ttyUSB0 is the serial port device used for the new serial port instantiated

Accessing the serial port

Here below the simple commands used for opening and using the serial port.

Once linux bootstrap is completed, insert the USB serial adapter:

root@sbc-lynx:~# [   42.091835] usb 1-1: new full-speed USB device number 2 using ci_hdrc
[   42.259706] pl2303 1-1:1.0: pl2303 converter detected
[   42.290004] usb 1-1: pl2303 converter now attached to ttyUSB0

The device node is listed:

root@sbc-lynx:~# ls -la /dev/ttyUSB0
crw-rw----    1 root     dialout   188,   0 Dec 10 03:20 /dev/ttyUSB0
root@sbc-lynx:~#

Configure the serial port parameter, e.g.

root@sbc-lynx:~# stty -F /dev/ttyUSB0 115200
root@sbc-lynx:~#

Connect the DB9 adapter to another device serial port, then you can check the correct transmission from SBC Lynx, simply echoing a string on the device:

root@sbc-lynx:~# echo "This is a serial port test transmission" > /dev/ttyUSB0
root@sbc-lynx:~#

or getting a string from the other device simply with a cat on the device (terminated with Ctrl+C):

root@sbc-lynx:~# cat /dev/ttyUSB0
This is a serial port test reception
^C
root@sbc-lynx:~#

Serial port hotplug

Disconnecting the USB serial port adapter, the kernel closes the device:

root@sbc-lynx:~# [  517.344155] usb 1-1: USB disconnect, device number 2
[  517.358084] pl2303 ttyUSB0: pl2303 converter now disconnected from ttyUSB0
[  517.368117] pl2303 1-1:1.0: device disconnected

root@sbc-lynx:~# ls -la /dev/ttyUSB0
ls: /dev/ttyUSB0: No such file or directory
root@sbc-lynx:~#

and reinserting the cable, the device will be automatically re-created:

root@sbc-lynx:~# [  555.621794] usb 1-1: new full-speed USB device number 3 using ci_hdrc
[  555.786656] pl2303 1-1:1.0: pl2303 converter detected
[  555.812561] usb 1-1: pl2303 converter now attached to ttyUSB0

It is possible to test the kernel hotplug capability with this simple script:

echo Test kernel USB hotplug
echo
while [ 1 ]
do 
   read -p "Insert the cable and press Enter" answer
   echo Test the device presence: 
   echo
   ls -la /dev/ttyUSB0
   sleep 1
   read -p "Disconnect the cable and press Enter" answer
   echo Test the device disconnection: 
   echo
   ls -la /dev/ttyUSB0
   sleep 1
done

ansd repeat the connect/disconnect many times

root@sbc-lynx:~# sh test-usb-to-serial.sh
Test kernel USB hotplug

Insert the cable and press Enter[  968.021749] usb 1-1: new full-speed USB device number 11 using ci_hdrc
[  968.186161] pl2303 1-1:1.0: pl2303 converter detected
[  968.214650] usb 1-1: pl2303 converter now attached to ttyUSB0

Test the device presence:

crw-rw----    1 root     dialout   188,   0 Dec 10 03:35 /dev/ttyUSB0
Disconnect the cable and press Enter[  972.397217] usb 1-1: USB disconnect, device number 11
[  972.407480] pl2303 ttyUSB0: pl2303 converter now disconnected from ttyUSB0
[  972.427977] pl2303 1-1:1.0: device disconnected

Test the device disconnection:

ls: /dev/ttyUSB0: No such file or directory
Insert the cable and press Enter[  976.331835] usb 1-1: new full-speed USB device number 12 using ci_hdrc
[  976.497614] pl2303 1-1:1.0: pl2303 converter detected
[  976.525595] usb 1-1: pl2303 converter now attached to ttyUSB0

Test the device presence:

crw-rw----    1 root     dialout   188,   0 Dec 10 03:35 /dev/ttyUSB0
Disconnect the cable and press Enter[  980.698156] usb 1-1: USB disconnect, device number 12
[  980.714359] pl2303 ttyUSB0: pl2303 converter now disconnected from ttyUSB0
[  980.729902] pl2303 1-1:1.0: device disconnected

Test the device disconnection:

ls: /dev/ttyUSB0: No such file or directory
Insert the cable and press Enter[  985.201808] usb 1-1: new full-speed USB device number 13 using ci_hdrc
[  985.367978] pl2303 1-1:1.0: pl2303 converter detected
[  985.395658] usb 1-1: pl2303 converter now attached to ttyUSB0

Test the device presence:

crw-rw----    1 root     dialout   188,   0 Dec 10 03:35 /dev/ttyUSB0
Disconnect the cable and press Enter[  988.074469] usb 1-1: USB disconnect, device number 13
[  988.089665] pl2303 ttyUSB0: pl2303 converter now disconnected from ttyUSB0
[  988.103128] pl2303 1-1:1.0: device disconnected

Test the device disconnection:

ls: /dev/ttyUSB0: No such file or directory
Insert the cable and press Enter^C
root@sbc-lynx:~#