Чтобы быть более точным, я хотел бы портируйте в python что-то вроде следующего:
sendln 'ATI9'
подождите "ОК" "+CME ERROR"
(Обратите внимание, что ответ от модема содержит несколько строки, оканчивающиеся на «ОК»)
В Python я реализовал следующие два варианта, но результат меня не очень устраивает:
используя time.sleep или ser.timeout и read_until из pyserial:
Отправить AT-команду
Код: Выделить всё
def send_at_command(ser, command, timeout):
print(f'Sending: {command}')
ser.write((command + '\r\n').encode('utf-8'))
time.sleep(timeout) # Wait for the response
response = ser.read_all().decode('utf-8')
print(f'Response: {response}')
return response
Код: Выделить всё
def send_at_command2(ser, command, timeout):
try:
print(f'Sending: {command}')
ser.timeout = timeout # Wait for the response
ser.write((command + '\r\n').encode('utf-8'))
ser.reset_output_buffer()
response = ser.read_until('\r').decode('utf-8')
print(f'Response: {response}')
#ser.reset_input_buffer()
except Exception as e:
print(f'No response to {command} : {str(e)}')
print(f'Response {response} : {ser.read_all()}')
ser.close()
otii.shutdown()
connection.close_connection()
server.terminate()
sys.exit()
return response
Код: Выделить всё
def validate_response(response, expected_keywords):
if any(keyword in response for keyword in expected_keywords):
#print('Test passed.')
return True
else:
#print('Test failed.')
return False
Код: Выделить всё
def Wait_At_Command_Response(ser, command, timeout, expected_keywords):
response = send_at_command2(ser,command, timeout)
#response = send_at_command(ser,command, timeout)
return validate_response(response, expected_keywords)
Код: Выделить всё
Wait_At_Command_Response(ser,'ATI9',2,['+CME ERROR', 'OK'])Спасибо
Подробности см. в описании
Подробнее здесь: https://stackoverflow.com/questions/790 ... -in-python