private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e) { lock (_commPortLock) { var communications = new GsmCommMain(_communicationsPort, _baudRate, _timeout); communications.Open(); communications.DeleteMessages(DeleteScope.All, _storage); communications.Close(); } while (!_backgroundWorker.CancellationPending) { DecodedShortMessage[] messages = null; lock (_commPortLock) { var communications = new GsmCommMain(_communicationsPort, _baudRate, _timeout); communications.Open(); messages = communications.ReadMessages(PhoneMessageStatus.ReceivedUnread, _storage); communications.Close(); } foreach (var message in messages) { _backgroundWorker.ReportProgress(0, message); } Thread.Sleep(1000); } }
internal void readAllMessages() { logger("Read from <2>:\n1.SIM \n2.Phone "); string msgStorage; string usrInput = Console.ReadLine(); if (usrInput.Length <= 0 || usrInput.Equals("2")) { msgStorage = PhoneStorageType.Phone; } else { msgStorage = PhoneStorageType.Sim; } try { // Read all SMS messages from the storage DecodedShortMessage[] messages = comm.ReadMessages(PhoneMessageStatus.All, msgStorage); foreach (DecodedShortMessage message in messages) { logger(string.Format("Message status = {0}, Location = {1}/{2}", StatusToString(message.Status), message.Storage, message.Index)); ShowMessage(message.Data); } logger(string.Format("{0,9} messages read.", messages.Length.ToString())); } catch (Exception ex) { ShowException(ex); } }
private void Read_message() { //pb.IsIndeterminate = true; string storage = GetMessageStorage(); //txtmessage.Text = ""; try { // Read all SMS messages from the storage DecodedShortMessage[] messages = comm.ReadMessages(PhoneMessageStatus.All, "SM"); if (messages.Length == 0) { messages = comm.ReadMessages(PhoneMessageStatus.All, "ME"); } foreach (DecodedShortMessage msg in messages) { SmsPdu pdu = msg.Data; if (pdu is SmsDeliverPdu) { // Received message SmsDeliverPdu data = (SmsDeliverPdu)pdu; String SenderNo = data.OriginatingAddress.ToString(); SenderNo = SenderNo.Replace("+63", "0"); string sdate = "20" + data.SCTimestamp.Year + "-" + data.SCTimestamp.Month + "-" + data.SCTimestamp.Day + " " + data.SCTimestamp.Hour + ":" + data.SCTimestamp.Minute + ":" + data.SCTimestamp.Second; //add to database //Database.DBMessages.DBInbox obj = new Database.DBMessages.DBInbox(); //obj.Add(DateTime.Parse(sdate), SenderNo, data.UserDataText.ToString()); cntmessageReceived++; ReceiveUpdates(); } } // delete all messages DeleteAllMessages(); } catch (Exception ex) { ex.ToString(); ReceiveUpdatesError(); } //pb.IsIndeterminate = false; }
private void Read_message() { //pb.IsIndeterminate = true; string storage = GetMessageStorage(); //txtmessage.Text = ""; try { // Read all SMS messages from the storage DecodedShortMessage[] messages = comm.ReadMessages(PhoneMessageStatus.All, "SM"); foreach (DecodedShortMessage msg in messages) { SmsPdu pdu = msg.Data; Database.DBMessages.DBInbox obj1 = new Database.DBMessages.DBInbox(); // Database.DBMessages.DBInboxLog obj2 = new Database.DBMessages.DBInboxLog(); if (pdu is SmsDeliverPdu) { // Received message SmsDeliverPdu data = (SmsDeliverPdu)pdu; String SenderNo = data.OriginatingAddress.ToString(); String SenderNo2 = SenderNo; SenderNo = SenderNo.Replace("+63", "0"); string sdate = "20" + data.SCTimestamp.Year + "-" + data.SCTimestamp.Month + "-" + data.SCTimestamp.Day + " " + data.SCTimestamp.Hour + ":" + data.SCTimestamp.Minute + ":" + data.SCTimestamp.Second; Class.Message m = new Class.Message(); m.watchers(SenderNo, data.UserDataText.ToString()); //m.VoteParsed(data.UserDataText.ToString()); obj1.Add(DateTime.Parse(sdate), SenderNo, data.UserDataText.ToString()); //if (m.MessageParsing(data.UserDataText.ToString(), SenderNo) == true) //{ // obj2.Add(DateTime.Parse(sdate), SenderNo, data.UserDataText.ToString(), "Valid"); //} //else //{ // obj1.Add(DateTime.Parse(sdate), SenderNo, data.UserDataText.ToString()); //} cntmessageReceived++; ReceiveUpdates(); } } // delete all messages DeleteAllMessages(); } catch (Exception e) { e.ToString(); ReceiveUpdatesError(); } //pb.IsIndeterminate = false; }
private void ReciveSMS() { string COMPortNumber = System.Configuration.ConfigurationManager.AppSettings["COMPortNumber"]; GsmCommMain comm = new GsmCommMain(Convert.ToInt32(COMPortNumber), 2400); try { comm.Open(); //string storage = GetMessageStorage(); string storage = PhoneStorageType.Sim; DecodedShortMessage[] messages = comm.ReadMessages(PhoneMessageStatus.ReceivedRead, storage); int count = messages.Length; //MessageBox.Show(count.ToString()); int i = 0; while (count != 0) { GsmComm.PduConverter.SmsPdu pdu = messages[i].Data; //GsmComm.PduConverter.SmsSubmitPdu data = (GsmComm.PduConverter.SmsSubmitPdu)pdu; string messageSMS = pdu.UserDataText; string phoneNumber = pdu.SmscAddress; //MessageBox.Show(i +" -- "+messageSMS); string URL = System.Configuration.ConfigurationManager.AppSettings["IncomingSMSUrl"] + phoneNumber + "&incommingmessage=" + messageSMS; HttpWebRequest req = (HttpWebRequest)WebRequest.Create(URL); HttpWebResponse res = (HttpWebResponse)req.GetResponse(); if (res.StatusCode == HttpStatusCode.OK) { comm.DeleteMessage(i, storage); //MessageBox.Show(i.ToString()); //WriteLogFile("SMS Sent", "SMS ID: " + Id + " |Status 1"); } else { WriteLogFile("SMS Not Recieved", "Error in reading the web page. Check internet connectivity"); } res.Close(); i++; count--; } } catch (Exception ex) { //Log WriteLogFile("Exception", ex.Message); } finally { comm.Close(); } }
public void ReadReceivedSMS() { if (comm.IsConnected() == true) { DecodedShortMessage[] messages = comm.ReadMessages (PhoneMessageStatus.All, PhoneStorageType.Sim); foreach (DecodedShortMessage message in messages) { ShowMessage(message.Data); //string Output = string.Format("Message status = {0}, Location = {1}/{2}, ActualLength = {3}, DataCodingScheme = {4}, ProtocolID = {5}, SmscAddress = {6}, SmscAddressType = {7}, TotalLength = {8}, UserData = {9}, UserDataLength = {10}, UserDataText = {11}", Convert.ToString(message.Status), Convert.ToString(message.Storage), Convert.ToString(message.Index), Convert.ToString(message.Data.ActualLength), Convert.ToString(message.Data.DataCodingScheme), Convert.ToString(message.Data.ProtocolID), Convert.ToString(message.Data.SmscAddress), Convert.ToString(message.Data.SmscAddressType), Convert.ToString(message.Data.TotalLength), Convert.ToString(message.Data.UserData), Convert.ToString(message.Data.UserDataLength), Convert.ToString(message.Data.UserDataText)); //Debug.WriteLine(Output); } using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\Public\Downloads\testworm.txt")) { file.WriteLine(_Output); } comm.Close(); } }
public static List <SmsPdu> ReadMessage() { List <SmsPdu> result = new List <SmsPdu>(); string Storage = GetMessageStorage(); DecodedShortMessage[] msgs = gsObj.ReadMessages(PhoneMessageStatus.ReceivedUnread, Storage); foreach (var obj in msgs) { result.Add(obj.Data); } return(result); }
private void comm_MessageReceived(object sender, MessageReceivedEventArgs e) { IMessageIndicationObject obj = e.IndicationObject; MemoryLocation loc = (MemoryLocation)obj; DecodedShortMessage[] messages; messages = comm.ReadMessages(PhoneMessageStatus.ReceivedUnread, loc.Storage); foreach (DecodedShortMessage message in messages) { SmsDeliverPdu data = new SmsDeliverPdu(); SmsPdu smsrec = message.Data; ShowMessage(smsrec, message.Index); } }
private void btnBacaSMS_Click(object sender, EventArgs e) { var messages = comm.ReadMessages(PhoneMessageStatus.All, PhoneStorageType.Sim); foreach (var message in messages) { var data = (SmsDeliverPdu)message.Data; var phoneNumber = data.OriginatingAddress; var msg = data.UserDataText; var tanggal = string.Format("{0:yyyy/MM/dd}", data.SCTimestamp.ToDateTime()); var jam = string.Format("{0:HH:mm:ss}", data.SCTimestamp.ToDateTime()); lstDaftarSMS.Items.Add(string.Format("{0}, {1}, {2}, {3}", tanggal, jam, phoneNumber, msg)); } }
private void Worker_DoWork(object sender, DoWorkEventArgs e) { while (gsmComm.IsConnected()) { var msgs = gsmComm.ReadMessages(PhoneMessageStatus.ReceivedUnread, "SM"); Thread.Sleep(512); foreach (var msg in msgs) { var sms = (SmsDeliverPdu)msg.Data; worker.ReportProgress(0, sms.UserDataText); gsmComm.DeleteMessage(msg.Index, msg.Storage); Thread.Sleep(512); } } Debug.WriteLine("Worker Thread Terminated"); }
}// Open Modem public String ReadSms() { DecodedShortMessage[] decsms = null; if (comm.IsOpen()) { do { try { Cursor.Current = Cursors.WaitCursor; decsms = comm.ReadMessages(PhoneMessageStatus.All, PhoneStorageType.Sim); Cursor.Current = Cursors.Default; } catch (Exception ex) { } Thread.Sleep(1); } while (decsms == null); } int len = decsms.Length; return("OK"); }
private static void ModemPollingLoop() { //Find a port where the modem is at _logger.Info("Searching for a modem..."); for (int i = 1; i <= 256; i++) { if (TestConnection($"COM{i}")) { break; } } if (_comm.IsOpen() && _comm.IsConnected()) { _logger.Info("Modem found"); _logger.Info("Setting modem settings..."); _comm.SelectCharacterSet("ucs2"); //Use UTF-16 encoding when getting data from modem //Hook debugging event if user wants to debug modem commands if (_config.GetValue("Debug/ModemCommands", false)) { _comm.LoglineAdded += comm_LoglineAdded; } try { _comm.SetSmscAddress(_config.GetValue("General/SMSCenter", "")); } catch (Exception) { _logger.Warn("Invalid SMS Center number or this feature is not supported by current modem"); } _logger.Info("Listening for incoming messages..."); while (_comm.IsOpen()) { if (_comm.IsConnected()) { try { //Get all messages saved on SIM DecodedShortMessage[] messages = _comm.ReadMessages(PhoneMessageStatus.All, PhoneStorageType.Sim); foreach (DecodedShortMessage message in messages) { SmsPdu pdu = message.Data; if (pdu is SmsDeliverPdu) // Check if it's SMS and not some kind of a service message { SmsDeliverPdu data = (SmsDeliverPdu)pdu; _logger.Debug($"Received message from {data.OriginatingAddress}: " + data.UserDataText + $" [TIMESTAMP: {data.SCTimestamp.ToSortableString()}]"); ChangePassword(data.OriginatingAddress, data.UserDataText); //Delete message so it won't be parsed again //and so we don't have a problem with free space on SIM //in the future _comm.DeleteMessage(message.Index, message.Storage); } else { _logger.Warn($"Received unknown message type: {pdu.GetType()}"); } } } catch (Exception ex) { _logger.Warn($"Cannot read messages: {ex.Message} ({ex.GetType()})"); break; } //Sleep 5 seconds between message checks Thread.Sleep(5000); } else { _logger.Error("Lost cellular network connection"); break; } } } else { _logger.Fatal("No modem found"); } //Close modem connection try { _comm.Close(); } catch (Exception) { } }
private void GetAllMessagesFromAllStorage() { try { if (Gsm.IsConnected() && Gsm.IsOpen()) { DecodedShortMessage[] SimMessages = Gsm.ReadMessages(PhoneMessageStatus.All, GetMessageStorage(MessageLocation.Sim)); Gsm.DeleteMessages(DeleteScope.All, GetMessageStorage(MessageLocation.Sim)); foreach (DecodedShortMessage message in SimMessages) { SmsPdu pdu = (SmsPdu)message.Data; if (pdu is SmsDeliverPdu) { // Received message SmsDeliverPdu data = (SmsDeliverPdu)pdu; string sim_msg = data.UserDataText; DateTime sim_receive_date = data.SCTimestamp.ToDateTime(); string sim_num = data.OriginatingAddress; //BSOB.SMS sms = new BSOB.SMS(); //sms.MESSAGE = sim_msg; //sms.PHONENO = sim_num; //sms.RECEIVEDDATE = sim_receive_date; //Order(sms); bsob.SMSQueuing(sim_msg, sim_num, sim_receive_date); //insert statement here } } DecodedShortMessage[] PhoneMessages = Gsm.ReadMessages(PhoneMessageStatus.All, GetMessageStorage(MessageLocation.Phone)); Gsm.DeleteMessages(DeleteScope.All, GetMessageStorage(MessageLocation.Phone)); foreach (DecodedShortMessage message in PhoneMessages) { SmsPdu pdu = (SmsPdu)message.Data; if (pdu is SmsDeliverPdu) { // Received message SmsDeliverPdu data = (SmsDeliverPdu)pdu; string phone_msg = data.UserDataText; DateTime phone_received_date = data.SCTimestamp.ToDateTime(); string phone_num = data.OriginatingAddress; //BSOB.SMS sms = new BSOB.SMS(); //sms.MESSAGE = phone_msg; //sms.PHONENO = phone_num; //sms.RECEIVEDDATE = phone_received_date; //Order(sms); bsob.SMSQueuing(phone_msg, phone_num, phone_received_date); //insert statement here } } } } catch (Exception ex) { LogError("GetAllMessagesFromAllStorage " + ex.Source + " " + ex.Message); if (Gsm.IsConnected() && Gsm.IsOpen()) { Gsm.Close(); } } }
void ReceiveMessages() { Log.Add(LogLevel.Verbose, "SMS", "Reading SMS messages from device..."); Stopwatch sw = new Stopwatch(); sw.Start(); DecodedShortMessage[] msgs = _gsm.ReadMessages(PhoneMessageStatus.All, "MT"); Log.Add(LogLevel.Verbose, "SMS", String.Format("{0} messages in storage", msgs.Length)); foreach (DecodedShortMessage msg in msgs) { string from = string.Empty; string text = string.Empty; DateTime received = DateTime.Now; bool _fullMessageReceived = false; SmsDeliverPdu pdu = (SmsDeliverPdu)msg.Data; if (SmartMessageDecoder.IsPartOfConcatMessage(pdu)) { IConcatenationInfo info = SmartMessageDecoder.GetConcatenationInfo(pdu); Log.Add(LogLevel.Debug, "SMS", string.Format("Received multi-part message {0}: {1}/{2}", info.ReferenceNumber, info.CurrentNumber, info.TotalMessages)); if (_concatPdus.ContainsKey(info.ReferenceNumber)) { _concatPdus[info.ReferenceNumber].Add(pdu); } else { _concatPdus.Add(info.ReferenceNumber, new List <SmsPdu>() { pdu }); } if (SmartMessageDecoder.AreAllConcatPartsPresent(_concatPdus[info.ReferenceNumber])) { _fullMessageReceived = true; from = pdu.OriginatingAddress; received = pdu.SCTimestamp.ToDateTime(); text = SmartMessageDecoder.CombineConcatMessageText(_concatPdus[info.ReferenceNumber]); } } else { Log.Add(LogLevel.Debug, "SMS", "Received single-part SMS."); _fullMessageReceived = true; from = String.Format("{0}", pdu.OriginatingAddress); received = pdu.SCTimestamp.ToDateTime(); text = pdu.UserDataText; } if (_fullMessageReceived) { Log.Add(LogLevel.Info, "SMS", String.Format("Incoming SMS from {0}", from)); if (NormalizeNumbers) { from = NormalizeNumber(from); } if (_resolveNumbers) { from = ResolveNameByNumber(from); } Log.Add(LogLevel.Debug, "SMS", String.Format("Message from {0} at {1}: {2}", from, received, text)); if (OnMessageReceived != null) { OnMessageReceived(this, new Message(from, received, text)); } } _gsm.DeleteMessage(msg.Index, msg.Storage); } sw.Stop(); Log.Add(LogLevel.Verbose, "SMS", String.Format("Reading took {0}ms", sw.Elapsed.TotalMilliseconds)); }