Automated Modem Diagnostics (Python)

The below code allows you to automatically run your Modem Diagnostics (as explained here) through a Python script, which will show you the output in your screen as well as save it in a text file.

 

First step is to plug in your device and run:

 

dmesg

 

This will show you "where" is your device connected in your machine (i.e. /dev/ttyACM1). Once you have the list (choose one from the list if applicable), make a note of it and update it into line 42 of the below code (NOTE: don't copy/paste the code since Python indentation might get lost in the process - You should download the attached script which already has the proper indentation):

 

# Automated Modem Diagnostics
import serial
import time
from curses import ascii

# Modem Diagnostics
def modemDiagnostics(srdv, atcmd, flnm):
# Initialize serial variable
device = serial.Serial(srdv, baudrate = 9600, timeout = 2.0)

# Initialize file variables
f = open(flnm, 'w')

# Run modem diagnostics
for cmd in atcmd:
print('Sending: ' + cmd)
# Send command
device.write(str.encode(cmd + '\r'))
time.sleep(2.0)
 
for i in range(10):
# Read command
c = device.readline()
time.sleep(2.0)
 
# Print command
c = c.decode('UTF-8')
# print(c)
# Write command into the file
f.writelines(c)
 
f.writelines('-----\r\n')

device.close()
f.close()

#############################################################################

# Do your thing
def main():
# Variables
serialDevice = '/dev/ttyACM1'
atCommands = ['AT','ATI','AT+CGMI','AT+CGMM','AT+GMM','AT+CPIN?','AT+CCID','AT+CRSM=176,28539,0,0,12','AT+CFUN?','AT+CSQ','AT+CREG?','AT+CGREG?','AT+CEREG?','AT+CGDCONT?','AT+CGACT?','AT+COPS?','AT+COPS=?']
# atCommands = 'AT\r'
fileName = 'ModemDiagnostics.txt'

# Call Modem Diagnostics
modemDiagnostics(serialDevice, atCommands, fileName)

# Done
print('Modem Diagnostics completed!')

# Python stuff
if __name__ == '__main__':
main()

 

Once you do, just run the script (attached) and it will run the Modem Diagnostics for you. You can see the output on your screen, but you will also see a ModemDiagnostics.txt file in the same location, which you can then send to us with your ticket so we can help you troubleshoot.

Was this article helpful?
1 out of 1 found this helpful