Chinaunix首页 | 论坛 | 博客
  • 博客访问: 156488
  • 博文数量: 33
  • 博客积分: 2057
  • 博客等级: 大尉
  • 技术积分: 430
  • 用 户 组: 普通用户
  • 注册时间: 2007-11-19 16:37
文章分类
文章存档

2013年(2)

2012年(23)

2011年(8)

分类: Python/Ruby

2012-10-21 16:05:09


点击(此处)折叠或打开

  1. #coding:utf-8
  2. import serial
  3. from datetime import datetime
  4. from pyExcelerator import *
  5. import time

  6. f_art = lambda x : [x[i]*256+x[i+1] for i in range(len(x))[3:len(x)-2:4]]
  7. f_kerui = lambda x : [x[i]*256+x[i+1] for i in range(len(x))[3:len(x)-2:2]]
  8. def f_jinma(x):
  9.    ws = [hex(i)[-2:] for i in x]
  10.    a = []
  11.    for i in range(13,len(ws)):
  12.       if ws[i][0]=='3': a.append(ws[i][1])
  13.       else: break
  14.    b = []
  15.    for j in range(i+1,len(ws)):
  16.       if ws[j]=='2d': b.append('-')
  17.       elif ws[j][0]=='3': b.append(ws[j][1])
  18.       else: break
  19.    return [int(''.join(a))/100.0,int(''.join(b))/100.0]
  20.       
  21.    
  22.    a = int(''.join(ws[13:17]),10)/100.0
  23.    if ws[18]=='d': b = - int(''.join(ws[19:23]),10)/100.0
  24.    else: b = int(''.join(ws[18:22]),10)/100.0
  25.    return [a,b]

  26. def crc16(x):
  27.     b = 0xA001
  28.     a = 0xFFFF
  29.     for byte in x:
  30.         a = a^byte
  31.         for i in range(8):
  32.             last = a%2
  33.             a = a>>1
  34.             if last ==1: a = a^b
  35.     aa = '0'*(6-len(hex(a)))+hex(a)[2:]
  36.     ll,hh = int(aa[:2],16),int(aa[2:],16)
  37.     #print hex(hh),hex(ll)
  38.     return [hh,ll]


  39. def art_sort(ser,address,times):
  40.     w = [address,0x03,0x00,0x80,0x00,0x06] #判断阿尔泰模块类型
  41.     s = ''.join([chr(i) for i in w+crc16(w)])
  42.     for i in range(times):
  43.         ser.write(s)
  44.         r = [ord(i) for i in ser.read(21)]
  45.         if r and r[0]==address and r[1]==0x03:
  46.             sort = "%s%s" %(hex(r[3])[2:], hex(r[4])[2:])
  47.             print u"地址%s, %s" %(address,sort)
  48.             return sort
  49.     sort = ''
  50.     print u"地址%s, %s" %(address,sort)
  51.     return sort

  52. def read_3058(ser,address,times):
  53.     for channel in range(8):
  54.         w = [address,0x10,0x01,channel,0x00,0x01,0x02,0x00,0x0c] #设置量程 4~20ma
  55.         s = ''.join([chr(i) for i in w+crc16(w)])
  56.         ser.write(s)
  57.         ser.read(8)
  58.             
  59.     w = [address,0x04,0x01,0x00,0x00,0x10] #读数据,8通道
  60.     s = ''.join([chr(i) for i in w+crc16(w)])
  61.     for i in range(times):
  62.         ser.write(s)
  63.         r = [ord(i) for i in ser.read(37)]
  64.         if r and r[0]==address and r[1]==0x04:
  65.             code = f_art(r)
  66.             print [float('%.1f' %(c/65536.0*16+4)) for c in code]
  67.             return [float('%.1f' %(c/65536.0*16+4)) for c in code]
  68.     print []
  69.     return []

  70. def read_3046(ser,address,times):
  71.     for channel in range(6):
  72.         w = [address,0x10,0x01,channel,0x00,0x01,0x02,0x00,0x20] #设置量程 -200~600.C
  73.         s = ''.join([chr(i) for i in w+crc16(w)])
  74.         ser.write(s)
  75.         ser.read(8)
  76.     w = [address,0x04,0x01,0x00,0x00,0x0c] #读数据,6通道
  77.     s = ''.join([chr(i) for i in w+crc16(w)])
  78.     for i in range(times):
  79.         ser.write(s)
  80.         r = [ord(i) for i in ser.read(29)]
  81.         if r and r[0]==address and r[1]==0x04:
  82.             code = f_art(r)
  83.             print [float('%.1f' %(c/65536.0*800-200)) for c in code]
  84.             return [float('%.1f' %(c/65536.0*800-200)) for c in code]
  85.     print []
  86.     return []

  87. def read_art_module(com,addresses):
  88.     ser=serial.Serial()
  89.     ser.baudrate=2400
  90.     ser.port=com-1
  91.     ser.timeout=30
  92.     ser.open()
  93.     print u"阿尔泰模块"
  94.     w="0x0f 0x0f 0x00 0x00 0x00 0x04 0x01 0x01 0x00 0x5a 0x20".split() #打开继电器
  95.     s = ''.join([chr(int(i,16)) for i in w])
  96.     ser.write(s)
  97.     print u'正在打开继电器...'
  98.     ser.read(8)
  99.     result = {}
  100.     for address in addresses:
  101.         sort = art_sort(ser,address,2)
  102.         if sort == '3058': data = read_3058(ser,address,2)
  103.         elif sort == '3046': data = read_3046(ser,address,2)
  104.         else: data = []
  105.         result[(sort,address,datetime.now())] = data
  106.     w="0f 0f 00 00 00 04 01 00 00 5b b0".split() #关闭继电器
  107.     s = ''.join([chr(int(i,16)) for i in w])
  108.     ser.write(s)
  109.     print u'正在关闭继电器...'
  110.     ser.read(8)
  111.     ser.close()
  112.     return result

  113. def kerui_sort(ser,address,times):
  114.     w = [address,0x17,0x00,0x00,0x00,0x07] #判断科瑞模块类型
  115.     s = ''.join([chr(i) for i in w+crc16(w)])
  116.     for i in range(times):
  117.         ser.write(s)
  118.         r = [ord(i) for i in ser.read(12)]
  119.         if r and r[0]==address and r[1]==0x17:
  120.             sort = '8511'
  121.             print u"地址%s, %s" %(address,sort)
  122.             return sort
  123.     sort = ''
  124.     print u"地址%s, %s" %(address,sort)
  125.     return sort
  126.     
  127. def read_8511(ser,address,times):
  128.     w = [address,0x04,0x00,0x00,0x00,0x06]
  129.     s = ''.join([chr(i) for i in w+crc16(w)])
  130.     for i in range(times):
  131.         ser.write(s)
  132.         r = [ord(i) for i in ser.read(17)]
  133.         if r and r[0]==address and r[1]==0x04:
  134.             code = f_kerui(r)
  135.             print [float('%.1f' %(c/100.0)) for c in code]
  136.             return [float('%.1f' %(c/100.0)) for c in code]
  137.     print []
  138.     return []
  139.     
  140. def read_kerui_module(com,addresses):
  141.     ser=serial.Serial()
  142.     ser.baudrate=2400
  143.     ser.port=com-1
  144.     ser.timeout=30
  145.     ser.open()
  146.     print u"科瑞模块"
  147.     w="0F 05 00 00 FF 00 8D 14".split() #打开继电器
  148.     s = ''.join([chr(int(i,16)) for i in w])
  149.     ser.write(s)
  150.     print u'正在打开继电器...'
  151.     ser.read(20)
  152.     result = {}
  153.     for address in addresses:
  154.         sort = kerui_sort(ser,address,2)
  155.         if sort == '8511': data = read_8511(ser,address,2)
  156.         else: data = []
  157.         result[(sort,address,datetime.now())] = data
  158.     w="0F 05 00 00 00 00 CC E4".split() #关闭继电器
  159.     s = ''.join([chr(int(i,16)) for i in w])
  160.     ser.write(s)
  161.     print u'正在关闭继电器...'
  162.     ser.read(20)
  163.     ser.close()
  164.     return result

  165. def read_j(ser,address,times):
  166.     w = [0x23]+[int('3'+i,16) for i in address]+[0x41,0xde,0x21]
  167.     s = ''.join([chr(i) for i in w])
  168.     for i in range(times):
  169.         ser.write(s)
  170.         r = [ord(i) for i in ser.read(27)]
  171.         if r and r[0]==0x24 and ''.join([hex(i)[-1] for i in r][4:12])==address:
  172.            data = f_jinma(r)
  173.            print data
  174.            return data
  175.     print []
  176.     return []

  177. def read_jinma(com,addresses):
  178.     ser=serial.Serial()
  179.     ser.baudrate=2400
  180.     ser.port=com-1
  181.     ser.timeout=30
  182.     ser.open()
  183.     print u"金马位移计"
  184.     if com in art_table.keys(): w = "0x0f 0x0f 0x00 0x00 0x00 0x04 0x01 0x01 0x00 0x5a 0x20".split()
  185.     elif com in kerui_table.keys(): w = "0F 05 00 00 FF 00 8D 14".split()
  186.     s = ''.join([chr(int(i,16)) for i in w])
  187.     ser.write(s)
  188.     print u'正在打开继电器...'
  189.     ser.read(20)
  190.     result = {}
  191.     for address in addresses:
  192.        print u"金马 %s" %(address)
  193.        data = read_j(ser,address,2)
  194.        result[(u'金马',address,datetime.now())] = data
  195.     if com in art_table.keys(): w = "0f 0f 00 00 00 04 01 00 00 5b b0".split()
  196.     elif com in kerui_table.keys(): w = "0F 05 00 00 00 00 CC E4".split()
  197.     s = ''.join([chr(int(i,16)) for i in w])
  198.     ser.write(s)
  199.     print u'正在关闭继电器...'
  200.     ser.read(20)
  201.     ser.close()
  202.     return result
  203.     

  204. art_table = {6:[23,37,48,50,51,52,59,60,61,68,69,70],
  205.              18:[39,47,83,84,85,86,87,89],
  206.              8:[17,26,36,59,60,61,65,66,67,68,69,70,71,72,73],
  207.              9:[20,29,33,74,75,76,77,78,79,81,82],
  208.              12:[18,37,38,74,75,76,77,78,79,83,84,85],
  209.              26:[28,45,59,60,61,86,87,88],
  210.              7:[17,19,34,68,69,70,71,72,73,74,75,76,77,78,79],
  211.              27:[32,35,38,71,72,73,83,84,85,86,87,88],
  212.              17:[22,29,44,77,78,79,80,81,82,83,84,85],
  213.              11:[20,30,62,63,64,74,75,76],
  214.              10:[18,21,39,41,53,54,55,56,57,58,71,72,73,80,81,82],
  215.              24:[19,30,44,62,63,64,81,82,83,84,85,89],
  216.              22:[16,42,49,50,51,52,53,54,55,88,90,91],
  217.              25:[16,46,50,51,52,56,57,58],
  218.              21:[22,23,35,46,56,57,58,65,66,67,68,69,70,80,81,82],
  219.              23:[24,32,40,56,57,58,62,63,64,86,87,88],
  220.              5:[25,26,40,56,57,58,65,66,67,68,69,70],
  221.              14:[33,36,59,60,61,86,87,89],
  222.              19:[21,34,43,45,50,51,52,53,54,55,71,72,73,74,75,76],
  223.              13:[25,27,31,59,60,61,62,63,64,77,78,79],
  224.          }

  225. kerui_table = {2:[4,5,6,7,8,9,29,30,31],
  226.                1:[4,5,6,7,8,9,19,28,31],
  227.                4:[5,6,7,8,9,16,17,18,24],
  228.                3:[4,5,6,7,8,9,16,17,18],
  229.                28:[7,8,9,16,19,20],
  230.                15:[4,5,6,20,21,23],
  231.                30:[17,18,19,20,21,22,23,24,30],
  232.                31:[19,20,21,22,23,24,25,26,27],
  233.                34:[21,22,23,24,26,27,28,29,30],
  234.                16:[20,21,25,26,27,28,29,30,31],
  235.                29:[16,17,18,25,26,27,28,29,30],
  236.                32:[16,17,18,23,25,26,28,29,31],
  237.                }

  238. jinma_table = {6:['00618648','00618658','00618666','00618394'],
  239.                18:['00618313','00618675'],
  240.                8:['00618678','00618661','00618341','00618335','00618393','00618340'],
  241.                9:['00618679','00618682','06118655','00618329'],
  242.                12:['00618662','00618328','00618656','00618694'],
  243.                26:['00618676','00618336'],
  244.                7:['00618322','00618659','00618680','00618321','00618654','00618657'],
  245.                27:['00618660','00618664','00618339','00618325'],
  246.                17:['00619162','00618646','00619157','00618690'],
  247.                11:['00619181','00619202'],
  248.                10:['00619171','00618650','00618337','00619166','00618651','00618645'],
  249.                24:['00618647','00618323','00619183','00619158'],
  250.                22:['00618338','00619180','00619156','00619175'],
  251.                25:['00618315','00619159'],
  252.                21:['00618312','00619190','00618686','00619163','00618331','00618334'],
  253.                23:['00619192','00619177','00618681','00619176'],
  254.                5:['00618683','00618684','00618389','00618667'],
  255.                14:['00618390','00618391'],
  256.                19:['00618663','00618669','00618672','00618333','00618670','00618327'],
  257.                13:['00618392','00618311','00618668','00618324'],
  258.                2:['00619155','00618653','00618652','00618330'],
  259.                1:['00619187','00619195','00618677','00619172'],
  260.                4:['00619194','00619199'],
  261.                3:['00619188','00619173'],
  262.                28:['00618649','00619198'],
  263.                15:['00619165','00619168'],
  264.                30:['00619191','00619196','00619197','00619200'],
  265.                31:['00619186','00619185','00619154','00619161'],
  266.                34:['00619164','00619179','00619167','00619184'],
  267.                16:['00619201','00619170','00619193','00619174'],
  268.                29:['00619178','00618692','00619160','00618693'],
  269.                32:['00619182','00619189','00619169','00618689'],
  270.                
  271.                }

  272. def write_result(result,w,sheet):
  273.     ws = w.add_sheet(sheet)
  274.     ks = result.keys()
  275.     ks.sort()
  276.     row = 0
  277.     for k in ks:
  278.        sort,address,datetime = k
  279.        ws.write(row,0,sort)
  280.        ws.write(row,1,address)
  281.        style = XFStyle()
  282.        style.num_format_str = 'M/D/YY h:mm'
  283.        ws.write(row,2,datetime,style)
  284.        col = 3
  285.        for d in result[k]:
  286.           ws.write(row,col,d)
  287.           col += 1
  288.        row += 1
  289.    

  290. if __name__=="__main__":
  291.     
  292.     art_ks = art_table.keys()
  293.     kerui_ks = kerui_table.keys()
  294.     k = input('port: ')
  295.     print u'默认位移计, 模块输入m'
  296.     t = raw_input(': ')
  297.     if t == 'm':
  298.        if k in art_ks: result = read_art_module(k,art_table[k])
  299.        elif k in kerui_ks: result = read_kerui_module(k,kerui_table[k])
  300.     else:
  301.        result = read_jinma(k,jinma_table[k])
  302.     w = Workbook()
  303.     write_result(result,w,u'串口%s' %(k))
  304.     w.save(u'单个串口%s.xls' %(time.time()))

阅读(3215) | 评论(0) | 转发(0) |
0

上一篇:crc16 python程序

下一篇:python调用vba宏

给主人留下些什么吧!~~