published on

Sending/reading SMS of a Huawei 3G dongle

Sending/reading SMS of a Huawei 3G dongle

A quick intro with a few examples on how you can get minimal basic sending/reading functionality for most Huawei 3G/4G dongles.

gcom/comgt

gcom and comgt is the same tool, but had to switch names. It’s a tool/scripting lanuage which allows one to communicate with 3G dongles and probably any other serial device.

huawei

Most Huawei 3G/4G modem’s I’ve come in contact with uses a serial AT interface for controlling it and we can use it to do all kinds of things. I’ll focus on sending/receiving/listing SMS though as this allows for all kinds of fun things like sending yourself a SMS when something happens on the connected device or controlling it with SMS.

running a comgt script

To run a script you pass it the device and the script to run.

gcom -d /dev/ttyUSB0 -v -e -s <script.comgt>

This runs it in a very verbose way and you probably want to drop a few options when you run it for real.

Check the manpage to learn about the options and the scripting language.

Listing SMS in text mode

The huawei (and maybe other) can list and send text messages in both text and PDU mode. The first is what it sounds like, it will output text on the serial port, which does not work very well with special characters and such. The PDU is a special format which handles multi-part messages and such. I’ll focus on the text as if it’s available and you send/receive messages mostly in ascii it will be enough and it’s much more simple.

The at command to list messages stored on the SIM is

AT+CMGL="ALL"

which lists both read-unread, you can also pass it it “REC UNREAD” or “REC READ” for listing only unread/read messages.

If we make this into a comgt script it could look like this

opengt
  # set the terminal settings
  set com 115200n81
  # echo commands
  set comecho on
  # use a tiny delay between each keypress
  set senddelay 0.02
  # wait for things to be silent on the serial port
  waitquiet 1 0.2
  # reset things a bit
  flash 0.1
  # set it to textmode
  send "AT+CMGF=1^m"
  # wait for OK
  waitfor 1 "OK"
  # tell it to list all messages
  send "AT+CMGL=\"ALL\"^m"
  # loop over each line received
  :getl
    # store the line, up until ^m into $l
    get 10 "^m" $l
    # if we time out something went wrong
    if % = -1 goto endl
    # print it
    print $l
    # if we have \nOK in $l, exit loop
    if len($l) < 2 goto getl
    if $right($l,2) = "OK" goto endl
    # loop
    goto getl
  :endl
  exit 0

This is a very basic script, does nothing fancy except list all messages in the raw format the modem returns which should look something like this

+CMGL: 1,"REC READ","+41123456789",,"15/02/29,15:46:00+01"
This is a read message
+CMGL: 2,"REC UNREAD","+41123456789",,"15/02/28,04:03:00+01"
Hello this is a unread sms, it will be marked read after this

it has no error handling so if your modem behaves differently than mine, it will probably not work. Also there might be other output in between, filtering that out is left as a excersise for the reader.

If you see just a bunch of 0012033300012349500023410230023 then the message is encoded in some non-ascii or similar. I’m not sure how to decode that, if so you might have to switch to PDU mode and use a PDU decoder to get the data out.

Sending SMS in text mode

opengt
  # set the terminal settings
  set com 115200n81
  # echo commands
  set comecho on
  # use a tiny delay between each keypress
  set senddelay 0.02
  # wait for things to be silent on the serial port
  waitquiet 1 0.2
  # reset things a bit
  flash 0.1
  # set it to textmode
  send "AT+CMGF=1^m"
  # wait for OK
  waitfor 1 "OK"
  # tell it we want to send a SMS and to what number
  send "AT+CMGS=\"+41554433221\"^m"
  # the modem will present a > and wait for you to input your message
  waitfor 1 ">"
  send "Hello testing" 
  # and expect you to end your message with ^Z
  send "^Z"
  # wait for it to send
  waitfor 5 "OK"
  exit 0

The above script will send a text message containing “Hello testing” to +41554433221, assuming you can send text and such. Otherwise it will return an CME or CMS error number.