This is the code I have tried.
- SerialPort _serialPort = new SerialPort("COM5", 9600);
- _serialPort.Parity = Parity.None;
- _serialPort.DataBits = 8;
- _serialPort.Handshake = Handshake.RequestToSend;
- _serialPort.DtrEnable = true;
- _serialPort.RtsEnable = true;
- _serialPort.NewLine = Environment.NewLine;
- _serialPort.Open();
- _serialPort.Write("AT" + System.Environment.NewLine);
- Thread.Sleep(1000);
- _serialPort.WriteLine("AT+CMGF=1" + System.Environment.NewLine);
- Thread.Sleep(1000);
- _serialPort.WriteLine("AT+CMGL=\"ALL\"\r" + System.Environment.NewLine);
- Thread.Sleep(3000);
- string response = _serialPort.ReadExisting();
- if (response.EndsWith("\r\nOK\r\n"))
- {
- Console.WriteLine(response);
- }
- else
- {
- Console.WriteLine(response);
- }
- Console.ReadLine();
- String input = "Data Source=10.31.8.107;Initial Catalog=WR_CWM_PR_Barrel_Bearing;User ID=sa;Password=server#qtr#15";
- Regex r = new Regex(@"\+CMGL: (\d+),""(.+)"",""(.+)"",(.*),""(.+)""\r\n(.+)\r\n");
- Match m = r.Match(_serialPort.ReadExisting());
- while (m.Success)
- {
- string a = m.Groups[1].Value;
- string b = m.Groups[2].Value;
- string c = m.Groups[3].Value;
- string d = m.Groups[4].Value;
- string s = m.Groups[5].Value;
- string f = m.Groups[6].Value;
- ListViewItem Item = new ListViewItem(new string[] { a, b, c, d, s, f });
- listView1.Items.Add(Item);
- m = m.NextMatch();
- }