namespace WindowsFormsApplication1 { public partial class Form5 : Form { public Form5() { InitializeComponent(); } private void Form5_Load(object sender, EventArgs e) { _serialPort = new SerialPort("COM4", 9600, Parity.None, 8, StopBits.One); //_serialPort.Open(); _response = string.Empty; _serialPort.RtsEnable = true; _serialPort.DataReceived += SerialPortDataReceived; } private static SerialPort _serialPort; private string _response; void SerialPortDataReceived(object sender, SerialDataReceivedEventArgs e) { if (e.EventType == SerialData.Chars) { _response += _serialPort.ReadLine(); } } public bool IsReady() { try { if (!_serialPort.IsOpen) _serialPort.Open(); _serialPort.Write("AT\r"); Thread.Sleep(3000); } catch { return false; } return _response.Contains("OK"); } public static bool SendSms(string phoneNumber, string message) { try { if (!_serialPort.IsOpen) _serialPort.Open(); // Send _serialPort.Write("AT+CMGF=1\r"); _serialPort.Write("AT+CMGS=\"" + phoneNumber + "\"\r\n"); _serialPort.Write(message + "\x1A"); return true; } catch { return false; } } private void button1_Click(object sender, EventArgs e) { SendSms(textBox1 .Text , textBox2 .Text ); } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)