Example #1
0
    def connect(self, port_name):
        
        if self.connection_open():
            raise IOError('Connection still open.')

        if self.parser:
            # Ask old parser to stop before we create another one for the new connection.
            self.parser.stop_request.set()
            
        self.connection = SerialConnection(port=port_name, timeout=0.3, writeTimeout=0.5, baudrate=115200)
        
        connection_thread = threading.Thread(target=self.connection.run)
        connection_thread.setDaemon(True)
        connection_thread.start()
        
        self.parser = ParserThread(self.connection, self.message_start_byte, self.new_message)
        self.parser.setDaemon(True)
        self.parser.start()
Example #2
0
class GlobLink(QObject):
    
    new_message = pyqtSignal(int, int, bytearray)
    
    def __init__(self):
        
        super(GlobLink, self).__init__()
        
        # Port to receive and transmit bytes over.
        self.connection = None
    
        # Special byte that begins each new message.
        self.message_start_byte = 0xFE
        
        self.parser = None
    
        # Transfer fields 
        self.num_bytes_sent = 0
        self.num_messages_sent = 0
        #self.transfer_buffer = array.array('c', '\0' * 300)
        self.transfer_buffer = bytearray(300)
        self.next_packet_num = 0 # used to detect dropped packets
        
    def connect(self, port_name):
        
        if self.connection_open():
            raise IOError('Connection still open.')

        if self.parser:
            # Ask old parser to stop before we create another one for the new connection.
            self.parser.stop_request.set()
            
        self.connection = SerialConnection(port=port_name, timeout=0.3, writeTimeout=0.5, baudrate=115200)
        
        connection_thread = threading.Thread(target=self.connection.run)
        connection_thread.setDaemon(True)
        connection_thread.start()
        
        self.parser = ParserThread(self.connection, self.message_start_byte, self.new_message)
        self.parser.setDaemon(True)
        self.parser.start()
        
    def disconnect(self):
        
        # reset stats
        self.num_bytes_sent = 0
        self.num_messages_sent = 0
        
        if self.parser:
            self.parser.stop_request.set()
            self.parser = None
        
        if self.connection_open():
            self.connection.close()
            self.connection = None
            return True
        
        return False # connection already closed
    
    def connection_open(self):
        
        return self.connection and self.connection.connection_is_open()
    
    def send(self, glob):
        
        if not self.connection_open():
            return
        
        body_bytes = glob.pack()
        body_size = len(body_bytes)
        
        # Send a 1 at start of header to show that CRC and packet number should be valid.
        header_fmt = '<BBBHBB'
        header = (self.message_start_byte, 1, glob.id, glob.instance, self.next_packet_num, body_size)
        header_size = 7
        
        struct.pack_into(header_fmt, self.transfer_buffer, 0, *header)
        
        self.transfer_buffer[header_size : header_size + body_size] = body_bytes # array.array('c', body_bytes)
        
        crc = calculate_crc(self.transfer_buffer, header_size + body_size, 0xFFFF)

        struct.pack_into('<H', self.transfer_buffer, header_size + body_size, crc)
        footer_size = 2
        
        message_size = header_size + body_size + footer_size

        self.connection.write(self.transfer_buffer[:message_size])
        
        self.num_bytes_sent += message_size
        self.num_messages_sent += 1
        self.next_packet_num += 1
        if self.next_packet_num > 255:
            self.next_packet_num = 0

    @property
    def num_messages_received(self):
        if self.parser:
            return self.parser.num_messages_received
        return 0
    
    @property
    def num_bytes_received(self):
        if self.parser:
            return self.parser.num_bytes_received
        return 0
    
    @property
    def num_bad_crc_messages(self):
        if self.parser:
            return self.parser.num_bad_crc_messages
        return 0

    @property
    def num_dropped_messages(self):
        if self.parser:
            return self.parser.num_dropped_messages
        return 0