Python Forum
Convert string to float problem
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert string to float problem
#1
Hi, I have a problem converting a string to a float. The program receives data from Arduino via the serial port and processes it. But I'm getting this error message. Is there a solution? Thank you!

from pynput.mouse import Button, Controller
import time
import serial

mouse = Controller()
ser = serial.Serial('COM3', baudrate=9600, timeout=1)


while True:
    arduinoData = ser.readline().decode('ascii')
    print(arduinoData)
    data = (float(arduinoData))
    mouse.position = (data, 100)

Attached Files

Thumbnail(s)
   
Reply
#2
The string is empty. What should your program do in that case? You can catch the exception and set whatever value you want when that happens, or do something else.
Reply
#3
If you don't get get data within 1 second the read gives up and returns an empty buffer. It is possible that you could also time out while reading data and get a partial buffer. You might also get a transmission error that results in bad data. I would cover all three possibilities by wrapping an exception handler around the conversion and only updating the mouse position when you successfully read a new value.
Reply
#4
Arduino sends data with a delay of 1 ms.
Reply
#5
This is how output and Arduino code looks like.

Attached Files

Thumbnail(s)
       
Reply
#6
Oh, thank you. It works perfectly with try/except! Also I have one more question. I need to make value bigger (multiply), but this does not work. How can I do it?

from pynput.mouse import Button, Controller
import time
import serial

mouse = Controller()
ser = serial.Serial('COM3', baudrate=9600, timeout=1)


while True:
    try:
        arduinoData = ser.readline().decode('ascii')
        data = (arduinoData)/5
        print(data)
        mouse.position = (data, 100)
    except:
        print("fail")
        break
EDIT: Solved also this.
Reply
#7
Where is float()?
Reply
#8
(May-28-2022, 01:08 PM)deanhystad Wrote: Where is float()?
Program doesnt end with error anymore so I don´t need it.
Reply
#9
This gets a str object:
arduinoData = ser.readline().decode('ascii')
This tries to divide the str by 5
data = (arduinoData)/5
Dividing a str by an int raises a TypeError which is caught by the overly broad except: Your program prints "fail" every time, no matter what is read
You should do this:
data = float(arduinoData)/5
This might also fail, but if arduinoData is a string that can be interpreted as a float, it will succeed.
ndc85430 likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Convert From PDf into JPEG Problem koklimabc 4 922 Sep-05-2023, 06:44 AM
Last Post: Gribouillis
  convert string to float in list jacklee26 6 1,820 Feb-13-2023, 01:14 AM
Last Post: jacklee26
  openpyxl convert data to float jacklee26 13 5,722 Nov-19-2022, 11:59 AM
Last Post: deanhystad
  how to convert tuple value into string mg24 2 2,238 Oct-06-2022, 08:13 AM
Last Post: DeaD_EyE
  Convert SQLite Fetchone() Result to float for Math Extra 13 3,390 Aug-02-2022, 01:12 PM
Last Post: deanhystad
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 4,767 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  Detecting float or int in a string Clunk_Head 15 4,283 May-26-2022, 11:39 PM
Last Post: Pedroski55
  Convert a string to a function mikepy 8 2,423 May-13-2022, 07:28 PM
Last Post: mikepy
Question How to convert string to variable? chatguy 5 2,235 Apr-12-2022, 08:31 PM
Last Post: buran
  Convert string to int Frankduc 8 2,395 Feb-13-2022, 04:50 PM
Last Post: menator01

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020