user2088201
user2088201

Reputation: 11

Manipulate numbers in python

I have to write a program in python which could manipulate a serial data input to make it usable.

The received data looks like this:

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

The first 9 bytes and the last 3 are always the same.

So the first step would be to keep only the bolted numbers.

Then, because it would be too easy, to invert some of those number because some are lowbyte first but not all.

In fact I need to go from this:

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

to this

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

Can someone lead me at least to the right documentation please?

Thanks a lot!

Upvotes: 0

Views: 326

Answers (1)

user2088201
user2088201

Reputation: 11

At the moment I've done this. It's working but Im not satisfied.

I commented the serial part and replaced the result by a exemple answer from the controler.

#!/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()

What do you think about this?

Upvotes: 1

Related Questions