Search code examples
serial-portat-commandtty

Sending AT command through bash script


I'm testing some satellital modem with a USB-to-serial (RS232) converter. I have already tested the "connection", and it works. I'm using minicom and am able to capture data sent from one terminal (running one bash script that echoes random numbers) to another.

To make this modem send things, I must send it AT commands to it. What is the best way to do it? Should I just echo the AT command from my bash script? Or is there any better way?

#!/bin/bash
while true;
  do 
    number=$RANDOM
    echo $number >/dev/ttyUSB0
    sleep 4     
  done

Thank you for your time!


Solution

  • Since you're talking with a modem, the general way to talk with the modem is to use ModemManager, an application that handles the communication with the modem for you.

    If you are unable to use ModemManager or you must use bash for some reason, note that commands must end with a \r\n to be used by the modem. The best way that I have found to do that is to use /bin/echo as follows:

    /bin/echo -n -e "AT\r\n" > /dev/ttyUSB0
    

    This ensures that echo will convert the escape characters to the carriage return and newline appropriately.