Python Forum

Full Version: Execute "AT" command in Python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,
To check ICCID value of a modem 4G, I execute AT command in a terminal (e.g. picocom): AT+QCCID.
Is it possible to do the same in Python ?
Thanks.
I can't give you a complete answer as my needs are few, and I usually do any 'AT' commands directly using Putty.

I will instead direct you to paramiko (which is an implementation of the SSHv2 protocol) URL's:
pipy: https://pypi.org/project/paramiko/
main site: https://www.paramiko.org/
GitHub: https://github.com/paramiko/paramiko

Check the 'demos' https://github.com/paramiko/paramiko/tree/main/demos
to see if you can find an example of what you're looking for.

There may be better options.
Here is a solution:

import serial
import io
import time
import os

signal = serial.Serial(
    port='/dev/ttyUSB2',
    baudrate=115200,
    bytesize=8,
    parity='N',
    timeout=1,
    stopbits=1,
    rtscts=False,
    dsrdtr=False
)

signal_text = io.TextIOWrapper(signal, newline='\r\n')

signal.write("at+qccid\r\n".encode())

aaa = "a"      
while not aaa[0]=='+':
    aaa = signal_text.readline().rstrip()
print("ICCID: ", aaa[8:])
Thanks for sharing!