@@ -71,6 +71,17 @@ class ASRUnit:
7171 0x43 : ["medium volume" , None ],
7272 0x44 : ["minimum volume" , None ],
7373 0x45 : ["check firmware version" , None ],
74+ 0x50 : ["PA2 high level" , None ],
75+ 0x51 : ["PA2 low level" , None ],
76+ 0x52 : ["PA3 high level" , None ],
77+ 0x53 : ["PA3 low level" , None ],
78+ 0x54 : ["PA4 high level" , None ],
79+ 0x55 : ["PA4 low level" , None ],
80+ 0x56 : ["PA5 high level" , None ],
81+ 0x57 : ["PA5 low level" , None ],
82+ 0x58 : ["PC4 high level" , None ],
83+ 0x59 : ["PC4 low level" , None ],
84+ 0x5A : ["inversion level" , None ],
7485 0xFE : ["Announce" , None ],
7586 0xFF : ["Hi,M Five" , None ],
7687 }
@@ -90,20 +101,48 @@ def _debug_print(self, *args, **kwargs):
90101 print (* args , ** kwargs )
91102
92103 def _handler (self , uart ) -> None :
104+ if uart .any () < 5 :
105+ return
106+
93107 data = uart .read ()
94- if data is not None and len (data ) >= 5 :
95- self ._debug_print (("Received data: " , data ))
96-
97- if data [0 ] == 0xAA and data [1 ] == 0x55 and data [- 2 ] == 0x55 and data [- 1 ] == 0xAA :
98- self .is_recieved = True
99- self .raw_message = " " .join (f"0x{ byte :02X} " for byte in data )
100- self ._debug_print (("Parsed message:" , self .raw_message .split ()))
101-
102- self .command_num = data [2 ]
103- self .check_tick_callback ()
104- else :
105- self ._debug_print ("Invalid frame received: header/footer mismatch" )
106- uart .read ()
108+ if data is None or len (data ) < 5 :
109+ return
110+
111+ self ._debug_print (("Received data: " , data ))
112+
113+ # 检查5字节格式: AA 55 CMD 55 AA
114+ if (
115+ len (data ) >= 5
116+ and data [0 ] == 0xAA
117+ and data [1 ] == 0x55
118+ and data [3 ] == 0x55
119+ and data [4 ] == 0xAA
120+ ):
121+ self .is_recieved = True
122+ self .command_num = data [2 ]
123+ self .msg = 0 # 5字节格式没有msg字段
124+ self .raw_message = " " .join (f"0x{ byte :02X} " for byte in data [:5 ])
125+ self ._debug_print (("Parsed 5-byte message:" , self .raw_message ))
126+ self .check_tick_callback ()
127+
128+ # 检查6字节格式: AA 55 CMD MSG 55 AA
129+ elif (
130+ len (data ) >= 6
131+ and data [0 ] == 0xAA
132+ and data [1 ] == 0x55
133+ and data [4 ] == 0x55
134+ and data [5 ] == 0xAA
135+ ):
136+ self .is_recieved = True
137+ self .command_num = data [2 ]
138+ self .msg = data [3 ] # 6字节格式包含msg字段
139+ self .raw_message = " " .join (f"0x{ byte :02X} " for byte in data [:6 ])
140+ self ._debug_print (("Parsed 6-byte message:" , self .raw_message ))
141+ self .check_tick_callback ()
142+
143+ else :
144+ self ._debug_print ("Invalid frame received: header/footer mismatch" )
145+ uart .read ()
107146
108147 def get_received_status (self ) -> bool :
109148 """Get message reception status.
0 commit comments