Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 1225

Python • Re: Serial communication problem

$
0
0
perhaps you could try the following:
- remove the sleep(1) after sending the 'o'
- instead of using readline, use for test purpose a while loop with read(size=1) and print result print( f"res {res:02x} {res}")

untested:

Code:

import serialimport time# Serial port settingsport = 'COM11'speed = 300data_bits = 7stop_bits = 1parity = 'N'try:    # Open serial connection    ser = serial.Serial(        port=port,        baudrate=speed,        bytesize=data_bits,        stopbits=stop_bits,        parity=parity,        timeout=1    )        # Check if the serial port is opened successfully    if ser.isOpen():        print("Serial port", port, "has been opened successfully.")    else:        raise Exception("Failed to open serial port", port)    while True:        # Text to send        text_to_send = "o\r"        # Sending data (encoding text to ASCII)        ser.write(text_to_send.encode('ascii'))        # Read the response        while True:            res = ser.read(size=1)            print( f"res {res:02x} {res}")    # Close the serial connection    ser.close()except Exception as e:    print("An error occurred:", e)

Statistics: Posted by ghp — Thu Jun 06, 2024 10:16 am



Viewing all articles
Browse latest Browse all 1225

Trending Articles