Untitled

mail@pastecode.io avatar
unknown
plain_text
22 days ago
993 B
2
Indexable
Never
import serial

def check_port(port, baudrate=9600):
    try:
        with serial.Serial(port, baudrate, timeout=1) as ser:
            print(f"Port {port} is available.")
            return True
    except serial.SerialException as e:
        print(f"Error: {e}")
        return False

def receive_message(port, baudrate=9600):
    if not check_port(port, baudrate):
        return

    try:
        with serial.Serial(port, baudrate, timeout=1) as ser:
            print("Waiting for message...")
            while True:
                if ser.in_waiting > 0:
                    message = ser.readline().decode('utf-8').strip()
                    print("Received message:", message)
    except serial.SerialException as e:
        print("Error:", e)

if __name__ == "__main__":
    port = 'COM3'  # پورت سریال بلوتوث خود را اینجا وارد کنید
    baudrate = 9600  # نرخ انتقال داده
    receive_message(port, baudrate)
Leave a Comment