Search code examples
phpencodinggsmat-command

GSM modem AT commands UCS2 error 500


Three days ago I started making a simple app to send SMS. I already tested it and it works in GSM CSCS mode, but when I switch it to UCS2 it doesn't show the Cyrillic letters

<?php
error_reporting(E_ALL);

$fp = fopen("/dev/ttyUSB0", 'w+');

$msg = strtoupper(bin2hex(mb_convert_encoding("Тест", "UCS-2", "UTF-8")));
$number_raw = "+359000000000";
$number = bin2hex(mb_convert_encoding($number_raw, "UCS-2", "UTF-8"));
echo $number."<br>";
echo $msg;
$debug = false;
if(!$fp || $debug){
  //echo "Can not open!";
}else{
  fwrite($fp, "AT+CMGF=1".chr(13)); // OK
  sleep(5);
  fwrite($fp, 'AT+CSCS="UCS2"'.chr(13)); // OK
  sleep(5);
  fwrite($fp, 'AT+CMGS="'.$number.'"'.chr(13)); // OK
  sleep(5);
  fwrite($fp, $msg.chr(26)); // ERROR 500
  echo "<br>Sent";
}
?>

Number and message are encoded properly according to this source: http://www.columbia.edu/kermit/ucs2.html

When the message is sent, I receive it (so the encoding of the number is correct) but the content isn't shown properly.

https://i.sstatic.net/JZaow.jpg

What are possible causes for this behaviour and could it be my PHP file encoding? Also why is linux finding 3 GSM tty devices?


Solution

  • After you have removed all the horrible sleep calls and implemented proper final response parsing, then you need fix the parsing for waiting for the "\r\n> " response of AT+CMGS.

    Without any of that fixed, character set encoding trouble is a very minor problem. When you run AT+CSCS="UCS2" every single string will be encoded by the modem that way and must be encoded that way by you until eternity (or another character set is selected), so for instance to switch from UCS2 to UTF-8 would be AT+CSCS="005500540046002D0038" and not AT+CSCS="UTF-8".