Pages: [1]   Go Down
Author Topic: Problems with Arduino + Serial + Python!  (Read 635 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
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.

Code:
#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()
    cout=0
    while(loopVar):
        ser.write('x')
        ejeX = ser.readline()
        ser.write('y')
        ejeY = ser.readline()
        ser.write('z')
        ejeZ = ser.readline()

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


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

Thx! smiley-wink
Logged

0
Offline Offline
God Member
*****
Karma: 39
Posts: 986
Get Bitlash: http://bitlash.net
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
$ 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)
123
>>> print float(foo)
123.0

-br
http://bitlash.net
http://entropymouse.com
Logged

Pages: [1]   Go Up
Jump to: