def __str__(self): result = array.array('c') result.fromstring('function: %s\n' % (self.function)) data = self.buffer[1:] if data: result.fromstring(dump_tostring(data, 'data: ')) return result.tostring()
def __str__(self): result = array.array('c') result.fromstring('slave_address: 0x%02X\nfunction: 0x%02X\n' % (self.slave_address, self.function)) data = self.buffer[2:-2] if data: result.fromstring(dump_tostring(data, 'data: ')) result.fromstring('crc: 0x%04X\n' % self.crc) return result.tostring()
def __str__(self): result = array.array('c') result.fromstring('slave_address: 0x%02X\nfunction: 0x%02X\n' % (self.slave_address, self.function)) result.fromstring('byte_count: 0x%02X\nslave_id: 0x%02X\n' % (self.byte_count, self.slave_id)) result.fromstring('run_status: 0x%02X\n' % self.run_status) if self.data: result.fromstring(dump_tostring(self.data, 'data: ')) result.fromstring('crc: 0x%04X\n' % self.crc) return result.tostring()
def dump_tostring(self, msg, hdr=None, offset=0): return dump_tostring(msg, hdr, offset, self.dump_cpl)
def send_command(self, cmd): command = cmd checksum = self.checksum(command) if cmd: #only add checksum to packets with commands command += '%c' % checksum frame_contents = '' for byte in command: # byte stuffing if byte in ('\xf0', '\xf1', '\xf2', '\xf3'): frame_contents += '\xf3%c' % (ord(byte) & 0x03) else: frame_contents += byte #self.port.drain() #discard lingering packets self.port.write(array.array('c','\xf1' + frame_contents + '\xf2')) #time.sleep(0.2) while 1: #loop until response comes back or timeout msg = array.array('c') self.port.read_upto(msg, ['\xf1',], self.timeout) #find beginning of response or timeout msg = array.array('c') if debug: print 'about to read upto f2' self.port.read_including(msg, ['\xf2',], 1) frame_contents = msg.tostring() if debug: print 'frame_contents: ', self.hexdump(frame_contents) msg = '' index = 0 while frame_contents[index] != '\xf2': #un stuff data segment byte = frame_contents[index] if byte == '\xf3': index += 1 msg += '%c' % (ord(frame_contents[index]) | 0xf0) else: msg += byte index += 1 if index >= len(frame_contents): msglog.log('csafe:send_command',msglog.types.ERR,'Comm error: Frame did not contain end character "f2".') return (status_text[0],msg[3:-1]) # show Error #check checksum #if self.checksum(msg) != 0: #msglog.log('mpx:csafe',msglog.types.WARN,'Bad CSAFE checksum detected. New status = %u' % ord(msg[0]) & 0xF) #continue #bad check try again new_status = ord(msg[0]) & 0xF if (new_status == 1) and (self.cur_status == 9): msglog.log('mpx:csafe',msglog.types.INFO,'Detected OffLine To Ready: %s' % _debug.dump_tostring(msg)) self.cur_status = new_status #print 'FEU %s sent %s' % (self.hexdump(addr), self.hexdump(msg)) # seperate out the status status = ord(msg[0]) & 0xF try: status = status_text[status] except: pass #non-legal value for status if debug: print 'status: ', str(status) if len(msg) == 2: #print 'FEU state is now %2.2x' % ord(msg[0]) if cmd == '\x80': #only return if this was a status request return (status, msg[0],) else: if command[0] == '\xaa': return (status, msg[2:-1],) else: if command[0] == msg[1]: #only return if response matches request return (status, msg[3:-1],) #strip off status, command and checksum if debug: print '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @@@ csafe rejected a response, look again' print self.hexdump(frame_contents), self.hexdump(cmd)
def __str__(self): result = array.array('c') data = self.buffer if data: result.fromstring(dump_tostring(data, 'data: ')) return result.tostring()