platypush/plugins/serial/__init__.py
def read(self, device=None, baud_rate=None, size=None, end=None):
        """
        Reads raw data from the serial device

        :param device: Device to read (default: default configured device)
        :type device: str

        :param baud_rate: Baud rate (default: default configured baud_rate)
        :type baud_rate: int

        :param size: Number of bytes to read
        :type size: int

        :param end: End of message byte or character
        :type end: int, bytes or str
        """

        if not device:
            if not self.device:
                raise RuntimeError('No device specified nor default device configured')
            device = self.device

        if baud_rate is None:
            if self.baud_rate is None:
                raise RuntimeError('No baud_rate specified nor default configured')
            baud_rate = self.baud_rate

        if (size is None and end is None) or (size is not None and end is not None):
            raise RuntimeError('Either size or end must be specified')

        if end and isinstance(end, str) and len(end) > 1:
            raise RuntimeError('The serial end must be a single character, not a string')

        data = bytes()

        try:
            serial_available = self.serial_lock.acquire(timeout=2)
            if serial_available:
                try:
                    ser = self._get_serial(device=device, baud_rate=baud_rate)
                except Exception as e:
                    self.logger.debug(e)
                    time.sleep(1)
                    ser = self._get_serial(device=device, baud_rate=baud_rate, reset=True)

                if size is not None:
                    for _ in range(0, size):
                        data += ser.read()
                elif end is not None:
                    if isinstance(end, str):
                        end = end.encode()

                    ch = None
                    while ch != end:
                        ch = ser.read()

                        if ch != end:
                            data += ch
            else:
                self.logger.warning('Serial read timeout')
        finally:
            try:
                self.serial_lock.release()
            except Exception as e:
                self.logger.debug(e)

        try:
            data = data.decode()
        except (ValueError, TypeError):
            data = base64.encodebytes(data)

        return data

        Reads raw data from the serial device

        :param device: Device to read (default: default configured device)
        :type device: str

        :param baud_rate: Baud rate (default: default configured baud_rate)
        :type baud_rate: int

        :param size: Number of bytes to read
        :type size: int

        :param end: End of message byte or character
        :type end: int, bytes or str
        
Reads raw data from the serial device
Reads raw data from the serial device
def read(self, device=None, baud_rate=None, size=None, end=None):
        if not device:
            if not self.device:
                raise RuntimeError('No device specified nor default device configured')
            device = self.device
        if baud_rate is None:
            if self.baud_rate is None:
                raise RuntimeError('No baud_rate specified nor default configured')
            baud_rate = self.baud_rate
        if (size is None and end is None) or (size is not None and end is not None):
            raise RuntimeError('Either size or end must be specified')
        if end and isinstance(end, str) and len(end) > 1:
            raise RuntimeError('The serial end must be a single character, not a string')
        data = bytes()
        try:
            serial_available = self.serial_lock.acquire(timeout=2)
            if serial_available:
                try:
                    ser = self._get_serial(device=device, baud_rate=baud_rate)
                except Exception as e:
                    self.logger.debug(e)
                    time.sleep(1)
                    ser = self._get_serial(device=device, baud_rate=baud_rate, reset=True)
                if size is not None:
                    for _ in range(0, size):
                        data += ser.read()
                elif end is not None:
                    if isinstance(end, str):
                        end = end.encode()
                    ch = None
                    while ch != end:
                        ch = ser.read()
                        if ch != end:
                            data += ch
            else:
                self.logger.warning('Serial read timeout')
        finally:
            try:
                self.serial_lock.release()
            except Exception as e:
                self.logger.debug(e)
        try:
            data = data.decode()
        except (ValueError, TypeError):
            data = base64.encodebytes(data)
        return data
def read ( self , device = None , baud_rate = None , size = None , end = None ) : if not device : if not self . device : raise RuntimeError ( 'No device specified nor default device configured' ) device = self . device if baud_rate is None : if self . baud_rate is None : raise RuntimeError ( 'No baud_rate specified nor default configured' ) baud_rate = self . baud_rate if ( size is None and end is None ) or ( size is not None and end is not None ) : raise RuntimeError ( 'Either size or end must be specified' ) if end and isinstance ( end , str ) and len ( end ) > 1 : raise RuntimeError ( 'The serial end must be a single character, not a string' ) data = bytes ( ) try : serial_available = self . serial_lock . acquire ( timeout = 2 ) if serial_available : try : ser = self . _get_serial ( device = device , baud_rate = baud_rate ) except Exception as e : self . logger . debug ( e ) time . sleep ( 1 ) ser = self . _get_serial ( device = device , baud_rate = baud_rate , reset = True ) if size is not None : for _ in range ( 0 , size ) : data += ser . read ( ) elif end is not None : if isinstance ( end , str ) : end = end . encode ( ) ch = None while ch != end : ch = ser . read ( ) if ch != end : data += ch else : self . logger . warning ( 'Serial read timeout' ) finally : try : self . serial_lock . release ( ) except Exception as e : self . logger . debug ( e ) try : data = data . decode ( ) except ( ValueError , TypeError ) : data = base64 . encodebytes ( data ) return data
Reads raw data from the serial device
Reads raw data from the serial device
"""
        Reads raw data from the serial device

        :param device: Device to read (default: default configured device)
        :type device: str

        :param baud_rate: Baud rate (default: default configured baud_rate)
        :type baud_rate: int

        :param size: Number of bytes to read
        :type size: int

        :param end: End of message byte or character
        :type end: int, bytes or str
        """
self
device
Device to read (default: default configured device)
Device to read ( default : default configured device )
baud_rate
Baud rate (default: default configured baud_rate)
Baud rate ( default : default configured baud_rate )
size
Number of bytes to read
Number of bytes to read
end
End of message byte or character
End of message byte or character