#!/usr/bin/env python ''' temper232 Reader for Linux/Unix Copyright (C) 2008 maluyao@gmail.com Many Thanks to Dr. Lifeng Zhao This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation - version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. ''' ''' Dependencies : Python,pyserial Fedora/RHEL/CentOS run: sudo yum install pyserial Ubuntu/Debian run: sudo apt-get install python-serial FreeBSD 7.1 run: sudo pkg_add -r py25-serial ''' # Input characters are sent directly (only LF -> CR/LF/CRLF translation is # done), received characters are displayed as is (or as trough pythons # repr, useful for debug purposes) # Baudrate and echo configuartion is done through globals #import sys, os, serial, threading, getopt import sys, os, serial, getopt,base64,time,string #EXITCHARCTER = '\x04' #ctrl+D CONVERT_CRLF = 2 CONVERT_CR = 1 CONVERT_LF = 0 def ByteToHex( byteStr ): hex = [] for aChar in byteStr: hex.append( "%02X " % ord( aChar ) ) return ''.join( hex ).strip() # a short help message def usage(): sys.stderr.write("""USAGE: %s [options] Temper232 Reader for Linux/Unix options: -p, --port=PORT: port, a number, default = /dev/ttyUSB0 or a device name -b, --baud=BAUD: baudrate, default 4800 -r, --rtscts: enable RTS/CTS flow control (default off) -x, --xonxoff: enable software flow control (default off) -e, --echo: enable local echo (default off) -c, --cr: do not send CR+LF, send CR only -n, --newline: do not send CR+LF, send LF only -D, --debug: debug received data (escape nonprintable chars) """ % (sys.argv[0], )) if __name__ == '__main__': #initialize with defaults # port = '/dev/ttyUSB0' if sys.platform == 'linux2': # print "This is Linux System" port = '/dev/ttyUSB0' if sys.platform == 'freebsd6': print "This is Freebsd 6" port = '/dev/ttyUSB0' if sys.platform == 'freebsd7': print "This is Freebsd 7" port = '/dev/ttyUSB0' # print "The port is", port # print sys.platform baudrate = 4800 echo = 0 convert_outgoing = CONVERT_CRLF rtscts = 0 xonxoff = 0 repr_mode = 0 #parse command line options try: opts, args = getopt.getopt(sys.argv[1:], "hp:b:rxecnD", ["help", "port=", "baud=", "rtscts", "xonxoff", "echo", "cr", "newline", "debug"] ) except getopt.GetoptError: # print help information and exit: usage() sys.exit(2) for o, a in opts: if o in ("-h", "--help"): #help text usage() sys.exit() elif o in ("-p", "--port"): #specified port try: port = int(a) except ValueError: port = a elif o in ("-b", "--baud"): #specified baudrate try: baudrate = int(a) except ValueError: raise ValueError, "Baudrate must be a integer number, not %r" % a elif o in ("-r", "--rtscts"): rtscts = 1 elif o in ("-x", "--xonxoff"): xonxoff = 1 elif o in ("-e", "--echo"): echo = 1 elif o in ("-c", "--cr"): convert_outgoing = CONVERT_CR elif o in ("-n", "--newline"): convert_outgoing = CONVERT_LF elif o in ("-D", "--debug"): repr_mode = 1 #open the port try: #ser = serial.Serial(port, baudrate, rtscts=rtscts, xonxoff=xonxoff,timeout=10) ser = serial.Serial(port, baudrate, rtscts=rtscts, xonxoff=xonxoff,timeout=5) except: sys.stderr.write("Could not open port\n") sys.exit(1) # sys.stderr.write("--- Miniterm --- type Ctrl-D to quit\n") #start serial->console thread # 24 10 05 01 01 00 32 01 55 # ser.write(ByteToHex('24 10 05 01 01 00 32 01 55\n')) # print ByteToHex('24 10 05 01 01 00 32 01 55') # quit() instr=base64.b16decode('241005010100320155') # print instr # quit() ser.write(instr) # time.sleep(1) s=ser.read(16) allstring='' for i in range (0,15): allstring=allstring+base64.b16encode(s[i]) for i in range (0,15): o1=base64.b16encode(s[i]) o2=base64.b16encode(s[i+1]) o3=base64.b16encode(s[i+2]) o5=base64.b16encode(s[i+5]) # if ( base64.b16encode(s[i]) == '24' and base64.b16encode(s[i+1]) =='FE' and base64.b16encode(s[i+2]) == '02'): if (o1=='24' and o2=='FE' and o3=='02' and o5=='55'): datastart=i+3 # print ('The start is ', datastart) break # print (base64.b16encode(s[datastart]),base64.b16encode(s[datastart+1])) # print allstring ser.close() HH=int(('0x'+base64.b16encode(s[datastart])),16) LL=int(('0x'+base64.b16encode(s[datastart+1])),16) # outdata2=str(base64.b16encode(s[datastart+1])) # print HH,LL xtemp=256*HH + LL if xtemp > 0x4000: temper=-(0x4000-(xtemp/4))*0.03125 else: temper=(xtemp/4)*0.03125 print int(round(temper))