我需要映射SIM卡的串口号和插槽。 我的SIM卡有256个插槽(8个端口,32个插槽),盒子的每个端口连接8个串口。 如何获取 8 个 COM 端口映射的银行端口号?
import serial.tools.list_ports
import logging
import time
def execute_command(modem, command, wait_time=2):
try:
modem.write(f"{command}\r".encode())
time.sleep(wait_time)
return modem.readlines()
except serial.SerialException as e:
print(f"Error executing command {command} in {modem.port}: {str(e)}")
return []
try:
modem = serial.Serial("COM" + str(real_port), 115200, timeout=1)
for i in range(132, 164):
sub_modem.append(serial.Serial("COM" + str(i), 115200, timeout=1))
print(f"########## Real Port: {real_port} ##########")
for index, modem_cop in enumerate(sub_modem):
for port in range(1, 9):
formatted_string = "AT+SWIT{:02d}-{:04d}".format(port, 1)
response = execute_command(modem, formatted_string)
try:
operator = ''.join([line.decode('utf-8', errors='ignore') for line in response])
except UnicodeDecodeError as e:
print(f"Decoding error: {e}")
continue
if str("SWITCH OK") in operator:
try:
res_cop = execute_command(modem_cop, "AT+COPS=?")
try:
oper_cop = ''.join([line.decode('utf-8', errors='ignore').strip() for line in res_cop])
except UnicodeDecodeError as e:
print(f"Decoding error: {e}")
continue
except Exception as e:
print(e)
for index in enumerate(sub_modem):
sub_modem[index].close()
modem.close()
print(f"########## Real Port: {real_port} ##########")
logging.info(f"########## Real Port: {real_port} ##########")
except Exception as e:
print(f"Error: {e}")
您可以使用 pyserial 库编写 Python 脚本来查询每个 COM 端口并发送识别 SIM 卡上相应端口和插槽的特定命令。