My first post and my first Pi project.
I'm experimenting with data transfer between Pi and external UART-enabled devices.
As part of this, I want to prototype simple bi-directional data transfer between onboard Pi UART and USB. Then use this understanding to prototype bi-directional data transfer with an external UART device.
Using a command-line approach (rather than code) is significant, as it helps with rapid prototyping.
I experimented with linking the onboard Pi-3 UART (GPIO 14 and 15) with one of the same Pi's USB sockets. I saw some unexpected results.
Uni-directional data transfer worked as expected and in each direction.
However, when I triggered bi-directional data transfer, both /dev/serial0 and /dev/ttyUSB0 started showing an indefinite and unexpected stream of characters.
Why the stream of extra characters?
Here's a bit more detail on what I did:
- Used raspi-config to disable serial console and enable UART for GPIO 14 & 15.
Connected pins:
- connected pins 8 (GPIO 14) and 10 (GPIO 15) to the same Pi's USB, via a USB-to-TTL adapter (from Adafruit).
- connected adapter red wire to pin 4 and black wire to pin 6.
- no other pins connected.
Fresh reboot.
Set /dev/serial0 baudrate. Checked default port configurations were identical, using
stty -F /dev/serial0 115200 stty -F /dev/serial0 -a stty -F /dev/ttyUSB0 -a
Sent test message from UART to USB:
- In console-1:
cat /dev/ttyUSB0
- In console-2:
echo -en 'Hello USB0 from serial0\r\n' >> /dev/serial0
- In console-1:
- Test message showed fine. Stopped cat with Ctrl-C.
- Sent similar test message from USB to UART:
cat /dev/serial0 echo -en 'Hello serial0 from USB0 \r\n' >> /dev/USB0
- This test message also showed fine. Stopped cat with Ctrl-C.
- Repeated test message each way a few times. All as expected.
- Then, tried sending test message while both ports were being monitored:
- In console-1:
cat /dev/ttyUSB0
- In console-2:
cat /dev/serial0
- In console-3:
echo -en 'Hello USB0 from serial0 \r\n' >> /dev/serial0
- In console-1:
- This triggered the problem. Both
cat
consoles quickly scrolled off the screen. - Saw same problem if direction reversed, i.e.:
echo -en 'Hello serial0 from USB0 \r\n' >> /dev/USB0
- Triggered the same problem again & recorded the data stream to file:
cat /dev/ttyUSB0 | tee sent-from-serial0.bin
- Hex editor showed the message being sent as expected. Then an indefinite mix of the same message repeated with 0A (newline) bytes:
$ hexedit sent-from-serial0.bin
00000000 48 65 6C 6C 6F 20 55 53 42 30 20 66 72 6F 6D 20 Hello USB0 from
00000010 73 65 72 69 61 6C 30 0A 0A 0A 48 65 6C 6C 6F 20 serial0...Hello
00000020 55 53 42 30 20 66 72 6F 6D 20 73 65 72 69 61 6C USB0 from serial
00000030 30 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 48 65 6C 0............Hel
00000040 6C 6F 20 55 53 42 30 20 66 72 6F 6D 20 73 65 72 lo USB0 from ser
00000050 69 61 6C 30 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A ial0............
00000060 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A ................
00000070 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A ................
00000080 0A 0A 0A 0A 48 65 6C 6C 6F 20 55 53 42 30 20 66 ....Hello USB0 f
00000090 72 6F 6D 20 73 65 72 69 61 6C 30 0A 0A 0A 0A 0A rom serial0.....
000000A0 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A ................
000000B0 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A ................
000000C0 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A ................
000000D0 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A ................
000000E0 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A ................
000000F0 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A ................
- Checked the other stream. It unexpectedly included the same message. It also included a similar mix of repeated message and 0A (newline) bytes:
$ hexedit sent-from-ttyUSB0.bin
00000000 48 65 6C 6C 6F 20 55 53 42 30 20 66 72 6F 6D 20 Hello USB0 from
00000010 73 65 72 69 61 6C 30 0A 0A 0A 0A 0A 0A 48 65 6C serial0......Hel
00000020 6C 6F 20 55 53 42 30 20 66 72 6F 6D 20 73 65 72 lo USB0 from ser
00000030 69 61 6C 30 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A ial0............
00000040 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 48 65 6C 6C ............Hell
00000050 6F 20 55 53 42 30 20 66 72 6F 6D 20 73 65 72 69 o USB0 from seri
00000060 61 6C 30 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A al0.............
00000070 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A ................
00000080 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A ................
00000090 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A ................
000000A0 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A ................
000000B0 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A ................
000000C0 0A 0A 0A 48 65 6C 6C 6F 20 55 53 42 30 20 66 72 ...Hello USB0 fr
000000D0 6F 6D 20 73 65 72 69 61 6C 30 0A 0A 0A 0A 0A 0A om serial0......
000000E0 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A ................
000000F0 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A ................
- Switched back to sending the test message down one line only & recorded the same data. It looked mostly correct. Noticed "\r\n" was not translated to the expected 0A 0D. Also noticed an unexpected extra 0A byte at the end:
00000000 48 65 6C 6C 6F 20 55 53 42 30 20 66 72 6F 6D 20 Hello USB0 from
00000010 73 65 72 69 61 6C 30 0A 0A 0A serial0...
00000020
I expected to see each device line operating independently, i.e. a TX from UART - USB could run both concurrently with & independently of a TX from USB - UART.
Why is UART-USB link inserting many unexpected bytes when using cat /dev/serial0 concurrently with cat dev/ttyUSB0?