my problem is that when I run the test:

#!/usr/bin/env python3
# Will also work on Python2.
# Serial port testing

from __future__ IMPORT print_function
import serial

test_string = "Testing 1 2 3 4".encode('utf-8')
# test_string = b"Testing 1 2 3 4" ### Will also work

port_list = ["/dev/serial0", "/dev/ttyS0"]

for port in port_list:

        serialPort = serial.Serial(port, 115200, timeout = 2)
        print("Opened port", port, "for testing:")
        bytes_sent = serialPort.write(test_string)
        print ("Sent", bytes_sent, "bytes")
        loopback = serialPort.read(bytes_sent)
        if loopback == test_string:
            print ("Received", len(loopback), "valid bytes, Serial port", port, "working \n")
            print ("Received incorrect data", loopback, "over Serial port", port, "loopback\n")
    except IOError:
        print ("Failed at", port, "\n")

I have this:

Opened port /dev/serial0 for testing:
Sent 15 bytes
Received incorrect data over Serial port /dev/serial0

Opened port /dev/ttyS0 for testing:
Sent 15 bytes
Received incorrect data over Serial port /dev/ttyS0

Someone can help me ? Thanks

P.S. when run minicom it seems blocked. I can't write commands.

  • Could you please take a minute to fix the formatting? Nov 10 '16 at 11:46
  • Are you expecting data you write to the serial port to come back as input when you read from it?
    – Mark Smith
    Nov 10 '16 at 11:52
  • Yes. It's very strange. I'm changed baud rate by 115200 to 9600 but it's the same. Nov 10 '16 at 11:56
  • Have you put a wire between the transmit pin (8) and the receive pin (10)?
    – joan
    Nov 10 '16 at 11:59
  • 1
    okay. So you have a loopback. Help us help you. Could you edit your post and include a print of loopback so we can see what was received.
    – joan
    Nov 10 '16 at 13:01

Since you do not mention any special hardware setup, I assume you are using a normal Raspberry Pi with no special hardware changes or external wiring etc.

This is behaving as I would expect: you are writing some stuff which is sent out of the serial port or to the tty. There is no reason that data would be presented back to you when you read from the serial port or tty.

The serial port is essentially two wires: one going out, one coming back in. Stuff you send out doesn't automatically come back in.

  • Sorry, you have reason. My hardware is Raspberry Pi 3 with Jessie. Nov 10 '16 at 12:24
  • I have clarified the first paragraph - hope it helps.
    – Mark Smith
    Nov 10 '16 at 12:55
  • There's an ambiguity in that the code in the question seems to assume ttyS0 and serial0 are different serial ports, and that they are connected together. If that were the case, this is not behaving as expected. The loop transmits the string and performs a blocking (?) read, so if they were separate ports connected together and the read is blocking, the first read should block indefinately. If the read is non-blocking, then the behaviour is indeteminate; most likely neither will get anything, but there is a chance the second one would get the string.
    – goldilocks
    Nov 10 '16 at 14:49

Maybe unrelated but I had problems with my serial giving garbage, turned off "Would you like a login shell to be accessible over serial" and that solved all my problems with intermittent serial problems

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.