python - Send serial value and convert them to float? -
i trying convert serial data float.
i send serial data other program need float value, dont know how do.
the values send between 0 , 180, , these values received degrees, need values degrees. how do??
while true: while z == true: x = x+1 print x if x == 180: z = false while z == false: x = x-1 print x if x == 0: z = true try: envio=ser.write("x") # write string #print envio except exception, e: print "error en \n\t "+str(e)+"\n" ser.close
and x value received in script
ser.open() degree = ser.read() x in range(1,400,10): linea = (432,200) linea_len = 100 x = linea[0] + math.cos(math.radians(degree)) * linea_len y = linea[1] + math.sin(math.radians(degree)) * linea_len
but x send serial seems binary, , need float.
i hope can me you!!
********************edit***********************
tho complement question
complete code send data
# -*- coding: utf-8 -*- """ created on thu aug 14 15:47:07 2014 @author: zucra """ import serial ser = serial.serial() # open first serial port ser.port = 2 ser.baudrate = 4800 x=0 z = true try: ser.open() except exception, e: print "error en \n\t "+str(e)+"\n" print ser.name # check port used #for in range(1,1000): while true: while z == true: x = x+1 print x if x == 9: z = false while z == false: x = x-1 print x if x == 0: z = true try: envio=ser.write("x") # write string #print envio except exception, e: print "error en \n\t "+str(e)+"\n" ser.close
complete code of code receive serial
import pygame import sys, serial pygame.locals import * pygame.init() width = 865 height = 286 ser = serial.serial() ser.port = 2 ser.baudrate = 4800 def load_image(filename, transparent=false): try: image = pygame.image.load(filename) except pygame.error, message: raise systemexit, message image = image.convert() if transparent: color = image.get_at((0,0)) image.set_colorkey(color, rleaccel) return image screen = pygame.display.set_mode((width, height)) pygame.display.set_caption("angulo pygame") background_image = load_image('fondo.png') ser.open() degree = float(ser.read(1)) # se define la letra por defecto fuente = pygame.font.font(none, 20) screen.blit(background_image, (0, 0)) while true: event in pygame.event.get(): if event.type == pygame.quit: sys.exit() elif event.type == pygame.keydown: if event.key == k_escape: sys.exit() # event in pygame.event.get(): # if event.type == pygame.quit: # sys.exit() # elif event.type == pygame.keydown: # if event.key == k_left: # if degree < 180 : # degree = degree + 1 # elif event.key == k_right: # if degree > 0: # degree = degree - 1 # elif event.key == k_escape: # sys.exit() # ser.close() x in range(1,400,10): linea = (432,200) linea_len = 100 x = linea[0] + math.cos(math.radians(degree)) * linea_len y = linea[1] + math.sin(math.radians(degree)) * linea_len # render line linea->(x,y) screen.blit(background_image, (0, 0)) texto = "%d" % (degree) mensaje = fuente.render(texto, 1, (255, 255, 255)) screen.blit(mensaje, (450, 185)) pygame.draw.line(screen, color("red"), linea, (x,y), 5) pygame.display.flip() #vuelca imagen
i'm not clear on mean x binary, have tried
degree = float(ser.read())
Comments
Post a Comment