查看: 6833|回复: 1
打印 上一主题 下一主题

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

[复制链接]
跳转到指定楼层
楼主
发表于 2014-3-3 17:30:15 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
国外写的一个利用串口连接设备程序,实现设备连接配置-发送短信-读取短信-删除短信功能。大家可以参考下:这个程序只适合发送英文短信,需要发送中文短信需要添加短信收发设备二次开发将uniCode转化为中文的程序才能实现。


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:

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:

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.

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);
    }
}

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|小黑屋|未来时代科技 ( 粤ICP备12044031号-1

GMT+8, 2024-9-20 07:46 , Processed in 0.081688 second(s), 33 queries .

Powered by WLSD X3.1

© 2013-2014 WLSD Inc.

快速回复 返回顶部 返回列表
 
【电话】(15118131494)
【QQ】 未来时代科技01 售前咨询
【QQ】 未来时代科技02 售后技术
【旺旺】 请问有什么可以帮到您?不在线可留言.
【邮箱】
inextera@sina.com
【地址】 (深圳市龙岗坂田扬马小区)