我一直在尝试在C#应用中使用GSMComm library,以便使用手机(通过USB线连接)作为GSM调制解调器发送短信。 我在SO中读过所有类似的帖子,但没有帮助。
除了交付报告外,一切运作良好。我将 RequestStatusReport 设置为true并启用了通知( EnableMessageNotifications ())。
问题是我无法读取收到的送达报告,虽然我知道它的存储(它总是“SR”)和索引号。我一直收到321错误代码(索引无效),因为 SR存储在我尝试读取时看起来是空的。
MessageReceived事件代码和相应的报告如下,任何帮助将不胜感激!
private static void Comm_MessageReceived(object sender, MessageReceivedEventArgs e)
{
IMessageIndicationObject obj = e.IndicationObject;
if (obj is MemoryLocation)
{
MemoryLocation loc = (MemoryLocation)obj;
Util.AddLog(string.Format("New message received in storage \"{0}\", index {1}.", loc.Storage, loc.Index));
DecodedShortMessage msg = Comm.ReadMessage(loc.Index, loc.Storage);
if (((SmsPdu)msg.Data) is SmsStatusReportPdu)
{
SmsStatusReportPdu data = (SmsStatusReportPdu)msg.Data;
Util.AddLog("rec msg ref #: " + data.MessageReference.ToString());
}
}
else
{
Util.AddLog("Error: Unknown notification object!");
}
}
报告:
New message received in storage "SR", index 0.
[GSM_LOG] 17:08:49.528 Reading message...
[Catch in MessageReceived] ##### ERROR: Message service error 321 occurred.
[GSM_LOG] 17:08:49.501 [gsmphone] >>
[GSM_LOG] 17:08:49.501 [gsmphone] +CDSI: "SR",0
[GSM_LOG] 17:08:49.501 [gsmphone]
[GSM_LOG] 17:08:49.501 [gsmphone] Unsolicited message: New SMS-STATUS-REPORT received (indicated by memory location)
[GSM_LOG] 17:08:49.501 [gsmphone] Firing async MessageReceived event.
[GSM_LOG] 17:08:49.528 [gsmphone] Selecting "SR" as read storage...
[GSM_LOG] 17:08:49.528 [gsmphone] << AT+CPMS="SR"
[GSM_LOG] 17:08:49.528 [gsmphone]
[GSM_LOG] 17:08:49.528 [gsmphone] >> AT+CPMS="SR"
[GSM_LOG] 17:08:49.528 [gsmphone]
[GSM_LOG] 17:08:49.528 [gsmphone] +CPMS: 0,0,0,23,1,40
[GSM_LOG] 17:08:49.528 [gsmphone]
[GSM_LOG] 17:08:49.528 [gsmphone] OK
[GSM_LOG] 17:08:49.528 [gsmphone]
[GSM_LOG] 17:08:49.528 [gsmphone] Memory status: 0/0 (0% used)
[GSM_LOG] 17:08:49.528 [gsmphone] Activating PDU mode...
[GSM_LOG] 17:08:49.528 [gsmphone] << AT+CMGF=0
[GSM_LOG] 17:08:49.528 [gsmphone]
[GSM_LOG] 17:08:49.528 [gsmphone] >> AT+CMGF=0
[GSM_LOG] 17:08:49.528 [gsmphone]
[GSM_LOG] 17:08:49.528 [gsmphone] OK
[GSM_LOG] 17:08:49.528 [gsmphone]
[GSM_LOG] 17:08:49.528 [gsmphone] Reading message from index 0...
[GSM_LOG] 17:08:49.528 [gsmphone] << AT+CMGR=0
[GSM_LOG] 17:08:49.528 [gsmphone]
[GSM_LOG] 17:08:49.528 [gsmphone] >> AT+CMGR=0
[GSM_LOG] 17:08:49.528 [gsmphone]
[GSM_LOG] 17:08:49.528 [gsmphone] +CMS ERROR: 321
[GSM_LOG] 17:08:49.528 [gsmphone]
[GSM_LOG] 17:08:49.545 [gsmphone] Failed. Phone reports message service (MS) error 321.
[GSM_LOG] 17:08:49.545 [gsmphone] AT+CMGR=0
[GSM_LOG] 17:08:49.545 [gsmphone]
[GSM_LOG] 17:08:49.545 [gsmphone] +CMS ERROR: 321
[GSM_LOG] 17:08:49.545 [gsmphone]
[GSM_LOG] 17:08:49.548 [gsmphone] Ending async MessageReceivedEventHandler call
答案 0 :(得分:1)
指示对象可以是MemoryLocation或ShortMessage。由于您已经拥有该对象并且只需要对其进行解码,因此无需再次从内存位置读取已接收的ShortMessage(用于传递状态)。以下适用于我
private void comm_MessageReceived(object sender, MessageReceivedEventArgs e)
{
try
{
IMessageIndicationObject obj = e.IndicationObject;
if (obj is MemoryLocation)
{
MemoryLocation loc = (MemoryLocation)obj;
log.InfoFormat("New message received in storage {0}, index {1}.", loc.Storage, loc.Index);
}
else if (obj is ShortMessage)
{
ShortMessage msg = (ShortMessage)obj;
SmsPdu pdu = comm.DecodeReceivedMessage(msg);
//Here can be delivery status or received message so cast to the right type by doing type checks for SmsDeliverPdu or SmsStatusReportPdu
}
else
{
log.ErrorFormat("Unknown notification message received");
log.InfoFormat("Message indication object is {0}", e.IndicationObject);
}
}
catch (Exception ex)
{
log.ErrorFormat("An error occurred while message ws received. Error: {0}", ex.Message);
log.Error(ex);
}