@@ -34,17 +34,22 @@ class QRCodeM14:
3434 POS_LIGHT_ON_DECODE = const (2 ) # 解码时亮
3535 POS_LIGHT_FLASH_ON_DECODE = const (1 ) # 解码时闪烁
3636
37- def __init__ (self , id : int = 1 , tx : int = 5 , rx : int = 6 , trig : int = 7 , led = None ) -> None :
37+ def __init__ (self , id : int = 1 , tx : int = 5 , rx : int = 6 , trig : int = None , led = None ) -> None :
3838 self .serial = machine .UART (id , baudrate = 115200 , tx = rx , rx = tx ) # 交叉
3939 self .serial_handler = serial_cmd_helper .SerialCmdHelper (self .serial , False )
40- self .trig = machine .Pin (trig , machine .Pin .OUT )
40+ self .trig = None
41+ if trig is not None :
42+ self .trig = machine .Pin (trig , machine .Pin .OUT )
4143 self .led = led
4244
4345 def set_trig (self , value : int ) -> None :
4446 """Set trigger pin. 设置触发引脚。
4547 :param int value: ``0`` - 低电平,``1`` - 高电平。
4648 """
47- self .trig .value (value )
49+ if self .trig is not None :
50+ self .trig .value (value )
51+ else :
52+ raise RuntimeError ("Trigger pin is not initialized." )
4853
4954 def start_decode (self ) -> None :
5055 """Start decode. 开始解码。"""
@@ -260,3 +265,28 @@ def set_protocol_format(self, mode: int) -> None:
260265 cmd = bytes ([0x21 , 0x51 , 0x43 , mode ])
261266 cmd_ack = bytes ([0x22 , 0x51 , 0x43 , 0x00 ])
262267 self .serial_handler .cmd (cmd , cmd_ack , timeout_ms = 200 )
268+
269+ def get_version (self , timeout_ms = 500 ):
270+ self .serial .read ()
271+ self .serial .write (b'\x43 \x02 \xc1 ' ) # 查询版本
272+ start = time .ticks_ms ()
273+ rx = b''
274+ while time .ticks_diff (time .ticks_ms (), start ) < timeout_ms :
275+ if self .serial .any ():
276+ time .sleep_ms (5 )
277+ rx += self .serial .read ()
278+ data_len = (rx [3 ] << 8 ) + rx [4 ]
279+ if len (rx ) >= 4 + data_len :
280+ if rx [0 ] == 0x44 and rx [1 ] == 0x02 and rx [2 ] == 0xC1 :
281+ total_len = 4 + data_len
282+ if len (rx ) >= total_len :
283+ payload = rx [4 :4 + data_len + 1 ]
284+ try :
285+ version_str = payload .decode ('ascii' )
286+ return version_str [1 :]
287+ except :
288+ return None
289+ else :
290+ return None
291+ time .sleep_ms (1 )
292+ return None
0 commit comments