獲取連線電腦的裝置的所有埠資訊

R0mrnel發表於2024-09-29
 1 def search_port(port_name):
 2     # 獲取連線電腦的裝置的所有埠資訊
 3     port_list = list(comports())
 4     print("port_list",port_list)
 5 
 6     if len(port_list) > 0:
 7         # 遍歷所有埠資訊,根據埠名稱port_name查詢需要的埠號
 8         for port in port_list:
 9             print(port.description,port.device)
10             if port_name in port.description:
11                 port_com = port.device
12                 return port_com
13         else:
14             return None
15     else:
16         return None
17 
18 
19 self.m_ser_hdl = serial.Serial(self.m_at_dev, 115200, timeout=30)
20 def at_cmd_exec(self, cmd, *exp):
21     if not cmd.endswith("\r") and not cmd.endswith("\r\n"):
22         cmd += "\r"
23 
24     try:
25         self.m_ser_hdl.write(cmd.encode())
26     except Exception as e:
27         print(e)
28         return None
29 
30     response = self.at_cmd_response()
31     if response:
32         return response
33     else:
34         return None

相關文章