C# 在gsmcomm中读取短消息并存储到数据库中
我已注册eventmessage receivedeventhandler以在调制解调器中接收传入的sms。我正在使用gsmcomm库来做这件事。我无法阅读收到的短消息,但当收到的短消息没有短消息时,我没有问题,它工作正常 这是我的代码:C# 在gsmcomm中读取短消息并存储到数据库中,c#,oracle11g,gsmcomm,C#,Oracle11g,Gsmcomm,我已注册eventmessage receivedeventhandler以在调制解调器中接收传入的sms。我正在使用gsmcomm库来做这件事。我无法阅读收到的短消息,但当收到的短消息没有短消息时,我没有问题,它工作正常 这是我的代码: private void comm_MessageReceived(object sender, MessageReceivedEventArgs e) { try { var obj = e.IndicationObject;
private void comm_MessageReceived(object sender, MessageReceivedEventArgs e)
{
try
{
var obj = e.IndicationObject;
if (obj is MemoryLocation)
{
var loc = (MemoryLocation)obj;
var msgs = string.Format("Pesan Baru Diterima dalam SIM \"{0}\", index {1}.", loc.Storage, loc.Index);
MessageBox.Show(msgs, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
DecodedShortMessage[] messages = CommSetting.comm.ReadMessages(PhoneMessageStatus.All, PhoneStorageType.Sim);
foreach (DecodedShortMessage message in messages)
{
StoreSMSToDatabase(message.Data); //save sms recieve to database
}
return;
}
if (obj is ShortMessage) // when short message recieve couldn't store to database
{
var msgs = (ShortMessage)obj;
var pdu = CommSetting.comm.DecodeReceivedMessage(msgs);
MessageBox.Show("Message Recieve");
DecodedShortMessage[] messages = CommSetting.comm.ReadMessages(PhoneMessageStatus.All, PhoneStorageType.Sim);
foreach (DecodedShortMessage message in messages)
{
StoreSMSToDatabase(message.Data); //save sms recieve to database
}
return;
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
以下是要存储到数据库的代码:
private void StoreSMSToDatabase(SmsPdu pdu)
{
if (pdu is SmsDeliverPdu)
{
SmsDeliverPdu data = (SmsDeliverPdu)pdu;
var phoneNumber = data.OriginatingAddress;
var msg = data.UserDataText;
var tanggal = string.Format("{0:dd/MM/yyyy}", data.SCTimestamp.ToDateTime());
var jam = string.Format("{0:HH:mm:ss}", data.SCTimestamp.ToDateTime());
/*---------------------- insert into database ------------------*/
if (koneksi_manual.con.State == ConnectionState.Open)
{
koneksi_manual.con.Close();
}
koneksi_manual.con.Open();
OracleCommand cmd = new OracleCommand();
cmd.CommandText = @"INSERT INTO PESANMASUK (IDMASUK, TANGGALMASUK, JAM, NOMERHP, ISIPESAN, USRPPP) VALUES
(SQ_PESANMASUK.NEXTVAL, '" + tanggal + "', '" + jam + "', '" + phoneNumber + "', '" + msg.Replace("'", "''") + "', '"+ Program.NilaiIDUser +"')";
cmd.Connection = koneksi_manual.con;
cmd.ExecuteReader();
/*-------------------------------------------------------------------------*/
//delete all messages in storage defined by you: sim or mem
CommSetting.comm.DeleteMessages(DeleteScope.All, PhoneStorageType.Sim);
}
}
任何人都可以建议我如何阅读短消息并将其存储到数据库中