发送串行值并转换为浮点数?
我想把串口数据转换成浮点数。
我把串口数据发送给另一个需要浮点值的程序,但我不知道该怎么做。
我发送的值在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值似乎是二进制的,我需要的是浮点数。
希望你能帮我,谢谢你!!
********************编辑***********************
补充一下问题
发送数据的完整代码
# -*- 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
2 个回答
0
我不太明白你说的“x是二进制”是什么意思,不过你试过
degree = float(ser.read())
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