@@ -34,17 +34,22 @@ class QRCodeM14:
34
34
POS_LIGHT_ON_DECODE = const (2 ) # 解码时亮
35
35
POS_LIGHT_FLASH_ON_DECODE = const (1 ) # 解码时闪烁
36
36
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 :
38
38
self .serial = machine .UART (id , baudrate = 115200 , tx = rx , rx = tx ) # 交叉
39
39
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 )
41
43
self .led = led
42
44
43
45
def set_trig (self , value : int ) -> None :
44
46
"""Set trigger pin. 设置触发引脚。
45
47
:param int value: ``0`` - 低电平,``1`` - 高电平。
46
48
"""
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." )
48
53
49
54
def start_decode (self ) -> None :
50
55
"""Start decode. 开始解码。"""
@@ -260,3 +265,28 @@ def set_protocol_format(self, mode: int) -> None:
260
265
cmd = bytes ([0x21 , 0x51 , 0x43 , mode ])
261
266
cmd_ack = bytes ([0x22 , 0x51 , 0x43 , 0x00 ])
262
267
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