0

我必须用 python 编写一个程序,它可以操纵串行数据输入以使其可用。

接收到的数据如下所示:

eb 90 eb 90 eb 90 00 a0 18 d8 0a ba 17 00 00 30 00 a1 08 d7 0a 01 00 00 62 00 00 01 01 31 3e 01 00 cb 2f 7f

前 9 个字节和后 3 个字节始终相同。

所以第一步是只保留螺栓编号。

然后,因为它太容易了,将其中一些数字反转,因为有些是低字节优先但不是全部。

事实上,我需要从这个出发:

d8 0a ba 17 00 00 30 00 a1 08 d7 0a 01 00 00 62 00 00 01 01 31 3e 01 00

对此

0ad8 17ba 0000 0030 08a1 0ad7 01 00 00 62 00 00 01 01 31 013e 00

有人可以引导我至少找到正确的文档吗?

非常感谢!

4

1 回答 1

1

目前我已经做到了。它的工作,但我不满意。

我评论了串行部分,并用控制器的示例答案替换了结果。

#!/usr/bin/env python

import serial, time

######## Serial request monitoring, always the same command
#ser = serial.Serial("/dev/ttyUSB0", 9600, timeout=0)
#ser.write("\xeb\x90\xeb\x90\xeb\x90\x01\xa0\x01\x03\xbd\xbb\x7f")
#time.sleep(1)
data = 'eb90eb90eb9000a01889091400000024017a087d0b0100002300000000320000004e4d7f'
#data = ser.readline()
#data = data.encode('hex')



######## results
sync = data[:12]                                             #6byte not needed
ident = data[12:14]                                          #1byte not needed
command = data[14:16]                                        #1byte not needed
datalength = data[16:18]                                     #1byte who care
batteryvoltage = data[20:22]+data[18:20]                     #2byte lowbyte first 
pvvoltage = data[24:26]+data[22:24]                          #2byte lowbyte first
res1 = data[26:30]                                           #2byte 
loadcurrent = data[32:34]+data[30:32]                        #2byte lowbyte first
overdischargevoltage = data[36:38]+data[34:36]               #2byte lowbyte first
batteryfullvoltage = data[40:42]+data[38:40]                 #2byte lowbyte first
loadstate = data[42:44]                                      #1byte
overload = data[44:46]                                       #1byte
loadshortcircuit = data[46:48]                               #1byte
res2 = data[48:50]                                           #1byte
batteryoverload = data[50:52]                                #1byte
overdischarge = data[52:54]                                  #1byte
fullindicator = data[54:56]                                  #1byte
chargingindicator = data[56:58]                              #1byte
batterytemp = data[58:60]                                    #1byte
chargingcurrent = data[62:64]+data[60:62]                    #2byte lowbyte first
res3 = data[64:66]                                           #1byte
check = data[66:70]                                          #2byte
exitcode = data[70:72]                                       #1byte

######## Convert
rbatteryvoltage = int(batteryvoltage, 16) / float(100)
rpvvoltage = int(pvvoltage, 16) / float(100)
rloadcurrent = int(loadcurrent, 16)  / float(100)
roverdischargevoltage = int(overdischargevoltage, 16) / float(100)
rbatteryfullvoltage = int(batteryfullvoltage, 16) / float(100)
rbatterytemp = int(batterytemp, 16) -30
rchargingcurrent= int(chargingcurrent, 16) / float(100)

######## units
amp = 'amperes'
volt = 'volts'
deg = 'degree'

######## text
tbatteryvoltage = 'Your battery voltage is:'
tpvvoltage = 'Your PV field voltage is:'
tloadcurrent = 'Your load consumption is'
toverdischargevoltage = 'Your discharged battery voltage is:'
tbatteryfullvoltage = 'Your full battery voltage is:'
tloadstate = 'the load is (1=on, 0=off)'
toverload = 'Your load is overloaded (1=yes, 0=no)'
tloadshortcircuit = 'The load is in short circuit (1=yes, 0=no)'
tbatteryoverload = 'Your battery is overloaded (1=yes, 0=no)'
toverdischarge = 'Your battery is overdischarged (1=yes, 0=no)'
tfullindicator = 'Your battery is full (1=yes, 0=no)'
tchargingindicator = 'You are charging (1=yes, 0=no)'
tbatterytemp = 'Your battery temperature is'
tchargingcurrent = 'You are charging at'



print tbatteryvoltage, rbatteryvoltage, volt
print tpvvoltage, rpvvoltage, volt
print tloadcurrent, rloadcurrent, amp
print toverdischargevoltage, roverdischargevoltage, volt
print tbatteryfullvoltage, rbatteryfullvoltage, volt
print tloadstate, int(loadstate, 16)
print toverload, int(overload, 16)
print tloadshortcircuit, int(loadshortcircuit, 16)
print tbatteryoverload, int(batteryoverload, 16)
print toverdischarge, int(overdischarge, 16)
print tfullindicator, int(fullindicator, 16)
print tchargingindicator, int(chargingindicator, 16)
print tbatterytemp, rbatterytemp, deg
print tchargingcurrent, rchargingcurrent, amp 

#ser.close()

你怎么看待这件事?

于 2013-02-25T18:33:11.450 回答