Pages: [1]   Go Down
Author Topic: Problems with Arduino + Serial + Python!  (Read 759 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Karma: 0
Posts: 17
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello friends, I commented that I am developing a project that I have to read data sent by the Arduino serial communication right through to a program in python that has to take numerical data and perform math ...

But the problem in question is next, take the arduino sends data right through the serial interface as "string" but I can not with any command to convert a numeric variable of type "int" or "float" in order to perform math operations ...

If anyone can help me do this conversion or directly take the data in numerical form, I will be very Agreco, as from now thank you very much.

Here I leave the code I'm using to read the data.

#Declaracion de librerias a utilizar
import serial
import win32api
import time
import math
#Clases para la ventana

#Inicializacion Serial
ser = serial.Serial(3)
ser.baudrate = 115200
ser.bytesize = 8
ser.parities = 0
ser.stopbits = 1
ser.xonxoff = 0
ser.rtscts = 0
ser.timeout = 1
print "Inicializando..."

loopVar = True
if (ser.isOpen()):
# Inicio del programa - void loop()
        ejeX = ser.readline()
        ejeY = ser.readline()
        ejeZ = ser.readline()

        while(cout!=1): #intento de transformar a int
                x = int(ejeX)
                y = int(ejeY)
                z = int(ejeZ)
            except ValueError:
                x = 0
                y = 0
                z = 0
            cout = cout + 1

print " EjeX = %d EjeY = %d EjeZ = %d " % (x,y,z)

Thx! smiley-wink

Offline Offline
God Member
Karma: 39
Posts: 988
Get Bitlash:
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Would int() and float() be what you're looking for?

$ python
Python 2.7 (r27:82508, Jul  3 2010, 20:17:05)
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> foo="123"
>>> print int(foo)
>>> print float(foo)


Pages: [1]   Go Up
Jump to: