1

I'm new to using serial ports, and I have a loopback cable for serial on a Centos 7 machine. I have something in python that tests the port, but I've found that the program returns the expected values even when my loopback isn't attached. Is there some kind of software loopback that already exists for ports in Linux?

In case it's useful information I'm using pySerial shown below (I had to type some of it from memory so I apologize if there are errors, but my question isn't about the code as much as it is how these ports actually work)

import serial

BAUD_RATE = 9600

def main():
    tx = b"A"

    COM_PORT='/dev/ttyS0'
    ser = serial.Serial(COM_PORT, BAUD_RATE, timeout=1, parity=serial.PARITY_NONE, rtscts=False, dsrdtr=False)
    ser.setDTR(False)
    ser.flush()
    print("Port " + COM_PORT)
    for y in range (0,10):
        print("Sent: " + str(tx))
        ser.write(tx)
        rx = ser.read(1)
        print("Received: " + str(rx))
    if(len(rx) > 0):
        print(COM_PORT + " - " + str(rx))
    ser.close()

if __name__ == "__main__":
    main()

The same thing happens when I use the much shorter and simple serial test shown below

Terminal 1

cat /dev/ttyS0

Terminal 2

echo "test" > /dev/ttyS0

Like I said, I get the expected output from both tests, but I get the same output whether the cable is connected or not.


Output From Tests

  • Checking if interface is really a character device

    ls -l /dev/ttyS0

output: crw-rw----, 1 user group 4, Date /dev/ttyS0

  • Check no other processes have it open

    sudo lsof /dev/ttyS0

output: Error I will have to copy in later. If I run without sudo I get a blank line returned

  • Checking for UART

    sudo setserial -v /dev/ttyS0

output: /dev/ttyS0, UART: 16550A, Port: 0x03f8, IRQ: 4

  • CTS and similar pins change when signal applied

    sudo statserial /dev/ttyS0

output: /dev/ttyS0, UART: 16550A, Port: 0x03f8, IRQ: 4

There is no change to this output when the echo command to the port is running with or without the cable connected.

  • Check that the transmit and receive numbers change

    sudo cat /proc/tty/driver/serial

output:

0: uart:16550A port:000003F8 irq:4 tx:8335 rx:8301
1: uart:16550A port:000002F8 irq:3 tx:0    rx:0
//There's a few more serial ports, but they all have 0 tx and rx, and on port 03F8, the tx and tx increase when I send to the serial port ttyS0

Another problem/symptom that could be related is that after I run the python script, I'm no longer able to cat /dev/ttys0

So, I can cat that file any time before I run the python script, as many times as I want, but immediately after I run that Python, I can't cat the file, it just immediately returns with nothing. I checked and the file isn't still open. I'm not sure why that would happen.


Update

After testing the other ports that were showing 0 on transmit and receive, I noticed that I actually am seeing the transmit value increase, but they are unable to receive. Is there anything that could be blocking their receive that I haven't tried? Once again, I'm assuming something is configured differently since the first port has expected behavior (other than the auto loopback I didn't expect), but I haven't found any rules applied to any of the ports.

  • 1
    Normally a serial port will echo input from the wire back out to the wire unless you set echo off. But you are writing to the device so echo does not apply. Check ls -l /dev/ttyS0 really is a character device. Check no other processes have it open sudo lsof /dev/ttyS0. Check sudo setserial -v /dev/ttyS0 says it is a uart. Check sudo statserial /dev/ttyS0 shows the CTS and similar pins change when you apply signals to them. Check sudo cat /proc/tty/driver/serial shows the count of rx/tx chars changing correctly. – meuh Aug 9 '18 at 10:00
  • @meuh Thanks for the help. I have edited the question to include the output you asked for. – trueCamelType Aug 9 '18 at 13:43
  • 1
    I'm not sure what to suggest next. stty -a -F /dev/ttyS0 will show you the settings left by your program. Perhaps clocal is being disabled (ie shown as -clocal) so your cat waits for a modem ready signal. Is the port soldered on the motherboard, or is it a cable that might be misplaced, miswired? Do you have a multi-meter or even just an led you could use on the output pins to check what TX is actually outputting? The idle "mark" state should be between -5v to -15v with reference to common ground, and when active goes to +5v. – meuh Aug 9 '18 at 14:54
  • I added an update based on some new information. I don't think it will be too helpful, since we've already checked things that I thought would be effecting the tx/rx. – trueCamelType Aug 17 '18 at 14:15
  • Also, I ran the stty -a -F /dev/ttyS[*], and all ports had the same result. – trueCamelType Aug 17 '18 at 14:36

Your Answer

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

Browse other questions tagged or ask your own question.