Files
ATTY/scripts/try_configurations.py
2023-03-05 12:20:39 +01:00

49 lines
1.8 KiB
Python

import serial
import itertools
import sys
import select
# Define the possible serial settings
baud_rates = [i for i in range(10000, 13000, 200)]#[9600, 19200, 38400]
baud_rates = [11200]
data_bits = [serial.FIVEBITS, serial.SIXBITS, serial.SEVENBITS, serial.EIGHTBITS]
data_bits = [serial.EIGHTBITS]
#stop_bits = [serial.STOPBITS_ONE, serial.STOPBITS_ONE_POINT_FIVE, serial.STOPBITS_TWO]
stop_bits = [serial.STOPBITS_ONE]
parity = [serial.PARITY_NONE, serial.PARITY_EVEN, serial.PARITY_ODD, serial.PARITY_MARK, serial.PARITY_SPACE]
#parity = [serial.PARITY_NONE]
# Define the serial port to test
port = '/dev/serial0'
# Try all possible combinations of settings
for baudrate, databits, stopbits, paritybit in itertools.product(baud_rates, data_bits, stop_bits, parity):
i = 0
try:
# Open the serial port with the current settings
ser = serial.Serial(port=port, baudrate=baudrate, bytesize=databits, stopbits=stopbits, parity=paritybit)
# Print the successful settings
print(f"Success! Baudrate: {baudrate}, Databits: {databits}, Stopbits: {stopbits}, Parity: {paritybit}")
while True:
if sys.stdin in select.select([sys.stdin], [], [], 0)[0]:
# Wait for the user to press a key
input_char = sys.stdin.read(1)
break
if ser.in_waiting > 0:
# Read and print the received data
data = ser.read(ser.in_waiting)
c = bin(int.from_bytes(data, byteorder="big"))
print(f"{i}: Received data: {data} chr: \"{c}\"")
i += 1
# Close the serial port
ser.close()
except serial.SerialException as e:
print(e)
# Ignore exceptions and continue with the next settings
pass