2

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
  • 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
  • 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?

  • Have you enabled serial console? – Milliways May 11 at 21:54
  • serial console was disabled using raspi-config. – james-h May 11 at 23:45
1

You directly connected Pins 8 and 10 to the USB-To-TTL-Adapter? That's a bad idea that can fry your Pi. The Pi runs at 3.3V (including its UART-Pins), Uart power level is typically between 5 and 20V. You need to use a voltage translator or a board specifially designed to convert the Raspberry Pi's Uart Pins to TTL level.

This may, in fact, result in the crosstalk you observe.

| improve this answer | |
  • Thanks PMF, this is helpful. I'll try inserting a logic level adapter & go round again. – james-h May 11 at 20:10
  • Mmmm, maybe not? I used this product, which claims 3.3v on the rx & tx pins: adafruit.com/product/954 – james-h May 11 at 20:38
  • Have just checked the voltage at the RX & TX pins of the USB-TTL adapter. Reads as 3.3v. – james-h May 11 at 20:44
  • Ok, then that's a setup especially created for the purpose of connecting a Pi (or similar device). Didn't know this kind of adapter exists. I was (with a different setup) having similar issues with crosstalk, where in the end I figured out that the logic level adapter didn't work as expected. Not sure how to test that in your setup, though. Maybe try looping connections (connect RX and TX of the same end) or check that you don't get echo if no wires are connected. – PMF May 12 at 6:18
  • There's no echo with no wires connected. The setup works as expected with one line only listening for signals. I only see the echo when both devices are listening, and then either device sends a signal. – james-h May 12 at 9:46
1

Found the answer to my own question.

The Pi UART is by default set to echo mode. Hence the echo behaviour! This presumably reflects its original purpose of working as a tty console. Echo is required for normal console behaviour.

Fix is simply to reconfigure the UART by disabling the echo.

Worked as expected after I'd configured with:

stty -F /dev/serial0 -echo
| improve this answer | |

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.