您的位置:首页 > 其它

如何通过GSM猫发送和接受GSM短信

2010-03-31 15:02 459 查看
原文地址:http://www.dingos.cn/index.php?topic=359.0



介绍

此项目主要功能是发送和接受SMS短信。它监听到来的信息,处理格式正确的信息,怎样处理将在下面详细的给您介绍。我将主要解释以下几点:
1、通讯端口设定
2、接收传进来的消息
3、发送消息
4、读取所有消息(由用户发送的)
5、删除消息(一条或者所有)

我将使用GSMComm类库来发送和接手SMS短信。你需要一个GSM猫或者电话来发送SMS短信。

代码片断

1、通讯端口设定




CommSetting类主要用于保存通讯端口的设定:

public class CommSetting{
public static int Comm_Port=0;
public static Int64 Comm_BaudRate=0;
public static Int64 Comm_TimeOut=0;
public static GsmCommMain comm;
public CommSetting() {
//
// TODO: Add constructor logic here
//
}
}

Comm是一个GsmCommMain的对象,主要是用来发送和接收消息的。我 们必须设置其通讯端口(串口),波特率和超时时间。最后使用上面的设置来打开端口。当我们设置好端口、波特率和超时后可以通过点击“test”按钮来测试 其设置。 假如端口不能打开,你将获得一条“No phone connected”提示,这可能主要是波特率设置错误的结果,调整波特率直到点击“test”按钮后获得一条“Successfully connected to the phone.”的提示。

在创建GSMComm对象之前,我们必须确定其端口、波特率和超时时间设置的正确性。

EnterNewSettings()就是此有效性检查的函数,假如有效将返回true,而且其将调用SetData(port,baud,timeout)来设置端口参数。

下面的代码就是试着连接端口。假如有任何问题发生,"Phone not connected"将会提示,你可以选择“retry”按钮或者“Cancel”按钮。

程序代码:
GsmCommMain comm = new GsmCommMain(port, baudRate, timeout);
try{
comm.Open();
while (!comm.IsConnected()){
Cursor.Current = Cursors.Default;
if (MessageBox.Show(this, "No phone connected.",
"Connection setup", MessageBoxButtons.RetryCancel,
MessageBoxIcon.Exclamation) == DialogResult.Cancel){
comm.Close();
return;
}
Cursor.Current = Cursors.WaitCursor;
}
// Close Comm port connection (Since it's just for testing
// connection)
comm.Close();
}catch(Exception ex){
MessageBox.Show(this, "Connection error: " + ex.Message,
"Connection setup", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
// display message if connection is a success.
MessageBox.Show(this, "Successfully connected to the phone.","Connection setup",
MessageBoxButtons.OK, MessageBoxIcon.Information);



2、接收传进来的消息
我们将处理GSMComm对象的下列事件。

PhoneConnected
当你试着打开通讯端口(串口)事,将会产生此事件。 comm_PhoneConnected是这个事件的处理, 其将通过ConnectHandler代理来调用OnPhoneConnectionChange(bool connected)。
MessageReceived
当有消息来到时将会产生此事件。 我将注册MessageReceivedEventHandler。当有消息来到是,将会调用comm_MessageReceived方法,其将会调用MessageReceived()方法来处理未读消息。GSMComm对象comm 有一个方法 ReadMessages,其被用于读取消息。其接收以下几个电话状态的参数(All, ReceivedRead, ReceivedUnread, StoredSent, and StoredUnsent)和一个存储类型:SIP卡或者Phone内存。

程序代码:
private void MessageReceived(){
Cursor.Current = Cursors.WaitCursor;
string storage = GetMessageStorage();
DecodedShortMessage[] messages =
CommSetting.comm.ReadMessages(PhoneMessageStatus.ReceivedUnread, storage);
foreach(DecodedShortMessage message in messages){
Output(string.Format("Message status = {0},Location = {1}/{2}",
StatusToString(message.Status),message.Storage, message.Index));
ShowMessage(message.Data);
Output("");
}
Output(string.Format("{0,9} messages read.",messages.Length.ToString()));
Output("");
}
上面的代码将会从SIM卡上读取未读消息。ShowMessage方法用来显示读取的消息。这个消息或是一条状态报告,存储的一条已发送/未发送消息,或者一条收到的消息。

3、发送消息



你可以通过输入目标电话号码和要发送的信息来发送SMS消息。

加入你需要使用你本地语言(Unicode)来发送消息,你必须确认使用Unicode(UCS2)来发送。GSMComm对象comm有一个SendMessage方法,其可以发送消息给任何电话。发送消息必须创建PDU。我们可以用以下的方法来直接创建一个PDU:

程序代码:
SmsSubmitPdu pdu = new SmsSubmitPdu (txt_message.Text,txt_destination_numbers.Text,"");
另一个扩展版本的PDU主要用来发送Unicode格式的消息。

程序代码:
try{
// Send an SMS message
SmsSubmitPdu pdu;
bool alert = chkAlert.Checked;
bool unicode = chkUnicode.Checked;
if (!alert && !unicode) {
// The straightforward version
pdu = new SmsSubmitPdu (txt_message.Text, txt_destination_numbers.Text,"");
}else{
// The extended version with dcs
byte dcs;
if (!alert && unicode)
dcs = DataCodingScheme.NoClass_16Bit;
else if (alert && !unicode)
dcs = DataCodingScheme.Class0_7Bit;
else if (alert && unicode)
dcs = DataCodingScheme.Class0_16Bit;
else
dcs = DataCodingScheme.NoClass_7Bit;
pdu = new SmsSubmitPdu(txt_message.Text,
txt_destination_numbers.Text, "",dcs);
}
// Send the same message multiple times if this is set
int times = chkMultipleTimes.Checked ?
int.Parse(txtSendTimes.Text) : 1;
// Send the message the specified number of times
for (int i=0;i<times;i++) {
CommSetting.comm.SendMessage(pdu);
Output("Message {0} of {1} sent.", i+1, times);
Output("");
}
}catch(Exception ex){
MessageBox.Show(ex.Message);
}
Cursor.Current = Cursors.Default;
4、读取所有消息



你只要点击“Read All Messages”按钮就能从SIM卡或者Phone内存中读取所有消息。消息的细节比如发送者,时间,消息内容都会显示在Data Grid。每条消息都会创建一新行,添加到Data table且把这个Data table设置为datagrid的source。

程序代码:
private void BindGrid(SmsPdu pdu){
DataRow dr=dt.NewRow();
SmsDeliverPdu data = (SmsDeliverPdu)pdu;
dr[0]=data.OriginatingAddress.ToString();
dr[1]=data.SCTimestamp.ToString();
dr[2]=data.UserDataText;
dt.Rows.Add(dr);
dataGrid1.DataSource=dt;}
上面的代码为从SIP卡内读取所有未读消息。ShowMessage方法用来显示读取的消息。这个消息或是一条状态报告,存储的一条已发送/未发送消息,或者一条收到的消息。未一的区别就是调用方法的第一个参数的值不同。

读取未读消息

程序代码:
DecodedShortMessage[] messages =
CommSetting.comm.ReadMessages(PhoneMessageStatus.ReceivedUnread,
storage);

读取所有消息

程序代码:
DecodedShortMessage[] messages =
CommSetting.comm.ReadMessages(PhoneMessageStatus.All, storage);

5、删除消息(一条或所有)



所有由用户发送的消息都被存储在SIP卡内,我将在一个Data grid里显示这些。我们可以通过特定的索引号(index number)来删除一条单一的消息。我们通过点击“Delete All”按钮来删除SIP卡上的所有消息。每条消息都通过一个下标来索引,所以删除也是通过这个索引来定位的。

下面的代码就是基于索引来删除消息的:

程序代码:
// Delete the message with the specified index from
storageCommSetting.comm.DeleteMessage(index, storage);

删除所有的消息( SIM/Phone):

程序代码:
// Delete all messages from phone
memoryCommSetting.comm.DeleteMessages(DeleteScope.All, storage);

DeleteScope是个枚举类型,其值如下:

All

Read

ReadAndSent

ReadSentAndUnsent
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: