0

pySerial write() won't take my string

In that post, coders explained how to use the

ser.write()

method.

I am still wondering how to use the

readline()

method in python3.x as I want to recieve a float, and compare it in my python 3.x code. I know how to use the readline() in python 2.7. But I am not able to get it working in 3.x.

import serial
import time

ser=serial.Serial('/dev/ttyACM0', 9600)
while 1:
    ans = ser.readline()
    print(ans)
    if ans > 25.50:
        print('its getting hot in  here')
    else:
        print('Everything is fine')

Thanks in advance

Anton van der Wel
  • 451
  • 1
  • 6
  • 20

1 Answers1

2

The first thing that occurs to me is that ans will be a string variable after readline(). You can't compare a string to a floating-point variable without some conversion. So:

if ans > 25.50:

will not work because ans is a string. Instead, you need something like:

float_ans = float(ans)
if float_ans > 25.50:
# etc...
TomServo
  • 7,248
  • 5
  • 30
  • 47