0

I am using python 3.6. The following code works just fine on Windows 10 Pro:

import serial
import binascii

ser = serial.Serial("COM1") # "COM1" will be "/dev/ttyS0" on Linux
if ser.is_open == True:
    print("COM open")

ser.baudrate = 2400

print('Port configuration:')
print('baudrate:',ser.baudrate)
print('parity:',ser.parity)
print('stopbits:',ser.stopbits)
print('bytesize:',ser.bytesize)
print('xonxoff:',ser.xonxoff)
print('timeout:',ser.timeout)
print()
print('sending...')
frame = bytearray()
frame.append(0x7e)
frame.append(0x03)
frame.append(0x02)
frame.append(0x21)
frame.append(0x00)
frame.append(0xa4)
ser.write(frame)
print(binascii.hexlify(frame))
print()
print('receiving...')
recv = ser.readline()
recv_len = len(recv)
print(binascii.hexlify(recv))
print()
ser.close()
if ser.is_open == False:
    print("COM closed")

But it gets stuck at 'ser.readline()' when I run it under CentOS 6.8, as there was no cable attached to the port. It looks like a trivial issue, but I cannot figure out what's wrong or missing.

If you cannot either, I hope the sample code can result useful to someone at least.

ambrojohn
  • 11
  • 1
  • the command `setserial -g /dev/ttyS*` on my CentOS returns `/dev/ttyS0, UART: 16550A, Port: 0x03f8, IRQ: 4`, `/dev/ttyS1, UART: 16550A, Port: 0x02f8, IRQ: 3`, `/dev/ttyS2, UART: unknown, Port: 0x03e8, IRQ: 4`, `/dev/ttyS4, UART: unknown, Port: 0x02f8, IRQ: 3` – ambrojohn Jul 11 '17 at 18:07
  • It seels like end-of-line issue, see for example this answer: https://stackoverflow.com/a/16961872/6401403 – Michael Jul 12 '17 at 09:31

1 Answers1

1

False problem. The code worked using ttyS1 instead of ttyS0 (I knew it was something trivial). Anyway, very useful to check

cat /proc/tty/driver/serial

which shows tx/rx statistics and parameters as DTS, RTS, RI, etc. next to each port. For example, next to the ttyS1 I noticed an 'RI' which was the same parameter that Hercules terminal on Windows showed me (graphically) when I tried to open COM1. Very intuitive to identify a serial port this way!

ambrojohn
  • 11
  • 1