Created
December 7, 2014 22:55
-
-
Save wattnotions/cd3d2a7708b83d476596 to your computer and use it in GitHub Desktop.
Python script to decode serial data from Tenma 72-7735 Multimeter to give numbers displayed on lcd screen
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import serial | |
import time | |
import binascii | |
ser = serial.Serial( | |
port='/dev/ttyUSB0', | |
baudrate=2400, | |
timeout = 0.1, | |
stopbits=serial.STOPBITS_ONE, | |
bytesize=serial.EIGHTBITS | |
) | |
ser.close() | |
ser.open() | |
ser.flush() | |
hex_vals = [] | |
temp_str = '' | |
bin_vals = [] | |
dmm_data = '' | |
lower_nibbles = [] | |
for i in range (0, 14): # make lists to store hex and binary values | |
hex_vals.append('0') | |
bin_vals.append(0) | |
lower_nibbles.append(0) | |
def get_lower_nibbles(bin_vals): # only keep the lower part of nibble | |
for i in range(0, len(bin_vals)): | |
temp_str = str(bin_vals[i]) | |
lower_nibbles[i] = str(temp_str[4:8]) | |
return(lower_nibbles) | |
a=[] | |
b=[] | |
c=[] | |
d=[] | |
digit = 0 | |
for h in range(8): # make a list for each digit to record which parts of seven segment are on or off | |
a.append('0') | |
b.append('0') | |
c.append('0') | |
d.append('0') | |
def lcd_get_num(lcd_array): ## each digit corresponds to certain parts of seven segment display being on | |
digit = 0 | |
if lcd_array == '1010000' : digit = 1 | |
if lcd_array =='1101101' : digit = 2 | |
if lcd_array == '1111001' : digit = 3 | |
if lcd_array == '0110011' : digit = 4 | |
if lcd_array == '1011011' : digit = 5 | |
if lcd_array == '1011111' : digit = 6 | |
if lcd_array == '1110000' : digit = 7 | |
if lcd_array == '1111111' : digit = 8 | |
if lcd_array == '1111011' : digit = 9 | |
if lcd_array == '1111110' : digit = 0 | |
return str(digit) | |
def decode_data(lower_nibbles): #this function works out what numbers are being displated based on | |
for i in range(1, len(lower_nibbles)): # which parts of each seven segment digit are turned on | |
temp_reg = lower_nibbles[i] | |
# print temp_reg | |
# print len(a) | |
if (i==1): ## first digit | |
#print temp_reg[3] | |
a[1] = temp_reg[3] | |
a[6] = temp_reg[2] | |
a[5] = temp_reg[1] | |
if (i==2): | |
a[2] = temp_reg[3] | |
a[7] = temp_reg[2] | |
a[3] = temp_reg[1] | |
a[4] = temp_reg[0] | |
if (i==3): | |
b[1] = temp_reg[3] | |
b[6] = temp_reg[2] | |
b[5] = temp_reg[1] | |
if (i==4): | |
b[2] = temp_reg[3] | |
b[7] = temp_reg[2] | |
b[3] = temp_reg[1] | |
b[4] = temp_reg[0] | |
if (i==5): | |
c[1] = temp_reg[3] | |
c[6] = temp_reg[2] | |
c[5] = temp_reg[1] | |
if (i==6): | |
c[2] = temp_reg[3] | |
c[7] = temp_reg[2] | |
c[3] = temp_reg[1] | |
c[4] = temp_reg[0] | |
if (i==7): | |
d[1] = temp_reg[3] | |
d[6] = temp_reg[2] | |
d[5] = temp_reg[1] | |
if (i==8): | |
d[2] = temp_reg[3] | |
d[7] = temp_reg[2] | |
d[3] = temp_reg[1] | |
d[4] = temp_reg[0] | |
a.pop(0) | |
b.pop(0) | |
c.pop(0) | |
d.pop(0) | |
achar = ''.join(a) | |
bchar = ''.join(b) | |
cchar = ''.join(c) | |
dchar = ''.join(d) | |
a.append('0') | |
b.append('0') | |
c.append('0') # this terrible piece of code would probably make | |
d.append('0') # Guido van Rossum cry | |
d1 = lcd_get_num(achar) | |
d2 = lcd_get_num(bchar) | |
d3 = lcd_get_num(cchar) | |
d4 = lcd_get_num(dchar) | |
print d1+d2+d3+d4 #print numbers displayed on LCD screen | |
return(achar, bchar, cchar, dchar) | |
dmm_data = "" | |
while True: | |
dmm_data = "" | |
ser.flush() | |
while dmm_data == "": #wait for a packet | |
dmm_data = str(binascii.hexlify(ser.read(14))) | |
g = 0 # concert hex values into binary | |
for i in range(0, len(dmm_data), 2): | |
bin_vals[g] = str( bin(int(dmm_data[i:i+2], 16))[2:].zfill(8)) | |
g=g+1 | |
#print bin_vals | |
get_lower_nibbles(bin_vals) # only interested in lower nibble of each byte | |
#print lower_nibbles | |
decode_data(lower_nibbles) # from this data work out what numbers are displayed on screen | |
time.sleep(0.5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment