Python 3.4 - tkinter - arduino - binary to float? -


i'm trying use arduino send temperature data via usb python. i'm displaying data in tkinter label , works fine. however, looks data binary string , i'm trying comparison (if reading greater x; y). i'm quite newb, don't understand why label displays correctly, when "print" value, shows binary string. label displays 69.25 print command displays b'69.25/r/n'. unless has better idea of how accomplish this, think need convert binary float can math. here relevant portion of code.

ser = serial.serial('com4', 115200, timeout=.1)         def update():             while 1:                 reading.set(ser.readline())                 root.update()                 sleep(2)                 xxx=reading.get()                 print(xxx)          reading=stringvar(root)         currenttemplabel=label(root,textvariable=reading)  root.after(1,update) 

first, i'm willing bet have bytes b'69.25\r\n', not b'69.25/r/n'. former 5 characters followed carriage return , newline; latter 5 characters followed slash, r, slash, , n.

now, if have bytes value b'69.25\r\n', , represents number 69.25, it's representing float string in encoding. since characters in string going ascii characters 0123456789+-.ee\r\n, doesn't matter which encoding, long it's ascii-compatible. so:

>>> b = b'69.25\r\n' >>> s = b.decode('ascii') >>> s '69.25\r\n' >>> f = float(s) >>> f 69.25 

so, why "just work" when stick in tkinter label? because when give tkinter bytes, decodes using default encoding, ascii-compatible, , when pass bytes or str multiple lines single-line control, ignores after newline.


if have /r/n, not \r\n, placed there error in whatever's running on other end , can ignore them. fortunately, none of characters can appear in valid float, can little hacky , this:

>>> b = b'69.25/r/n' >>> s = b.decode('ascii') >>> s '69.25/r/n' >>> stripped = s.rstrip('rn/') >>> stripped '69.25' >>> f = float(stripped) >>> f 69.25 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -