Reverse Bit Order Python? ESC/POS DLE EOT Printer

2019-09-02 00:04发布

问题:

I am having issues decoding DLE EOT 1 im thinking its the bit order and the lack of leading zeros

import serial
x = 1
while x:
   time.sleep(3)
   ser.write("\x10\x04\x01".encode())  
   bytesToRead = ser.inWaiting()
   data = ser.read(bytesToRead)
   while data:
      print(data)
      print(bin(int.from_bytes(data, byteorder="big")))
      print(bin(data[0])[2:])
      data = ""

so this is what is returned when in ready and online status:

b'\x16'
0b10110
10110

this is what returns when the Door is open 'assume OFFLINE status':

b'\x1e'
0b11110
11110

how does any of that translate? dont i need 8bits back?

Excerpt from EPSON ESC Manual:

Each status consists of 1 byte, and the value is 0xx1xx10b. The real time status can be differentiated by the bits 0, 1, 4, and 7 from other transmission data, except for data in block data (Header – NUL).

Bit Binary  Status                                 |Hex|Decimal
====+==============================================+===+======
0   | 0 |   Fixed                                  |00 |0    |
----+---+------------------------------------------+---+-----+
1   | 1 |   Fixed                                  |02 |2    |
----+---+------------------------------------------+---+-----+
2   | 0 | Drawer kick-out connector pin 3 is LOW   |00 |0    |
    | 1 | Drawer kick-out connector pin 3 is HIGH  |04 |4    |
----+---+------------------------------------------+---+-----|
3   | 0 | Online                                   |00 |0    |
    | 1 | Offline                                  |08 |8    |
----+---+------------------------------------------+---+-----|
4   | 1 | Fixed                                    |10 |16   |
----+---+------------------------------------------+---+-----|
5   | 0 | Not waiting for online recovery          |00 |0    |
    | 1 | Waiting for online recovery              |20 |32   |
----+---+------------------------------------------+---+-----|
6   | 0 | Paper feed button is not being pressed   |00 |0    |
    | 1 | Paper feed button is being pressed       |04 |64   |
----+---+------------------------------------------+---+-----|
7   | 0 | Fixed                                    |00 |0    |
--------------------------------------------------------------

回答1:

 print(bin(data[0])[2:].zfill(8)[::-1])

this will add leading zeros and reverse the bits. The result: status online:

                            /---------Bit 3
00010110  -> reversed =  01101000
0xx1xx10b -> reversed = b01xx1xx0
                            ^---------Bit 3