Esempio n. 1
0
 def tcpread(self,data,addr,cmd):
     #print('tcpread',self.index,addr,cmd,data)
     if len(data)>7 and self.index==bytesToint(data[0:2]):
         #num=(data[4]<<8)+data[5]
         if addr==data[6] and cmd==data[7]:
             if cmd==3:
                 return bytesToint(data[9:9+data[8]])
             elif cmd==6:
                 if len(data)>11:
                     return (data[10]<<8)+data[11]
                 else:
                     return 0
Esempio n. 2
0
File: api.py Progetto: hdqlife/blink
 def Write(self,command,data,addr,index,num,**kwargs):
     return None
     DLL.modbus_set_slave(self.ctx,addr)
     rt=None
     if DLL.modbus_connect(self.ctx)==-1:
         return None
     if command==1:
         destbit=(c_ubyte*num)()
         DLL.modbus_read_bits(self.ctx, index, num, destbit)
     elif command==2:
         destbit=(c_ubyte*num)()
         DLL.modbus_read_input_bits(self.ctx, index, num, destbit)
     elif command==3:
         destbit=(c_int64*num)()
         destbit[0]=3
         DLL.modbus_read_registers(self.ctx, index, num, destbit)
     elif command==4:
         destbit=(c_ushort*num)()
         DLL.modbus_read_input_registers(self.ctx, index, num,destbit)
     elif command==5:
         rt=DLL.modbus_write_bit(self.ctx,index,num)
     elif command==6:
         rt=DLL.modbus_write_register(self.ctx, index, kwargs['value'])
     elif command==15:
         destbit=(c_ubyte*num)(*data)
         rt=DLL.modbus_write_bits(self.ctx, index, num,destbit)
     elif command==10:
         destbit=(c_ubyte*num)(*data)
         rt=DLL.modbus_write_registers(self.ctx, index, num,  destbit)
     if rt==-1:return None
     if rt is None:
         rt=function.bytesToint(destbit,mode="big")
     DLL.modbus_close(self.ctx)
     return rt
Esempio n. 3
0
 def rturead(self,data,addr,cmd):
     all_num=len(data)
     for i in range(all_num-2):
         if data[i]==addr:
             if data[i+1]==3:
                 crc_begin=i+5
             else:
                 crc_begin=i+6
             if crc_begin+1<all_num:
                 crc_value1,crc_value2=data[crc_begin],data[crc_begin+1]
                 crc_value=crc16(data[i:crc_begin])
                 if crc_value[0]==crc_value1 and crc_value[1]==crc_value2:
                     if data[i+1]==cmd:
                         if cmd==3:
                             num=data[i+2]
                             return bytesToint(data[i+3:i+3+num])
                         else:
                             return 1
     return -1
Esempio n. 4
0
File: api.py Progetto: hdqlife/blink
 def deviceRead(self,path,index,openMode,**kwargs):
     if path=='/virture/value':
         return kwargs['value']
     data=self.regValue[path[:-1]]
     if path=='/dev/ad7795':
         mp=[5,4,3,2,1,6]
         data[3]=mp[int(kwargs['Id'][-1:])]
         #function.log('read',kwargs['Id'],data)
     value=self.Write(path,openMode,data)
     rt=kwargs['value']
     if value[0]==0:
         if path=='/dev/iocontrol':
             rt=value[8-index]          
         elif path=='/dev/ad7795':
             if value[1]==0:
                 rt=kwargs['value']
             else:
                 rt=function.bytesToint(value[2:4],mode="small")
         #function.log(path,openMode,kwargs['defaultValue'],rt,value,index,num)
     #print('read',path,rt)
     return rt