操纵python中的数字

时间:2013-02-19 18:14:31

标签: python

我必须在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

有人能引导我至少找到正确的文件吗?

非常感谢!

1 个答案:

答案 0 :(得分: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()

您如何看待这个?