Reverse Bit Order Python? ESC/POS DLE EOT Printer status escpos

本秂侑毒 提交于 2020-01-25 07:41:05

问题


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


来源:https://stackoverflow.com/questions/49186670/reverse-bit-order-python-esc-pos-dle-eot-printer-status-escpos

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!