串口通讯麻烦

时间:2014-06-03 17:18:32

标签: python serial-port embedded pyserial

我陷入了Digi Xbee ConnectPort X3和嵌入式主板之间的串行通信。 Digi Xbee是python代码。我正在以数据包的形式将digi的温度和光传感器值发送到电路板:

Sync | Low Byte | High Byte | Data | Checksum

设备接受。我单独测试了两个设备。他们工作得很好。我使用pyserial从我的计算机向嵌入式主板发送数据,它可以完美地接收数据(大约30个数据)。然后我检查了Digi是否在COM端口使用我的电脑完美地发送数据。但是当我将Digi设备连接到电路板时,digi发送的数据非常少(1到3),即我可以将digi Xbee调试到一个扩展,我在发送数据后看到Digi Xbee的COM端口关闭。

我已经添加了Digi Xbee的部分源代码供您理解

open()

while True:
# Retrieve a sample reading from the LT

flushInput()

flushOutput()

io_sample = xbee.ddo_get_param(sensor_address, “is”)

light = parseIS(io_sample)["AI1"]

temp = parseIS(io_sample)["AI2"]

hum = parseIS(io_sample)["AI3"]

mVanalog = (float(temp) / 1023.0) * 1200.0

temp_C = (mVanalog – 500.0)/ 10.0 # – 4.0

lux = (float(light) / 1023.0) * 1200.0

print “hum:%f” %hum

sync=254

temp=round(temp_C,2)

light=round(lux,2)

no_temp = len(str(temp))

no_light = len(str(light))

total_length=no_temp+no_light+3+2

if total_length<256:

    low_byte=total_length

    high_byte=0

else:

  low_byte=total_length%256

  high_byte=high_byte/256

msg_id_low=0

msg_id_high=0

checksum=low_byte+high_byte+msg_id_low+msg_id_high+ord('#')+ord(':')+ord(',')

t=str(temp)

for c in t:

  if c == '.':

    checksum+=ord('.')

  else:

    checksum+=ord(c)

t=str(light)

for c in t:

  if c == '.':

    checksum+=ord('.')

  else:

    checksum+=ord(c)

checksum=256-(checksum%256)

if checksum == 256:

  checksum=0

print "Checksum Value after applying mod operator:%d" %checksum

packet=str(chr(254))+str(chr(low_byte))+str(chr(high_byte))+str(chr(msg_id_low))+str(chr(msg_id_high))+str('#')+str(temp)+str(':')+str(light)+str(',')+str(chr(checksum))


print "packet:%s" %packet

bytes_written = write(packet)

print "bytes_written : %s value : %s" %(bytes_written,packet)

time.sleep(15)

0 个答案:

没有答案