Untitled

 avatar
unknown
plain_text
5 months ago
1.2 kB
3
Indexable
import serial

def main():
    # Serial port ayarları
    port = "/dev/serial0"  # Raspberry Pi üzerinde varsayılan UART portu
    baudrate = 9600        # Baud rate
    bytesize = serial.EIGHTBITS  # Veri bitleri
    parity = serial.PARITY_NONE  # Parite
    stopbits = serial.STOPBITS_ONE  # Stop bit

    try:
        # Serial portu aç
        ser = serial.Serial(
            port=port,
            baudrate=baudrate,
            bytesize=bytesize,
            parity=parity,
            stopbits=stopbits,
            timeout=1
        )

        print(f"Listening on {port} with baudrate {baudrate}...")
        
        while True:
            if ser.in_waiting > 0:  # Gelen veri var mı?
                data = ser.read(ser.in_waiting).decode('utf-8')  # Veriyi oku ve decode et
                print(f"Received data: {data}")
    except serial.SerialException as e:
        print(f"Error: {e}")
    except KeyboardInterrupt:
        print("\nProgram terminated.")
    finally:
        if 'ser' in locals() and ser.is_open:
            ser.close()
            print("Serial port closed.")

if __name__ == "__main__":
    main()
Editor is loading...
Leave a Comment