S
S
Sergey Torop2020-09-28 22:38:28
linux
Sergey Torop, 2020-09-28 22:38:28

How to communicate with modem in CLI linux?

Good afternoon,
I have linux (Raspbian, raspberry pi) and a GSM modem, visible as /dev/ttyUSB0.

The task is to give simple commands to the modem from the CLI, receive and process
responses to it.

Found the atinout program and also tried using:
echo "at+csq"| socat - /dev/ttyUSB0,crnl

At the beginning they worked, but at some point both of them started to issue
a bunch of garbage after the response (command repetition, response, many empty lines, etc.).

If after atinout and socat you run the good old "minicom -D /dev/ttyUSB0",
then at the beginning the same garbage will run through, and then you can enter your own
AT command. It seems that the "input buffer" of the modem is clogged with the output of the response
from the modem (response from command request "echo AT | atinout - /dev/ttyUSB0 -" ).

Possible incorrect interpretation of the response from the modem and the console (regarding CRLF).

Tried
# echo AT | atinout - /dev/ttyUSB0 -
AT
OK
followed by
# stty -F /dev/ttyUSB0 raw

followed by
# echo AT | atinout - /dev/ttyUSB0 -
you get
^MAT^MATAT
ERROR
because there is something left in the "buffer" from the previous response.

Who decides how to communicate with the modem from the CLI and what could be the problem
in the above method? Thank you.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
Sergey Torop, 2020-10-04
@TSerge

As an alternative to the above, you can use a simple script
to send commands to the serial port, with a preliminary. clearing the buffer and receiving a modem response:

#!/usr/bin/env python
# coding: utf8

import sys
import serial
import time

modem_port = '/dev/gsmmodem'
modem_baudrates = [2400, 9600, 19200, 38400, 115200]
modem_bytesize = 8
modem_parity = serial.PARITY_NONE
modem_stopbits = 1

ser_modem = serial.Serial(modem_port, modem_baudrates[1], parity=modem_parity)

ser_modem.flushInput()
ser_modem.flushOutput()

try:
    command = sys.argv[1] + '\r'
except:
    command = 'AT' + '\r'

for i in command:
    ser_modem.write(i)
    time.sleep(0.1)

res = []
while ser_modem.inWaiting() > 0:
    res.append(ser_modem.readline())

ser_modem.close()

print (''.join(res))

A
Alexey Cheremisin, 2020-09-28
@leahch

Like an old schooler, when working with modems I used chat , expect , minicom , wvdial and python with serial module . Through the latter, I wrote a bunch of things about working with modems, because it's simple, fast and beautiful. As an example, reading statistics (from usb modems with multiple ttys), reboots, sending sms and other nonsense - https://pyserial.readthedocs.io/en/latest/index.html
Well, at least send +++ first after connection when the port is opened. This is a universal escape sequence, served without the AT prefix .

K
Karpion, 2020-09-29
@Karpion

We look at the / dev / file of the COM port. We read man on it - there, most likely, there is a list of programs for working with the COM port.
Or you can start with the getty program. Well, it would be nice to remember about ioctl - setting the parameters of the port.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question