コード例 #1
0
class SerialMessagesInterface(threading.Thread):
    def __init__(self,
                 callback,
                 verbose=False,
                 device='/dev/ttyUSB0',
                 baudrate=115200,
                 msg_class='telemetry'):
        threading.Thread.__init__(self)
        self.callback = callback
        self.verbose = verbose
        self.msg_class = msg_class
        self.running = True
        try:
            self.ser = serial.Serial(device, baudrate, timeout=1.0)
        except serial.SerialException:
            print("Error: unable to open serial port '%s'" % device)
            exit(0)
        self.trans = PprzTransport(msg_class)

    def stop(self):
        print("End thread and close serial link")
        self.running = False
        self.ser.close()

    def shutdown(self):
        self.stop()

    def __del__(self):
        try:
            self.ser.close()
        except:
            pass

    def send(self, msg, sender_id):
        """ Send a message over a serial link"""
        if isinstance(msg, PprzMessage):
            data = self.trans.pack_pprz_msg(sender_id, msg)
            self.ser.write(data)
            self.ser.flush()

    def run(self):
        """Thread running function"""
        try:
            while self.running:
                # Parse incoming data
                c = self.ser.read(1)
                if len(c) == 1:
                    if self.trans.parse_byte(c):
                        (sender_id, msg) = self.trans.unpack()
                        if self.verbose:
                            print("New incoming message '%s' from %i" %
                                  (msg.name, sender_id))
                        # Callback function on new message
                        self.callback(sender_id, msg)

        except StopIteration:
            pass
コード例 #2
0
class SerialMessagesInterface(threading.Thread):
    def __init__(self, callback, verbose=False, device='/dev/ttyUSB0', baudrate=115200,
                 msg_class='telemetry'):
        threading.Thread.__init__(self)
        self.callback = callback
        self.verbose = verbose
        self.msg_class = msg_class
        self.running = True
        try:
            self.ser = serial.Serial(device, baudrate, timeout=1.0)
        except serial.SerialException:
            print("Error: unable to open serial port '%s'" % device)
            exit(0)
        self.trans = PprzTransport(msg_class)

    def stop(self):
        print("End thread and close serial link")
        self.running = False
        self.ser.close()

    def shutdown(self):
        self.stop()

    def __del__(self):
        try:
            self.ser.close()
        except:
            pass

    def send(self, msg, sender_id):
        """ Send a message over a serial link"""
        if isinstance(msg, PprzMessage):
            data = self.trans.pack_pprz_msg(sender_id, msg)
            self.ser.write(data)
            self.ser.flush()

    def run(self):
        """Thread running function"""
        try:
            while self.running:
                # Parse incoming data
                c = self.ser.read(1)
                if len(c) == 1:
                    if self.trans.parse_byte(c):
                        (sender_id, msg) = self.trans.unpack()
                        if self.verbose:
                            print("New incoming message '%s' from %i" % (msg.name, sender_id))
                        # Callback function on new message 
                        self.callback(sender_id, msg)

        except StopIteration:
            pass
コード例 #3
0
 def __init__(self,
              callback,
              verbose=False,
              device='/dev/ttyUSB0',
              baudrate=115200,
              msg_class='telemetry'):
     threading.Thread.__init__(self)
     self.callback = callback
     self.verbose = verbose
     self.msg_class = msg_class
     self.running = True
     try:
         self.ser = serial.Serial(device, baudrate, timeout=1.0)
     except serial.SerialException:
         print("Error: unable to open serial port '%s'" % device)
         exit(0)
     self.trans = PprzTransport(msg_class)
コード例 #4
0
 def __init__(self, callback, verbose=False, device='/dev/ttyUSB0', baudrate=115200,
              msg_class='telemetry'):
     threading.Thread.__init__(self)
     self.callback = callback
     self.verbose = verbose
     self.msg_class = msg_class
     self.running = True
     try:
         self.ser = serial.Serial(device, baudrate, timeout=1.0)
     except serial.SerialException:
         print("Error: unable to open serial port '%s'" % device)
         exit(0)
     self.trans = PprzTransport(msg_class)