public DTO_SMS_Collection ParseMessages(string input) { DTO_SMS_Collection messages = new DTO_SMS_Collection(); try { Regex r = new Regex(@"\+CMGL: (\d+),""(.+)"",""(.+)"",(.*),""(.+)""\r\n(.+)\r\n"); Match m = r.Match(input); while (m.Success) { DTO_SMS msg = new DTO_SMS(); msg.Index = m.Groups[1].Value; msg.Status = m.Groups[2].Value; msg.Sender = m.Groups[3].Value; msg.Alphabet = m.Groups[4].Value; msg.Sent = m.Groups[5].Value; msg.Message = m.Groups[6].Value; messages.Add(msg); m = m.NextMatch(); } } catch (Exception ex) { throw ex; } return(messages); }
public DTO_SMS_Collection ReadSMS(SerialPort port, string p_strCommand) { // Set up the phone and read the messages DTO_SMS_Collection 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 set message format."); // Use character set "PCCP437" ExecCommand(port, "AT+CSCS=\"PCCP437\"", 300, "Failed to set character set."); // Select SIM storage ExecCommand(port, "AT+CPMS=\"SM\"", 300, "Failed to select message storage."); // Read the messages var input = ExecCommand(port, p_strCommand, 5000, "Failed to read the messages."); string input_s = input.Result; #endregion #region Parse messages messages = ParseMessages(input_s); #endregion } catch (Exception ex) { throw ex; } if (messages != null) { return(messages); } else { return(null); } }
private void ReadSMS() { try { //count SMS int uCountSMS = _BUSSMS.CountSMSmessages(port); if (uCountSMS > 0) { #region Command string strCommand = "AT+CMGL=\"ALL\""; #endregion // If SMS exist then read SMS #region Read SMS //.............................................. Read all SMS .................................................... _smsCollection = _BUSSMS.ReadSMS(port, strCommand); foreach (DTO_SMS msg in _smsCollection) { _BUSSMS.InsetSMS_toDatabase(msg, "ReceivedSMS"); } strCommand = "AT+CMGD=1,3"; _BUSSMS.DeleteMsg(port, strCommand); dgvRequest.DataSource = _BUSSMS.LoadSMS("ReceivedSMS"); #endregion } else { //MessageBox.Show("There is no message in SIM"); this.statusBar1.Text = "There is no message in SIM"; } } catch (Exception ex) { cGlobalFunction.ErrorLog(ex.Message); } Invoke(new RefreshReceivedDgv(RfshReceivedDgv), dgvRequest); }