public static void ReceiveSMS(Settings.DevicesItem Device) { var Data = RunCommandWithFeedBack(Device.ModemPort, "AT+CPMS= \"ME\",\"ME\",\"ME\""); if (!Data.Contains("OK")) { throw new Exception($"Init Failure:{Data}"); } Data = RunCommandWithFeedBack(Device.ModemPort, "AT+CPMS?"); if (!Data.Contains("OK")) { throw new Exception($"Init Failure:{Data}"); } var RegexStr = "\\r\\n\\+CPMS: [\\\"A-Z,0-9]*\\r\\n"; Data = Regex.Match(Data, RegexStr).ToString().Replace("\r\n", "").Replace("+CPMS: ", "").Replace("\"", ""); var SMSNum = Int32.Parse(Data.Split(',')[1]); if (SMSNum == 0) { return; } for (var SMSCount = 0; SMSNum >= SMSCount; SMSCount++) { Data = RunCommandWithFeedBack(Device.ModemPort, "AT+CMGR=" + SMSCount.ToString()); if (!Data.Contains("OK")) { throw new Exception("Recv Failure"); } RegexStr = $"\r\n([0-9A-F]*)\r\n"; Data = Regex.Match(Data, RegexStr).ToString().Replace("\r\n", ""); if (Data == "") { continue; } GsmComm.PduConverter.IncomingSmsPdu PDU = IncomingSmsPdu.Decode(Data, true); var SendTime = "GMT" + PDU.GetTimestamp().ToDateTime().ToString("z yyyy-MM-dd HH:mm:ss"); var ReceiveTime = "GMT" + DateTime.Now.ToString("z yyyy-MM-dd HH:mm:ss"); var SMSC = PDU.SmscAddress; var Msg = PDU as SmsDeliverPdu; var From = Msg.OriginatingAddress; var To = Device.Name + "@" + Device.PhoneNumber; var Tittle = From + "->" + To; Data = "From:" + From + "\r\n" + "To:" + To + "\r\n" + "Send:" + SendTime + "\r\n" + "Received:" + ReceiveTime + "\r\n" + "SMSC:" + SMSC + "\r\n" + PDU.UserDataText; Console.WriteLine($"{Device.Name}-SMS Received:{Tittle}"); Console.WriteLine($"SMS:{Data}"); try { Device.PostMessage(Tittle, Data); } catch (Exception ex) { throw new Exception($"Push Exception: {ex.Message}"); } Data = RunCommandWithFeedBack(Device.ModemPort, "AT+CMGD=" + SMSCount.ToString()); } }
/// <summary> /// Decodes an incoming SMS PDU stream. /// </summary> /// <param name="pdu">The PDU string to decode.</param> /// <param name="includesSmscData">Specify true if the PDU data contains an SMSC header, otherwise false.</param> /// <param name="actualLength">The size of the PDU data in bytes, not counting the SMSC header. Set to -1 if unknown.</param> /// <returns>An <see cref="T:GsmComm.PduConverter.IncomingSmsPdu" /> object representing the decoded message.</returns> public static IncomingSmsPdu Decode(string pdu, bool includesSmscData, int actualLength) { if (pdu != string.Empty) { int num = 0; if (includesSmscData) { int num1 = num; num = num1 + 1; byte num2 = BcdWorker.GetByte(pdu, num1); if (num2 > 0) { num = num + num2; } } int num3 = num; IncomingMessageType messageType = IncomingSmsPdu.GetMessageType(BcdWorker.GetByte(pdu, num3)); IncomingMessageType incomingMessageType = messageType; switch (incomingMessageType) { case IncomingMessageType.SmsDeliver: { return(new SmsDeliverPdu(pdu, includesSmscData, actualLength)); } case IncomingMessageType.SmsStatusReport: { return(new SmsStatusReportPdu(pdu, includesSmscData, actualLength)); } } throw new NotSupportedException(string.Concat("Message type ", messageType.ToString(), " recognized, but not supported by the SMS decoder.")); } else { throw new ArgumentException("pdu must not be an empty string."); } }
/// <summary> /// Decodes an incoming SMS PDU stream. /// </summary> /// <param name="pdu">The PDU string to decode.</param> /// <param name="includesSmscData">Specify true if the PDU data contains an SMSC header, otherwise false.</param> /// <returns>An <see cref="T:GsmComm.PduConverter.IncomingSmsPdu" /> object representing the decoded message.</returns> /// <remarks>Use this overload only if you do not know the size of the PDU data.</remarks> public static IncomingSmsPdu Decode(string pdu, bool includesSmscData) { return(IncomingSmsPdu.Decode(pdu, includesSmscData, -1)); }