コード例 #1
0
ファイル: MP25.py プロジェクト: stoq/stoqdrivers
def _rbytes2str(data):
    if isinstance(data, tuple):
        data = list(data)
        return tuple(_rbytes2str(b) for b in data)
    elif isinstance(data, bytes):
        return bytes2str(data)
    else:
        return data
コード例 #2
0
def _rbytes2str(data):
    if isinstance(data, tuple):
        data = list(data)
        return tuple(_rbytes2str(b) for b in data)
    elif isinstance(data, bytes):
        return bytes2str(data)
    else:
        return data
コード例 #3
0
    def _send_command(self, command, **params):
        # Page 38-39
        parameters = []
        for param, value in sorted(params.items()):
            if isinstance(value, Decimal):
                value = ('%.03f' % value).replace('.', ',')
            elif isinstance(value, bytes):
                value = '"%s"' % bytes2str(value)
            elif isinstance(value, str):
                value = '"%s"' % value
            elif isinstance(value, bool):
                if value is False:
                    value = 'f'
                elif value is True:
                    value = 't'
            elif isinstance(value, datetime.date):
                value = value.strftime('#%d/%m/%y#')

            parameters.append('%s=%s' % (param, value))

        reply = self.writeline("%d;%s;%s;" % (self._command_id,
                                              command,
                                              ' '.join(parameters)))
        if reply[0] != '{':
            # This happened once after the first command issued after
            # the power returned, it should probably be handled gracefully
            raise AssertionError(repr(reply))

        # Page 39
        sections = reply[1:].split(';')
        if len(sections) != 4:
            raise AssertionError

        retdict = self._parse_return_value(sections[2])
        errorcode = int(sections[1])
        if errorcode != 0:
            errordesc = retdict['Circunstancia']
            try:
                exception = self.errors_dict[errorcode]
            except KeyError:
                raise DriverError(errordesc, errorcode)
            raise exception(errordesc, errorcode)

        return retdict
コード例 #4
0
    def _create_packet(self, command):
        """
        Create a 'pre-package' (command + params, basically) and involves
        it around STX, NB and CS::
           1     2           n           2
        +-----+------+-----------------+----+
        | STX |  NB  | 0x1C + command  | CS |
        +-----+------+-----------------+----+

        Where:

        STX: 'Transmission Start' indicator byte
        NB: 2 bytes, big endian length of command + CS (2 bytes)
        CS: 2 bytes, big endian checksum for command
        """

        command = chr(self.CMD_PROTO) + command
        packet = struct.pack('<bH%dsH' % len(command), STX,
                             len(command) + 2, str2bytes(command),
                             sum([ord(i) for i in command]))
        return bytes2str(packet)
コード例 #5
0
ファイル: MP25.py プロジェクト: stoq/stoqdrivers
    def _create_packet(self, command):
        """
        Create a 'pre-package' (command + params, basically) and involves
        it around STX, NB and CS::
           1     2           n           2
        +-----+------+-----------------+----+
        | STX |  NB  | 0x1C + command  | CS |
        +-----+------+-----------------+----+

        Where:

        STX: 'Transmission Start' indicator byte
        NB: 2 bytes, big endian length of command + CS (2 bytes)
        CS: 2 bytes, big endian checksum for command
        """

        command = chr(self.CMD_PROTO) + command
        packet = struct.pack(
            '<bH%dsH' % len(command),
            STX,
            len(command) + 2,
            str2bytes(command),
            sum([ord(i) for i in command]))
        return bytes2str(packet)
コード例 #6
0
 def read(self, n_bytes):
     # stoqdrivers is expecting str but pyserial will reply with bytes
     data = self._port.read(n_bytes)
     return bytes2str(data)