目前我已经做到了。它的工作,但我不满意。
我评论了串行部分,并用控制器的示例答案替换了结果。
#!/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()
你怎么看待这件事?