pythonpython-3.xarduinoserial-portpyserial

How to create an if statement with serial data?


How would I write the code so if I recieve 'hello' it runs a function?

import serial


ser = serial.Serial(port='/dev/tty.usbmodem11201',baudrate=9600)

while True:
    value= ser.readline()
    valueInString=str(value, 'UTF-8')
    print(valueInString)

    if ser.is_open == hello:
        print("Hi")

This is what I saw to do but it did not work. How would I get it to work? Thanks!


Solution

  • ser.is_open == hello is wrong. It checks if the serial port is open, not if the message is "hello".

    if valueInString.strip() == "hello":