sending and recieving through serial
Question: I have got a simple program running in the uno that measures the distance
using ping sensor and now i am trying to control some servos based on the
distance in python but the conditional thingy is not working even in a simple
code like this

    
    
    import serial
    data = serial.Serial('COM7',9600)
    while(1):        
        if(data.inWaiting()>0):
            dist = data.readline()
            if(dist>100):
                print("dist is greater than 100")
            else:
                print("this shit does not work")
    

It is always the if that works, I am a noob please help me!


Answer: Data from serial port type is `str`

You are trying to read the data and use `if .... > 100` this will never work.

You need to read the data to a buffer and then check it, Also you need some
sync frame to know you have got all the data(it's not a must but its much
eaiser)

for example let say the read data is `100!` and `!` is your sync frame.

    
    
    import serial
    my_serial = serial.Serial('COM7',9600)
    my_data = ""
    while(1):        
        if(my_serial.inWaiting()>0):
            my_data = my_serial.readline()
            if '!' in my_data:
                break
    
    my_data = [:my_data.find("!")]
    if int(my_data) > 100:
        print("dist is greater than 100")
    else:
        print("this shit does not work")
    

