• 热门专题

python串口编程之传感器数据采集模块和位移计

作者:niexining   发布日期:2012-10-22 09:08:52
Tag标签:python  串口编程  
  • #coding:utf-8
    
    import serial
    
    from datetime import datetime
    
    from pyExcelerator import *
    
    import time
    
    
    
    f_art = lambda x : [x[i]*256+x[i+1] for i in range(len(x))[3:len(x)-2:4]]
    
    f_kerui = lambda x : [x[i]*256+x[i+1] for i in range(len(x))[3:len(x)-2:2]]
    
    def f_jinma(x):
    
       ws = [hex(i)[-2:] for i in x]
    
       a = []
    
       for i in range(13,len(ws)):
    
          if ws[i][0]=='3': a.append(ws[i][1])
    
          else: break
    
       b = []
    
       for j in range(i+1,len(ws)):
    
          if ws[j]=='2d': b.append('-')
    
          elif ws[j][0]=='3': b.append(ws[j][1])
    
          else: break
    
       return [int(''.join(a))/100.0,int(''.join(b))/100.0]
    
          
    
       
    
       a = int(''.join(ws[13:17]),10)/100.0
    
       if ws[18]=='d': b = - int(''.join(ws[19:23]),10)/100.0
    
       else: b = int(''.join(ws[18:22]),10)/100.0
    
       return [a,b]
    
    
    
    def crc16(x):
    
        b = 0xA001
    
        a = 0xFFFF
    
        for byte in x:
    
            a = a^byte
    
            for i in range(8):
    
                last = a%2
    
                a = a>>1
    
                if last ==1: a = a^b
    
        aa = '0'*(6-len(hex(a)))+hex(a)[2:]
    
        ll,hh = int(aa[:2],16),int(aa[2:],16)
    
        #print hex(hh),hex(ll)
    
        return [hh,ll] 
    
    
    
    
    
    def art_sort(ser,address,times):
    
        w = [address,0x03,0x00,0x80,0x00,0x06] #判断阿尔泰模块类型
    
        s = ''.join([chr(i) for i in w+crc16(w)])
    
        for i in range(times):
    
            ser.write(s)
    
            r = [ord(i) for i in ser.read(21)]
    
            if r and r[0]==address and r[1]==0x03:
    
                sort = "%s%s" %(hex(r[3])[2:], hex(r[4])[2:])
    
                print u"地址%s, %s" %(address,sort)
    
                return sort
    
        sort = ''
    
        print u"地址%s, %s" %(address,sort)
    
        return sort
    
    
    
    def read_3058(ser,address,times):
    
        for channel in range(8):
    
            w = [address,0x10,0x01,channel,0x00,0x01,0x02,0x00,0x0c] #设置量程 4~20ma
    
            s = ''.join([chr(i) for i in w+crc16(w)])
    
            ser.write(s)
    
            ser.read(8)
    
                
    
        w = [address,0x04,0x01,0x00,0x00,0x10] #读数据,8通道
    
        s = ''.join([chr(i) for i in w+crc16(w)])
    
        for i in range(times):
    
            ser.write(s)
    
            r = [ord(i) for i in ser.read(37)]
    
            if r and r[0]==address and r[1]==0x04:
    
                code = f_art(r)
    
                print [float('%.1f' %(c/65536.0*16+4)) for c in code]
    
                return [float('%.1f' %(c/65536.0*16+4)) for c in code]
    
        print []
    
        return [] 
    
    
    
    def read_3046(ser,address,times):
    
        for channel in range(6):
    
            w = [address,0x10,0x01,channel,0x00,0x01,0x02,0x00,0x20] #设置量程 -200~600.C
    
            s = ''.join([chr(i) for i in w+crc16(w)])
    
            ser.write(s)
    
            ser.read(8)
    
        w = [address,0x04,0x01,0x00,0x00,0x0c] #读数据,6通道
    
        s = ''.join([chr(i) for i in w+crc16(w)])
    
        for i in range(times):
    
            ser.write(s)
    
            r = [ord(i) for i in ser.read(29)]
    
            if r and r[0]==address and r[1]==0x04:
    
                code = f_art(r)
    
                print [float('%.1f' %(c/65536.0*800-200)) for c in code]
    
                return [float('%.1f' %(c/65536.0*800-200)) for c in code]
    
        print []
    
        return []
    
    
    
    def read_art_module(com,addresses):
    
        ser=serial.Serial()
    
        ser.baudrate=2400
    
        ser.port=com-1
    
        ser.timeout=30
    
        ser.open()
    
        print u"阿尔泰模块"
    
        w="0x0f 0x0f 0x00 0x00 0x00 0x04 0x01 0x01 0x00 0x5a 0x20".split() #打开继电器
    
        s = ''.join([chr(int(i,16)) for i in w])
    
        ser.write(s)
    
        print u'正在打开继电器...'
    
        ser.read(8)
    
        result = {}
    
        for address in addresses:
    
            sort = art_sort(ser,address,2)
    
            if sort == '3058': data = read_3058(ser,address,2)
    
            elif sort == '3046': data = read_3046(ser,address,2)
    
            else: data = []
    
            result[(sort,address,datetime.now())] = data
    
        w="0f 0f 00 00 00 04 01 00 00 5b b0".split() #关闭继电器
    
        s = ''.join([chr(int(i,16)) for i in w])
    
        ser.write(s)
    
        print u'正在关闭继电器...'
    
        ser.read(8)
    
        ser.close()
    
        return result
    
    
    
    def kerui_sort(ser,address,times):
    
        w = [address,0x17,0x00,0x00,0x00,0x07] #判断科瑞模块类型
    
        s = ''.join([chr(i) for i in w+crc16(w)])
    
        for i in range(times):
    
            ser.write(s)
    
            r = [ord(i) for i in ser.read(12)]
    
            if r and r[0]==address and r[1]==0x17:
    
                sort = '8511'
    
                print u"地址%s, %s" %(address,sort)
    
                return sort
    
        sort = ''
    
        print u"地址%s, %s" %(address,sort)
    
        return sort
    
        
    
    def read_8511(ser,address,times):
    
        w = [address,0x04,0x00,0x00,0x00,0x06] 
    
        s = ''.join([chr(i) for i in w+crc16(w)])
    
        for i in range(times):
    
            ser.write(s)
    
            r = [ord(i) for i in ser.read(17)]
    
            if r and r[0]==address and r[1]==0x04:
    
                code = f_kerui(r)
    
                print [float('%.1f' %(c/100.0)) for c in code]
    
                return [float('%.1f' %(c/100.0)) for c in code]
    
        print []
    
        return []
    
        
    
    def read_kerui_module(com,addresses):
    
        ser=serial.Serial()
    
        ser.baudrate=2400
    
        ser.port=com-1
    
        ser.timeout=30
    
        ser.open()
    
        print u"科瑞模块"
    
        w="0F 05 00 00 FF 00 8D 14".split() #打开继电器
    
        s = ''.join([chr(int(i,16)) for i in w])
    
        ser.write(s)
    
        print u'正在打开继电器...'
    
        ser.read(20)
    
        result = {}
    
        for address in addresses:
    
            sort = kerui_sort(ser,address,2)
    
            if sort == '8511': data = read_8511(ser,address,2)
    
            else: data = []
    
            result[(sort,address,datetime.now())] = data
    
        w="0F 05 00 00 00 00 CC E4".split() #关闭继电器
    
        s = ''.join([chr(int(i,16)) for i in w])
    
        ser.write(s)
    
        print u'正在关闭继电器...'
    
        ser.read(20)
    
        ser.close()
    
        return result
    
    
    
    def read_j(ser,address,times):
    
        w = [0x23]+[int('3'+i,16) for i in address]+[0x41,0xde,0x21]
    
        s = ''.join([chr(i) for i in w])
    
        for i in range(times):
    
            ser.write(s)
    
            r = [ord(i) for i in ser.read(27)]
    
            if r and r[0]==0x24 and ''.join([hex(i)[-1] for i in r][4:12])==address:
    
               data = f_jinma(r)
    
               print data
    
               return data
    
        print []
    
        return []
    
    
    
    def read_jinma(com,addresses):
    
        ser=serial.Serial()
    
        ser.baudrate=2400
    
        ser.port=com-1
    
        ser.timeout=30
    
        ser.open()
    
        print u"金马位移计"
    
        if com in art_table.keys(): w = "0x0f 0x0f 0x00 0x00 0x00 0x04 0x01 0x01 0x00 0x5a 0x20".split()
    
        elif com in kerui_table.keys(): w = "0F 05 00 00 FF 00 8D 14".split()
    
        s = ''.join([chr(int(i,16)) for i in w])
    
        ser.write(s)
    
        print u'正在打开继电器...'
    
        ser.read(20)
    
        result = {}
    
        for address in addresses:
    
           print u"金马 %s" %(address)
    
           data = read_j(ser,address,2)
    
           result[(u'金马',address,datetime.now())] = data
    
        if com in art_table.keys(): w = "0f 0f 00 00 00 04 01 00 00 5b b0".split()
    
        elif com in kerui_table.keys(): w = "0F 05 00 00 00 00 CC E4".split() 
    
        s = ''.join([chr(int(i,16)) for i in w])
    
        ser.write(s)
    
        print u'正在关闭继电器...'
    
        ser.read(20)
    
        ser.close()
    
        return result
    
        
    
    
    
    art_table = {6:[23,37,48,50,51,52,59,60,61,68,69,70],
    
                 18:[39,47,83,84,85,86,87,89],
    
                 8:[17,26,36,59,60,61,65,66,67,68,69,70,71,72,73],
    
                 9:[20,29,33,74,75,76,77,78,79,81,82],
    
                 12:[18,37,38,74,75,76,77,78,79,83,84,85],
    
                 26:[28,45,59,60,61,86,87,88],
    
                 7:[17,19,34,68,69,70,71,72,73,74,75,76,77,78,79],
    
                 27:[32,35,38,71,72,73,83,84,85,86,87,88],
    
                 17:[22,29,44,77,78,79,80,81,82,83,84,85],
    
                 11:[20,30,62,63,64,74,75,76],
    
                 10:[18,21,39,41,53,54,55,56,57,58,71,72,73,80,81,82],
    
                 24:[19,30,44,62,63,64,81,82,83,84,85,89],
    
                 22:[16,42,49,50,51,52,53,54,55,88,90,91],
    
                 25:[16,46,50,51,52,56,57,58],
    
                 21:[22,23,35,46,56,57,58,65,66,67,68,69,70,80,81,82],
    
                 23:[24,32,40,56,57,58,62,63,64,86,87,88],
    
                 5:[25,26,40,56,57,58,65,66,67,68,69,70],
    
                 14:[33,36,59,60,61,86,87,89],
    
                 19:[21,34,43,45,50,51,52,53,54,55,71,72,73,74,75,76],
    
                 13:[25,27,31,59,60,61,62,63,64,77,78,79],
    
             }
    
    
    
    kerui_table = {2:[4,5,6,7,8,9,29,30,31],
    
                   1:[4,5,6,7,8,9,19,28,31],
    
                   4:[5,6,7,8,9,16,17,18,24],
    
                   3:[4,5,6,7,8,9,16,17,18],
    
                   28:[7,8,9,16,19,20],
    
                   15:[4,5,6,20,21,23],
    
                   30:[17,18,19,20,21,22,23,24,30],
    
                   31:[19,20,21,22,23,24,25,26,27],
    
                   34:[21,22,23,24,26,27,28,29,30],
    
                   16:[20,21,25,26,27,28,29,30,31],
    
                   29:[16,17,18,25,26,27,28,29,30],
    
                   32:[16,17,18,23,25,26,28,29,31],
    
                   }
    
    
    
    jinma_table = {6:['00618648','00618658','00618666','00618394'],
    
                   18:['00618313','00618675'],
    
                   8:['00618678','00618661','00618341','00618335','00618393','00618340'],
    
                   9:['00618679','00618682','06118655','00618329'],
    
                   12:['00618662','00618328','00618656','00618694'],
    
                   26:['00618676','00618336'],
    
                   7:['00618322','00618659','00618680','00618321','00618654','00618657'],
    
                   27:['00618660','00618664','00618339','00618325'],
    
                   17:['00619162','00618646','00619157','00618690'],
    
                   11:['00619181','00619202'],
    
                   10:['00619171','00618650','00618337','00619166','00618651','00618645'],
    
                   24:['00618647','00618323','00619183','00619158'],
    
                   22:['00618338','00619180','00619156','00619175'],
    
                   25:['00618315','00619159'],
    
                   21:['00618312','00619190','00618686','00619163','00618331','00618334'],
    
                   23:['00619192','00619177','00618681','00619176'],
    
                   5:['00618683','00618684','00618389','00618667'],
    
                   14:['00618390','00618391'],
    
                   19:['00618663','00618669','00618672','00618333','00618670','00618327'],
    
                   13:['00618392','00618311','00618668','00618324'],
    
                   2:['00619155','00618653','00618652','00618330'],
    
                   1:['00619187','00619195','00618677','00619172'],
    
                   4:['00619194','00619199'],
    
                   3:['00619188','00619173'],
    
                   28:['00618649','00619198'],
    
                   15:['00619165','00619168'],
    
                   30:['00619191','00619196','00619197','00619200'],
    
                   31:['00619186','00619185','00619154','00619161'],
    
                   34:['00619164','00619179','00619167','00619184'],
    
                   16:['00619201','00619170','00619193','00619174'],
    
                   29:['00619178','00618692','00619160','00618693'],
    
                   32:['00619182','00619189','00619169','00618689'],
    
                   
    
                   }
    
    
    
    def write_result(result,w,sheet):
    
        ws = w.add_sheet(sheet)
    
        ks = result.keys()
    
        ks.sort()
    
        row = 0 
    
        for k in ks:
    
           sort,address,datetime = k
    
           ws.write(row,0,sort)
    
           ws.write(row,1,address)
    
           style = XFStyle()
    
           style.num_format_str = 'M/D/YY h:mm'
    
           ws.write(row,2,datetime,style)
    
           col = 3
    
           for d in result[k]:
    
              ws.write(row,col,d)
    
              col += 1
    
           row += 1
    
       
    
    
    
    if __name__=="__main__":
    
        
    
        art_ks = art_table.keys()
    
        kerui_ks = kerui_table.keys()
    
        k = input('port: ')
    
        print u'默认位移计, 模块输入m'
    
        t = raw_input(': ')
    
        if t == 'm':
    
           if k in art_ks: result = read_art_module(k,art_table[k])
    
           elif k in kerui_ks: result = read_kerui_module(k,kerui_table[k])
    
        else:
    
           result = read_jinma(k,jinma_table[k]) 
    
        w = Workbook()
    
        write_result(result,w,u'串口%s' %(k))
    
        w.save(u'单个串口%s.xls' %(time.time()))
    
    
    


     

About IT165 - 广告服务 - 隐私声明 - 版权申明 - 免责条款 - 网站地图 - 网友投稿 - 联系方式
本站内容来自于互联网,仅供用于网络技术学习,学习中请遵循相关法律法规