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 に答える