- Sending modem AT command and parsing result
- 2 Answers 2
- Выполнение AT/USSD команд к /dev/ttyUSB* модему из консоли Linux
- Теория
- Выполнение AT/USSD команд
- bash
- minicom
- ussd.py
- Кодировка GSM 7bit в USSD запросах
- Кодирование в gsm7bit
- Декодирование из gsm7bit
- How to send AT commands to device
- From OpenWrt
- socat
- echo
- From a computer
- Examples of AT commands
- How to force LTE connection
- Further references
Sending modem AT command and parsing result
I would like to send AT command to my modem by using shell script and parse the result in order to verify if the OK is returned.
at=`echo -ne "AT+CFUN1=1\r\n" > /dev/ttyUSB0 | cat /dev/ttyUSB0`
You cannot send AT command from the normal command line to a modem. You need to use something like socat or minicom to establish a serial connection with the modem. See: How to send AT commands to a modem in Linux?
I am using minicom to establish the serial connection to the board. Once modem started, I use microcom or socat to speak with it. However, It is possible to send AT comands thru this interface by using a simple echo, isn’t ?
Before talk to the modem serial port, you should use the stty or setserial to set the baud rate of the SerialPort.
2 Answers 2
It is absolutely possible to send AT commands to a modem and capture its output from the command line like you are trying to do, however not by just using plain bash shell scripting. Which is why I wrote the program atinout specifically to support scenarios like you ask about.
MODEM_DEVICE=/dev/ttyUSB0 MODEM_OUTPUT=`echo AT | atinout - $MODEM_DEVICE -` case $MODEM_OUTPUT in *OK*) echo "Hurray, modem is up and running :)" ;; *) echo "Oh no! Something is not working :(" ;; esac
If you intend to parse the output in any more sophisticated way you should save the output to a file instead by giving a filename instead of the last — and read that.
Выполнение AT/USSD команд к /dev/ttyUSB* модему из консоли Linux
Утилиты и скрипты для коммуникации с /dev/ttyUSB* устройством из shell консоли (bash скриптов).
Теория
Отправить в /dev/ttyUSB* терминал AT команду не составляет проблем. Но результат вывода мы не увидим!
echo ATI > /dev/ttyUSB2 # или echo ATI | sudo tee /dev/ttyUSB2
Мы можем читать поток stdout c /dev/ttyUSB2 (результат выполнения команд):
- Нужно получить только результат выполнения конкретной AT команды, а не весь поток вывода.
- Нужно использовать gsm7bit кодировку для кодирования параметров и получения ответа при выполнении USSD запросов:
echo AT+CUSD=1,AA182C3602,15 | sudo tee /dev/ttyUSB2
Выполнение AT/USSD команд
bash
Скрипт at.sh для отправки AT команды и получения ответа в переменную:
at.sh "AT+COPS=3,0\nAT+COPS?" /dev/ttyUSB2
#!/bin/bash AT="$1" TTY="$2" SLEEP=0 RESULT=$(cat $TTY & echo -e "$AT" > $TTY; sleep $SLEEP; kill %cat) echo $RESULT
Но вы получите ответ вида:
AT+COPS? +COPS: 0,0,"lifecell",2 OK ^BOOT:36507742,0,0,0,87 ^RSSI:3 ^RSSI:3 ^RSSI:3
minicom
Можно отправить AT команду и перенаправить весь вывод в файл:
sudo minicom -D /dev/ttyUSB2 -S ./at/carrier-name.txt -C output.txt
После этого нужно закрыть терминал и разобрать/декодировать весь вывод из файла output.txt. Не круто..
ussd.py
В сети нашел python скрипт для отправки USSD запросов (немного поправил):
122 SMS po Ukraine. 3G: 1024 MB Nomer do 01.05.18.
#!/usr/bin/python #coding: utf8 import base64, io, tty, sys import time ussd = sys.argv[1] modem = '/dev/ttyUSB5' if len(sys.argv) > 2: modem = sys.argv[2] else: modem = '/dev/ttyUSB2' def write2p(a): #sp.write(bytes(a + '\r\n', 'utf8')) sp.write(a + '\r\n') def gsm7bitencode(src): """ Encode ASCII text to 7-bit encoding """ result, count, last = [], 0, 0 for c in src: this = ord(c) > 8) | (this & 0xFF))) count = (count + 1) % 8 last = this result.append('%02x' % (last >> 8)) return ''.join(result) def gsm7bitdecode(f): f = ''.join(["".format(int(f[i:i+2], 16)) for i in range(0, len(f), 2)][::-1]) return ''.join([chr(int(f[::-1][i:i+7][::-1], 2)) for i in range(0, len(f), 7)]) ### sp = io.open(modem, 'w+b', 0) tty.setraw(sp) write2p('AT+CUSD=1,' + gsm7bitencode(ussd) + ',15') ### start = time.time() while (time.time() - start < 10): l = sp.readline() #print(l) if l.startswith('+CUSD'): ussd_response = l[10:l.rfind('"')] response_decoded = gsm7bitdecode(ussd_response) print(response_decoded) break # @note Реализация без таймаута # @todo Нужен таймаут для прерывания ридера потока, если ничего #for l in sp: ##print(l) #if l.startswith('+CUSD'): #ussd_response = l[10:l.rfind('"')] ##print(ussd_response) #response_decoded = gsm7bitdecode(ussd_response) #print(response_decoded) #break sp.close()
Кодировка GSM 7bit в USSD запросах
Кодирование в gsm7bit
При отправке USSD запросов нужно кодировать USSD-код в кодировку GSM 7bit (как я понял из-за того, что модем не поддерживает текстовый режим).
Следующие запросы вернут ERROR:
AT+CUSD=1,"*111#","15" AT+CUSD=1,"*121#",15
Python функция gsm7bitencode для кодирования utf8 строки в gsm7bit:
def gsm7bitencode(src): """ Encode ASCII text to 7-bit encoding """ result, count, last = [], 0, 0 for c in src: this = ord(c) > 8) | (this & 0xFF))) count = (count + 1) % 8 last = this result.append('%02x' % (last >> 8)) return ''.join(result)
Примечание
Смотрите онлайн-сервис для работы с кодировками: http://smstools3.kekekasvi.com/topic.php?id=288
Пример USSD команд в кодировке 7bit gsm проверки баланса:
AT+CUSD=1,"AA582C3602",15 # *111# AT+CUSD=1,"AA182C3602",15 # *121#
Кодированные ответы (ответ всегда приходит на устройство /dev/ttyUSB2):
+CUSD: 0,"C2303BEC9E83662E98ED2C77B340E2B7BB3E07C15C30D859EE762914",15 +CUSD: 0,"C2303BEC9E83662E98ED2C77B340E2B7BB3E07C15C30D859EE7629542A9502442CD3C3ECB40EA48AC9622317E8299687E9697A196477A7DB6177BACC02B9DFEDB21C4466E7C320B8FC6D2FCBD7699038CC0EBBE761103B6D2E8FCB6C3648158BC5460A05",15
Декодирование из gsm7bit
Python функция gsm7bitdecode для декодирования gsm7bit в utf8:
def gsm7bitdecode(f): f = ''.join(["".format(int(f[i:i+2], 16)) for i in range(0, len(f), 2)][::-1]) return ''.join([chr(int(f[::-1][i:i+7][::-1], 2)) for i in range(0, len(f), 7)])
How to send AT commands to device
AT commands (“attention commands” formally, the Hayes command set), are used to communicate directly with a modem device and configure it.
From OpenWrt
To send AT commands directly from OpenWrt, you can simply use echo to send them to the right device. However, you will not get any errors, confirmation or any other answer from the modem.
A better solution is to use socat , which can both send commands and print the modem's answers.
socat
socat will open a prompt where you can enter AT commands (see examples in the section below).
For instance, to access a Quectel EP06 LTE modem, which creates 4 devices ( /dev/ttyUSB0 through to /dev/ttyUSB3 ):
(Here, socat sends a carriage return (cr) and a new line (nl) after each command, which seems to be needed for this modem.)
To find the device on which to run socat , try looking through dmesg . Something like the following may help:
dmesg | grep -A 1 -B 12 ttyUSB
Some modems also show up as a ttyACM device.
echo
Use echo -e so as to be able to escape characters such as “ .
From a computer
To send AT commands to a LTE modem, you need to first connect the device/modem to the computer, most likely using an adapter (built-in modem slots are very rare these days) and access it with a COM terminal.
If you are not familiar with using COM terminals, you might want to use a graphical tool like: CuteCom or minicom . Installation of these are beyond the scope of this page.
These settings should work fine:
Device: /dev/ttyUSB0 Connection: 115200 @ 8-N-1 Line end: CR
Examples of AT commands
To test things are working, you can issue ATI which should return basic information such as brand, model and firmware revision.
AT+CSQ can be used to get signal strength. The values returned are the RSSI (received signal strength indication,higher is better) and BER (bit error rate, lower is better)
Here is example for the Huawei E392 LTE/3G dongle:
Send: AT OK Send: AT^SETPORT=? Recieve: 1:MODEM Recieve: 2:PCUI Recieve: 3:DIAG Recieve: 4:PCSC Recieve: 5:GPS Recieve: 6:GPS CONTROL Recieve: 7:NDIS Recieve: A:BLUE TOOTH Recieve: B:FINGER PRINT Recieve: D:MMS Recieve: E:PC VOICE Recieve: A1:CDROM Recieve: A2:SD Recieve: OK Send: AT^SETPORT? Recieve: A1,A2;1,2,3,A1,A2 Recieve: OK Send: AT^SETPORT="A1;2,7,A2" Recieve: OK Send: AT^SETPORT? Recieve: A1;2,7,A2 Recieve: OK
AT^SETPORT=? - Lists the Available interfaces and their numbers AT^SETPORT? - Show current configuration AT^SETPORT="A1;2,7 - Sets configuration.
Modem configuration is split into 2 parts: before ; and after.
Once a modem is plugged-in, it should declare itself in first configuration (normally with at least: A1 - virtual CD drive with Drivers and application). If the drivers are installed, they will see the modem and issue a special command to switch to a “working” configuration - this is 2,7 interfaces in this example.
Warning: Never turn Off PC interface! (2:PCUI in this example) You will lose the ability to access modem with terminal and change the configuration.
You can add more interfaces to be active i.e. SD card:
If you get an ERROR, maybe the numerical mode is not sorted (16,2,7)→(2,7,16). If your device answers to set command with OK but AT^SETPORT? doesn't show your desired settings, you can try using space in between numerical modes(2,7) and alphabetical modes(A2) like so:
How to force LTE connection
Using telnet / SSH command line.
Number format, network search mode 0 AUTO 1 GSM only 2 UMTS only 3 LTE only Number format, when to take effect 0 Take effect after UE reboots 1 Take effect immediately
echo -e "AT+QCFG=\"nwscanmode\",3,1" > /dev/ttyUSB2
echo -e "AT+QCFG=\"nwscanmode\",0,1" > /dev/ttyUSB2
Further references
This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website. OK More information about cookies
Self-registration in the wiki has been disabled.
If you want to contribute to the OpenWrt wiki, please post HERE in the forum or ask on IRC for access.
Except where otherwise noted, content on this wiki is licensed under the following license:
CC Attribution-Share Alike 4.0 International