Send USSD code to modem in C# and ERROR in return always

社会主义新天地 提交于 2019-12-02 02:55:20

问题


I am sending USSD code on modem through serial port. But always it is giving ERROR in response.

AT commands I am sending are: in sequence:

serialPort.Write("AT+CMGF=0" + "\r\n");
serialPort.Write("AT+CUSD=1,\"*135#\"" + "\r\n");

when I am configuring message format in first AT command, it is giving 'OK' response. But on sending USSD code, response is always 'ERROR'. Why is it so?


回答1:


Don't use \n in end of command, use only \r.

Form of CUSD command is: AT+CUSD=1,"*135#",15.

In C# it should be:

serialPort.Write("AT+CMGF=0" + "\r");
serialPort.Write("AT+CUSD=1,\"*135#\",15" + "\r");



回答2:


First, AT command is ended by "\r" in C#. You may also to check TE Character set of your modem.

serialPort.Write("AT+CSCS?\r");

if Character Set is "UCS2" use the following conversation:

serialPort.Write("AT+CUSD=1,\"" + UnicodeStr2HexStr("*135#") + "\",15" + "\r");

And

public static String UnicodeStr2HexStr(String strMessage)
{
    byte[] ba = Encoding.BigEndianUnicode.GetBytes(strMessage);
    String strHex = BitConverter.ToString(ba);
    strHex = strHex.Replace("-", "");
    return strHex;
}

Please see also https://stackoverflow.com/a/25155746/638977 (Convert to UCS2)



来源:https://stackoverflow.com/questions/21019475/send-ussd-code-to-modem-in-c-sharp-and-error-in-return-always

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!