我试图将串行数据转换为浮动。
我将串行数据发送到需要浮点值的其他程序,但我不知道我是怎么做的。
我发送的值介于0和180之间,这些值接收度数,但我需要像度数一样的值。 我怎么办?
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 a string
#print envio
except Exception, e:
print "Error en \n\t "+str(e)+"\n"
ser.close
此脚本中收到x值
ser.open()
degree = ser.read()
for 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
但是x by serial似乎是二进制文件,我需要一个浮点数。
我希望你能帮助我 谢谢你!
******************** EDIT ***********************
补充问题
完整的代码发送数据
# -*- 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 which port was really used
#for i 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 a string
#print envio
except Exception, e:
print "Error en \n\t "+str(e)+"\n"
ser.close
完整的代码接收序列
import pygame
import sys, serial
from 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:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == K_ESCAPE:
sys.exit()
# for 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()
for 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
# then render the 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
答案 0 :(得分:0)
我不清楚你的意思是x是二进制,但你试过吗
degree = float(ser.read())
答案 1 :(得分:0)
您可以将数据作为二进制文件发送,也可以使用多种文本表示形式之一发送。
将其作为二进制数据发送:
import struct
ser.write(struct.pack("!f", x)) # On sending side
degree = struct.unpack("!f", ser.read(4)) # On receiving side
序列化Python数据的标准文本格式称为“pickle”:
import pickle
pickle.dump(x, ser) # On the sending side
degree = pickle.load(ser) # On the receiving side