0

The serial port program attempted to print the data from the Python Let me show you the code I used.

import serial
import time

ser =serial.Serial(port="COM1",
                   baudrate=926100,
                   bytesize=serial.EIGHTBITS,
                   parity=serial.PARITY_NONE,
                   timeout=20)


ser.isOpen()                    ## open port 



print ("Sensortag's connected Succesfully!.")



while 1:

    res1 =ser.readline()

    res3=res1[13:21]          
    time.sleep(1.0)
    if res3 >=250:               
       print("Sensor has invalid value Please wait a moment.")
    elif res3 < 250 & res3>=200:
        print("The user's movements are abnormal. Please check.")
    else:
        print("The user Your movements are normal.")



print(res3)

First of all, what I want is to print numbers in a program and see if they exceed a certain range.But here's the problem.

Traceback (most recent call last):
      File "C:\Users\SEOI\AppData\Local\Programs\Python\Python37-32\ㅋㅋㅋㅋ.py",     line 37, in <module>
        if res3 >=250:               ## 3).When UART outputs a value, it outputs     the required parts
TypeError: '>=' not supported between instances of 'bytes' and 'int'

In the following questions, you have identified an error because the value i can receive in the program is not numeric However, it is not possible to know how 'b' should remove the part from the part that is output from the file

We used sliding to solve the following problem, but 'b' as shown in the picture has been output. I'll attach the picture to the printout section of B mentioned above.

enter image description here

++)I just checked, and I can't see the image of B in the right direction, so I'll leave it in writing.

b'0253.787' b'0261.909' b'0268.000' b'0263.939' b'0261.909' b'0259.878' b'0257.848' b'0255.818' b'0253.787' b'0290.333' b'0282.212' b'0276.121' b'0270.030' b'0265.969' b'0263.939' b'0261.909' b'0259.878' b'0261.909' b'0263.939' b'0261.909' b'0259.878' b'0257.848' b'0255.818' b'0253.787' b'0300.484' b'0485.242' b'0633.454'

임종훈
  • 147
  • 1
  • 3
  • 8
  • @isydmr The `'b'` is not "extra". It signifies that the information coming from the serial port is ***b***inary. – DeepSpace Aug 27 '18 at 10:27
  • @DeepSpace is right - you should just convert to str before printing the output - it should fix "extra b problem" for you – Alex K. Aug 27 '18 at 10:28
  • @AlexK. Actually, converting to `str` will simply change the error OP is getting. `res3` should be converted to `float` – DeepSpace Aug 27 '18 at 10:30
  • @DeepSpace he will lose leading zeros then – Alex K. Aug 27 '18 at 10:35
  • 1
    @AlexK. Correct, but I assume they mean nothing since OP is comparing the value with an integer. – DeepSpace Aug 27 '18 at 10:36
  • @isydmr As you answered, print(res3[1b]) was used. However, an invalid systex error occurred. How do we solve the problem? – 임종훈 Aug 28 '18 at 04:39
  • @DeepSpace I thought about it the way you told me. But the problem I'm trying to solve is that I don't have 'b' and I just want numbers to be printed. What should I do? And is it possible to change that into numerical columns? – 임종훈 Aug 28 '18 at 04:41

1 Answers1

2
b'... '

Is python's way of representing a string of bytes. The error is telling you that you can't use >= to compare bytes and an integer.

You need to convert your byte strings to integers before that line. There is an easy way of doing this, as pointed out by DeepSpace

integer = float(res3)
strava
  • 765
  • 6
  • 14
  • 1
    No need to convert to `str` first. `float(res3)` will be enough. Converting to `int` will raise a `ValueError` (strings with decimal point can not be converted to `int`) – DeepSpace Aug 27 '18 at 10:31
  • No way, that's much simpler XD – strava Aug 27 '18 at 10:32
  • I did not let B come in at this time in various responses above. However, I tried to print out a number other than b, but it appears as string, so I could not solve the problem. What should I do? – 임종훈 Aug 28 '18 at 12:02