static private void CallbackMsg(string data) { try { if (data.IndexOf("CMGL", 0) == -1) { Console.WriteLine("None msg"); return; } int i = data.IndexOf("\n", 0) + 1; i = data.IndexOf("\n", i) + 1; data = data.Substring(i, data.IndexOf("\n", i) - (i + 1)); SMS sms = new SMS(); SMS.Fetch(sms, ref data); needCheck = false; if (isControll) { Console.WriteLine(sms.PhoneNumber + " - sending msg"); SendMsgToServer("number", sms.PhoneNumber); } else { Console.WriteLine(sms.Message + " - sending msg"); SendMsgToServer("code", sms.Message); } } catch (Exception e) { Console.WriteLine(e); } }
private void ReadData(object sender, SerialDataReceivedEventArgs e) { SerialPort sp = (SerialPort)sender; string data = sp.ReadExisting(); //Console.WriteLine(data); if (data.IndexOf("CMGL:", 0) != -1) { int i = 0, end; while ((i = data.IndexOf("CMGL:", i)) != -1) { if (data.IndexOf("\n", i) == -1) { break; } i = data.IndexOf("\n", i) + 1; end = (data.IndexOf("\n", i) == -1 ? data.Length : data.IndexOf("\n", i)) - (i + 1); string msg = data.Substring(i, end); try { SMS sms = new SMS(); SMS.Fetch(sms, ref msg); Console.WriteLine(sms.PhoneNumber + " - " + sms.Message); } catch (Exception ee) { Console.WriteLine("Broken msg!"); } } } if (data.IndexOf("+CMGL", 0) != -1) { Console.WriteLine("Msg:"); } if (data.IndexOf("CIMI", 0) != -1) { int i = data.IndexOf("\n", 0) + 1; data = data.Substring(i, data.IndexOf("\n", i) - (i + 1)); Console.WriteLine(data + " - CIMI\n"); } }