def _port_pollin(self): chars = self._port.drain() if self._buffer is None: self._buffer = array.array('c') self._packet_length = 4 #shortest legit packet self._buffer.extend(chars) if len( self._buffer ) >= self._packet_length: #might be a good packet, otherwise wait for more packet_type = ord(self._buffer[1]) if not packet_type_vs_length.has_key( packet_type): #don't crock this one self._buffer = None #reset the packet processing return packet_length = packet_type_vs_length[packet_type] if packet_length < 0: #must be variable length packet packet_length = abs(packet_length) if len(self._buffer ) < packet_length: #not enough of the packet in yet return #try again when more comes in packet_length = packet_length + ord( self._buffer[packet_length - 3]) #calc variable length packet size if len(self._buffer) >= packet_length: #enought to test the crc chars = self._buffer self._buffer = None #reset packet parsing regardless of what happens below bbuffer = array.array('B', chars[:packet_length].tostring()) #is checksum good? if crc(bbuffer) == 0: #things is gud if debug: print 'command' dump(chars) response = self.owner.command(chars) if response is None: # message sent to a Modbus that does not exist return response.append_crc() if debug: print 'response:', response dump(response.buffer) self._port.write(response.buffer) # If this is a Megatron RS485 port then consume echoed transmit characters # Wait for up to 2 seconds to see our echoed transmit data. if self.megatron_485: try: self.port.read(array.array('c'), len(response.buffer), 2.0) except: msglog.exception() else: if debug: print 'crc failed - might be response' return
def _port_pollin(self): chars = self._port.drain() if self._buffer is None: self._buffer = array.array("c") self._packet_length = 4 # shortest legit packet self._buffer.extend(chars) if len(self._buffer) >= self._packet_length: # might be a good packet, otherwise wait for more packet_type = ord(self._buffer[1]) if not packet_type_vs_length.has_key(packet_type): # don't crock this one self._buffer = None # reset the packet processing return packet_length = packet_type_vs_length[packet_type] if packet_length < 0: # must be variable length packet packet_length = abs(packet_length) if len(self._buffer) < packet_length: # not enough of the packet in yet return # try again when more comes in packet_length = packet_length + ord(self._buffer[packet_length - 3]) # calc variable length packet size if len(self._buffer) >= packet_length: # enought to test the crc chars = self._buffer self._buffer = None # reset packet parsing regardless of what happens below bbuffer = array.array("B", chars[:packet_length].tostring()) # is checksum good? if crc(bbuffer) == 0: # things is gud if debug: print "command" dump(chars) response = self.owner.command(chars) if response is None: # message sent to a Modbus that does not exist return response.append_crc() if debug: print "response:", response dump(response.buffer) self._port.write(response.buffer) # If this is a Megatron RS485 port then consume echoed transmit characters # Wait for up to 2 seconds to see our echoed transmit data. if self.megatron_485: try: self.port.read(array.array("c"), len(response.buffer), 2.0) except: msglog.exception() else: if debug: print "crc failed - might be response" return
def crc(self, byte_array): return base.crc(byte_array)