def _end_request(self, sock, data): if isinstance(data, ErrorCode): data = str(data) n = len(data) + 8 data = '{:04X}{}{}'.format(n, data, computeCRC(data)) # self.debug('response len={}'.format(n)) if globalv.use_ipc: try: if globalv.ipc_dgram: func = lambda x: sock.sendto(x, self.path) else: func = lambda x: sock.send(x) mlen = len(data) totalsent = 0 while totalsent < mlen: try: totalsent += func(data[totalsent:]) # self.debug('totalsent={} n={}'.format(totalsent, mlen)) except socket.error: continue except Exception, err: self.debug('End Request Exception: {}'.format(err))
def _recvall(self, recv, datasize=None, frame=None): """ recv: callable that accepts 1 argument (datasize). should return a str """ # ss = [] sum = 0 # disable message len checking # msg_len = 1 # if self.use_message_len_checking: # msg_len = 0 msg_len = 1 nm = -1 if frame is None: frame = self.message_frame if frame.message_len: msg_len = 0 nm = frame.nmessage_len if datasize is None: datasize = self.datasize data = b'' while 1: s = recv(datasize) if not s: break if not msg_len: msg_len = int(s[:nm], 16) sum += len(s) data += s if sum >= msg_len: break if frame.message_len: # trim off header data = data[nm:] if frame.checksum: nc = frame.nchecksum checksum = data[-nc:] data = data[:-nc] comp = computeCRC(data) if comp != checksum: print('checksum fail computed={}, expected={}'.format( comp, checksum)) return return data.decode('utf-8')
def _validate_checksum(self, word): if word is not None: checksum = word[-4:] data = word[:-4] # self.debug('{} {}'.format(data, checksum)) expected = computeCRC(data) if expected != checksum: self.warning('The checksum is not correct for this message. Expected: {}, Actual: {}'.format(expected, checksum)) else: return True
def _recvall(self, recv, frame=None): """ recv: callable that accepts 1 argument (datasize). should return a str """ ss = [] sum = 0 # disable message len checking # msg_len = 1 # if self.use_message_len_checking: # msg_len = 0 msg_len = 1 nm = -1 if frame is None: frame = self.message_frame if frame.message_len: msg_len = 0 nm = frame.nmessage_len while 1: s = recv(self.datasize) # self._sock.recv(2048) if not s: break if not msg_len: msg_len = int(s[:nm], 16) sum += len(s) ss.append(s) if sum >= msg_len: break data = ''.join(ss) data = data.strip() if frame.message_len: # trim off header data = data[nm:] if frame.checksum: nc = frame.nchecksum checksum = data[-nc:] data = data[:-nc] comp = computeCRC(data) if comp != checksum: print 'checksum fail computed={}, expected={}'.format( comp, checksum) return return data
def _recvall(self, recv, frame=None): """ recv: callable that accepts 1 argument (datasize). should return a str """ ss = [] sum = 0 # disable message len checking # msg_len = 1 # if self.use_message_len_checking: # msg_len = 0 msg_len = 1 nm = -1 if frame is None: frame = self.message_frame if frame.message_len: msg_len = 0 nm = frame.nmessage_len while 1: s = recv(self.datasize) # self._sock.recv(2048) if not s: break if not msg_len: msg_len = int(s[:nm], 16) sum += len(s) ss.append(s) if sum >= msg_len: break data = ''.join(ss) data = data.strip() if frame.message_len: # trim off header data = data[nm:] if frame.checksum: nc = frame.nchecksum checksum = data[-nc:] data = data[:-nc] comp = computeCRC(data) if comp != checksum: print 'checksum fail computed={}, expected={}'.format(comp, checksum) return return data
def _execute_request(self, args, response_type, **kw): ''' ''' cmd = ''.join([self.slave_address] + args) # convert hex string into list of ints cmdargs = self._parse_hexstr(cmd, return_type='int') # calculate the CRC and append to message crc = computeCRC(cmdargs) cmd += crc kw['is_hex'] = True if self.scheduler is not None: resp = self.scheduler.schedule(self.ask, args=(cmd, ), kwargs=kw) else: resp = self.ask(cmd, **kw) return self._parse_response(cmd, resp, response_type)
def _execute_request(self, args, response_type, **kw): ''' ''' cmd = ''.join([self.slave_address] + args) # convert hex string into list of ints cmdargs = self._parse_hexstr(cmd, return_type='int') # calculate the CRC and append to message crc = computeCRC(cmdargs) cmd += crc kw['is_hex'] = True if self.scheduler is not None: resp = self.scheduler.schedule(self.ask, args=(cmd,), kwargs=kw ) else: resp = self.ask(cmd, **kw) return self._parse_response(cmd, resp, response_type)
def wrapper(*args, **kw): d = func(*args, **kw) return '{}{}'.format(d, computeCRC(d))
def _parse_response(self, cmd, resp, response_type): ''' ''' if resp is not None and resp is not 'simulation': args = self._parse_hexstr(resp) if args: if args[0] != cmd[:2]: self.warning('{} != {} {} >> {}'.format( cmd[:2], args[0], cmd, resp)) return else: return # check the crc cargs = self._parse_hexstr(resp, return_type='int') crc = ''.join(args[-2:]) calc_crc = computeCRC(cargs[:-2]) if not crc.upper() == calc_crc.upper(): msg = 'Returned CRC ({}) does not match calculated ({})'.format( crc, calc_crc) self.warning(msg) raise CRCError('{} {}'.format(cmd, msg)) else: if response_type == 'register_write': return True ndata = int(args[2], 16) dataargs = args[3:3 + ndata] if len(dataargs) < ndata: ndata = 4 dataargs = args[3:3 + ndata] if ndata > 2: data = [] for i in range(ndata / 4): s = 4 * i low_word = ''.join(dataargs[s:s + 2]) high_word = ''.join(dataargs[s + 2:s + 4]) if self.device_word_order == 'low_high': ''' dataargs in low word - high word order 1234 5678 want high word -low word order 5678 1234 ''' di = ''.join([high_word, low_word]) else: di = ''.join([low_word, high_word]) data.append(di) data = ''.join(data) if response_type == 'float': fmt_str = '!' + 'f' * (ndata / 4) resp = struct.unpack(fmt_str, data.decode('hex')) # return a single value if ndata == 4: return resp[0] else: # return a list of values return resp else: data = ''.join(args[3:3 + ndata]) return int(data, 16)
def _parse_response(self, cmd, resp, response_type): ''' ''' if resp is not None and resp is not 'simulation': args = self._parse_hexstr(resp) if args: if args[0] != cmd[:2]: self.warning('{} != {} {} >> {}'.format(cmd[:2], args[0], cmd, resp)) return else: return # check the crc cargs = self._parse_hexstr(resp, return_type='int') crc = ''.join(args[-2:]) calc_crc = computeCRC(cargs[:-2]) if not crc.upper() == calc_crc.upper(): msg='Returned CRC ({}) does not match calculated ({})'.format(crc, calc_crc) self.warning(msg) raise CRCError('{} {}'.format(cmd, msg)) else: if response_type == 'register_write': return True ndata = int(args[2], 16) dataargs = args[3:3 + ndata] if len(dataargs) < ndata: ndata = 4 dataargs = args[3:3 + ndata] if ndata > 2: data = [] for i in range(ndata / 4): s = 4 * i low_word = ''.join(dataargs[s:s + 2]) high_word = ''.join(dataargs[s + 2:s + 4]) if self.device_word_order == 'low_high': ''' dataargs in low word - high word order 1234 5678 want high word -low word order 5678 1234 ''' di = ''.join([high_word, low_word]) else: di = ''.join([low_word, high_word]) data.append(di) data = ''.join(data) if response_type == 'float': fmt_str = '!' + 'f' * (ndata / 4) resp = struct.unpack(fmt_str, data.decode('hex')) # return a single value if ndata == 4: return resp[0] else: # return a list of values return resp else: data = ''.join(args[3:3 + ndata]) return int(data, 16)
def _parse_response(self, cmd, resp, response_type): """ """ # self.debug('asdf {} {}'.format(cmd, resp)) if resp is not None and resp is not 'simulation': args = self._parse_hexstr(resp) if args: # self.debug('args={}'.format(args)) if bytes(args[:2]) != bytes.fromhex(cmd[:4]): self.warning('{} != {} {} >> {}'.format(cmd[:4], args[:2], cmd, resp)) return else: return # check the crc # cargs = self._parse_hexstr(resp, return_type='int') # self.debug('cargs {}'.format(cargs)) # self.debug('args {}'.format(args)) # crc = ''.join([str(bytes([a]) for a in args[-2:]]) # self.debug('asdf {}'.format(args[-2:])) # crc = bytes(args[-2:]) # calc_crc = computeCRC(args[:-2]) crc = args[-2:] calc_crc = computeCRC(args[:-2]) calc_crc = [int(calc_crc[:2], 16), int(calc_crc[2:], 16)] if not crc == calc_crc: msg = 'Returned CRC ({}) does not match calculated ({})'.format(crc, calc_crc) self.warning(msg) raise CRCError('{} {}'.format(cmd, msg)) else: if response_type == 'register_write': return True # ndata = int(args[2], 16) ndata = int.from_bytes([args[2]], 'big') dataargs = args[3:3 + ndata] if len(dataargs) < ndata: ndata = 4 dataargs = args[3:3 + ndata] if ndata > 2: data = [] for i in range(ndata // 4): s = 4 * i low_word = ''.join(['{:02X}'.format(d) for d in dataargs[s:s+2]]) high_word = ''.join(['{:02X}'.format(d) for d in dataargs[s+2:s+4]]) # low_word = ''.join(dataargs[s:s + 2]) # high_word = ''.join(dataargs[s + 2:s + 4]) if self.device_word_order == 'low_high': ''' dataargs in low word - high word order 1234 5678 want high word -low word order 5678 1234 ''' di = ''.join([high_word, low_word]) else: di = ''.join([low_word, high_word]) data.append(di) data = ''.join(data) if response_type == 'float': fmt_str = '!' + 'f' * (ndata//4) resp = struct.unpack(fmt_str, bytes.fromhex(data)) # return a single value if ndata == 4: return resp[0] else: # return a list of values return resp else: data = args[3:3 + ndata] return int.from_bytes(data, 'big')