我正在为串行写入通信而挣扎。基本上,我不知道选择哪个选项来以正确的方式组合整数和浮点值,以便进行串行写入。在
问题:我需要将数据值(chars、int、floats)发送到微控制器(ARM8处理器)以控制机器人平台的轮子。通过rsm的方式,从一个微处理器42m读取相同的数据值:
import serial
from struct import unpack
# Initialization
counter = 0
#open serial for reading (BMW to PC communication)
s_port = 'COM8'
b_rate = 460800
ser = serial.Serial(port=s_port,baudrate=b_rate,timeout=0.01)
#method for reading incoming bytes on serial
while counter<20:
data = ser.readline()
data = data.encode("hex")
strlen = len(data)
rev_data = "".join(reversed([data[i:i+2] for i in range(0, len(data), 2)]))
if strlen==80:
Soh = data[0:2]
Nob = data[2:4]
Adr = data[4:6]
Cmd = data[6:8]
Hrt = data[8:12]
Po1 = data[12:28]
Po2 = data[28:44]
En1 = data[44:60]
En2 = data[60:76]
Crc = data[76:78]
Eot = data[78:80]
So1 = unpack('B', Soh.decode("hex")) # unsigned char
No1 = unpack('B', Nob.decode("hex")) # unsigned char
Ad1 = unpack('B', Adr.decode("hex")) # unsigned char
Cm1 = unpack('B', Cmd.decode("hex")) # unsigned char
Hr1 = unpack('h', Hrt.decode("hex")) # short
Po1 = unpack('d', Po1.decode("hex")) # double
Po2 = unpack('d', Po2.decode("hex")) # double
En1 = unpack('d', En1.decode("hex")) # double
En2 = unpack('d', En2.decode("hex")) # double
Cr1 = unpack('B', Crc.decode("hex")) # unsigned char
Eo1 = unpack('B', Eot.decode("hex")) # unsigned char
StartOfHeader = So1[0]
NoOfBytes = No1[0]
Address = Ad1[0]
Command = Cm1[0]
Heartbeat = Hr1[0]
Potentiometer1 = Po1[0]
Potentiometer2 = Po2[0]
Encoder1 = En1[0]
Encoder2 = En2[0]
CRC = Cr1[0]
EndOfTransmission = Eo1[0]
counter = counter+1
ser.close()
在Labview中,串行写入通信已经开始工作,因此这是我的出发点:
但是我需要使用Python,诀窍是让它在Python中工作。 据我所知,根据Labview(http://digital.ni.com/public.nsf/allkb/287D59BAF21F58C786256E8A00520ED5)中的“Type Cast”函数,数据被转换为ASCII
老实说,我对ASCII消息一无所知,所以我不能百分之百肯定。在
现在,我想对python2.7中的串行写入执行相同的技巧。从chr、int、float值开始转换成十六进制字符串,然后写入串口。我想在最后使用ASCII转换,但我不知道它是否是ASCII以及正确的Python命令,以正确的方式进行转换。
这是到目前为止我用Python编写的串行编写代码(请原谅,代码行太长且效率低下):
^{pr2}$这个Python代码生成与Labview程序相同的十六进制字符串(称为rev_hex_string)。但直到现在,我还不能用Python编写串行代码。在
我不知道该怎么办。串行写入需要ASCII码吗?使用哪些Python代码?编码(“ascii”),解码(“hex”)?我完全迷失在许多可能性中….
如果我在Python中使用encode(“ascii”)或decode(“hex”),它只会生成一个“.01”(ascii??)留言。在
当我在Labview中切换普通视图时,它会显示一条更长的消息: “0.01欧元?@|-A@A M“ (我还缺了一些字,但这是最多的)
除此之外,我需要回车和/或刷新缓冲区吗?
我得说我是串行通信新手。我希望你能帮助我。在
抱歉,我的帖子太长了,我希望尽可能准确。在
目前没有回答
相关问题 更多 >
编程相关推荐