利用串口实现短信发送功能的一个国外程序

分享

国外写的一个利用串口连接设备程序,实现设备连接配置-发送短信-读取短信-删除短信功能。大家可以参考下:这个程序只适合发送英文短信,需要发送中文短信需要添加短信猫二次开发将uniCode转化为中文的程序才能实现。



165600zij6odi6piyp9z4d

public SerialPort OpenPort(string p_strPortName,

       int p_uBaudRate, int p_uDataBits,

       int p_uReadTimeout, int p_uWriteTimeout)

{

    receiveNow = new AutoResetEvent(false);

    SerialPort port = new SerialPort();


    try

    {

        port.PortName =p_strPortName;       //COM1     
        port.BaudRate = p_uBaudRate;        //9600         
        port.DataBits = p_uDataBits;            //8         
        port.StopBits =StopBits.One;           //1         
        port.Parity = Parity.None;                 //None         
        port.ReadTimeout =p_uReadTimeout;           //300         
        port.WriteTimeout = p_uWriteTimeout;           //300         
        port.Encoding =Encoding.GetEncoding("iso-8859-1");

        port.DataReceived += newSerialDataReceivedEventHandler

                (port_DataReceived);

        port.Open();

        port.DtrEnable = true;

        port.RtsEnable = true;

    }

    catch (Exception ex)

    {

        throw ex;

    }

    return port;

}


Send SMS


In the second tab, you can send SMS:


165601e76ufux6fq8x8qq6



public bool sendMsg(SerialPort port, string PhoneNo, string Message)

{

    bool isSend = false;


    try

    {

        string recievedData =ExecCommand(port,"AT", 300, "No phone connected");

        recievedData =ExecCommand(port,"AT+CMGF=1", 300,

            "Failed to setmessage format.");

        String command = "AT+CMGS="" + PhoneNo + """;

        recievedData =ExecCommand(port,command, 300,

            "Failed toaccept phoneNo");

        command = Message + char.ConvertFromUtf32(26) + " ";

        recievedData =ExecCommand(port,command, 3000,

            "Failed to sendmessage"); //3 seconds         if(recievedData.EndsWith(" OK "))

        {

            isSend = true;

        }

        else if (recievedData.Contains("ERROR"))

        {

            isSend = false;

        }

        return isSend;

    }

    catch (Exception ex)

    {

        throw new Exception(ex.Message);

    }

}


Read SMS


In the third tab, you can read SMS:


165602bd33923x3eoiyx92




public ShortMessageCollection ReadSMS(SerialPort port)

{

    // Set up the phoneand read the messages     ShortMessageCollection messages =null;

    try

    {

        #region Execute Command

        // Check connection         
ExecCommand(port,"AT", 300, "No phone connected");

        // Use message format"Text mode"         
ExecCommand(port,"AT+CMGF=1", 300, "Failed to setmessage format.");

        // Use character set"PCCP437"         
ExecCommand(port,"AT+CSCS="PCCP437"", 300,

        "Failed to setcharacter set.");

        // Select SIM storage        
ExecCommand(port,"AT+CPMS="SM"", 300,

        "Failed toselect message storage.");

        // Read the messages         
string input = ExecCommand(port,"AT+CMGL="ALL"", 5000,

            "Failed to readthe messages.");

        #endregion


        #region Parse messages

        messages =ParseMessages(input);

        #endregion

    }

    catch (Exception ex)

    {

        throw new Exception(ex.Message);

    }


    if (messages != null)

        return messages;

    else

        return null;

}


Delete SMS


In the fourth and last tab, you can count the number of SMS and delete SMSas well.


165602z5qzz4mqmk6rllzm


public bool DeleteMsg(SerialPort port , string p_strCommand)

{

    bool isDeleted = false;

    try

    {

        #region Execute Command

        string recievedData =ExecCommand(port,"AT", 300, "No phone connected");

        recievedData =ExecCommand(port,"AT+CMGF=1", 300,

            "Failed to setmessage format.");

        String command = p_strCommand;

        recievedData =ExecCommand(port,command, 300, "Failed todelete message");

        #endregion


        if (recievedData.EndsWith(" OK "))

        {

            isDeleted = true;

        }

        if (recievedData.Contains("ERROR"))

        {

            isDeleted = false;

        }

        return isDeleted;

    }

    catch (Exception ex)

    {

        throw new Exception(ex.Message);

    }

}


短信设备二次开发 2022-04-03 0 0

admin

  • 注册时间 : 2022-03-30 04:08:47
  • 邮箱地址 : admin@tenghengkeji.com
  • 此页面用于展示用户的基本上资料信息

回帖 ( 0 )