华电新星 发表于 2010-11-8 19:40:30

基于at指令串口通讯程序

namespace conectAndSend
{
    public partial class Form1 : Form
    {
      public Form1()
      {
            InitializeComponent();
      }
      StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
      ConnectAndSend connectandsend = new ConnectAndSend();
         SerialPort _serialPort;
      static bool _continue;
      static Thread thread1;
      private void button1_Click(object sender, EventArgs e)
      {
            OpenSerialport();
             _continue = true;
            thread1 = new Thread(creat);
            thread1.Start();
            
         
         
      }
      public void OpenSerialport()
      {
            CheckForIllegalCrossThreadCalls = false;
            _serialPort = new SerialPort();
            _serialPort.PortName = "COM1";
            _serialPort.BaudRate = 9600;
            _serialPort.Parity = Parity.None;
            _serialPort.DataBits = 8;
            _serialPort.StopBits = StopBits.One;
            _serialPort.Handshake = Handshake.None;
            _serialPort.RtsEnable = true;
            _serialPort.NewLine = "\r\n";
            _serialPort.ReadTimeout = 30000;
            _serialPort.WriteTimeout = 500;
            _serialPort.Open();
      }
      public void creat()
      {
            while (_continue)
            {
                try
                {
                  _serialPort.WriteLine("ate");
                  listBox1.Items.Add("1.ate");
                  string an0 = _serialPort.ReadLine();

                  //listBox1.Items.Add(an0);
                  string an1 = _serialPort.ReadLine();
                  listBox1.Items.Add("2." + an1);
                  if (stringComparer.Equals("OK", an1))
                  {
                  mylable:
                        _serialPort.WriteLine("atd15811482417");
                        listBox1.Items.Add("1.atd015811482417");
                        //_serialPort.WriteLine("atd15810507416");
                        //listBox1.Items.Add("atd15810507416");
                        // Thread.Sleep(10000);
                        string an2 = _serialPort.ReadLine();
                        while (an2 == "")
                        {
                            Thread.Sleep(1000);
                            an2 = _serialPort.ReadLine();
                        }
                        listBox1.Items.Add("2." + an2);
                        if (stringComparer.Equals("CONNECT 9600", an2))
                        {
                            listBox1.Items.Add("1.已连接开始通话!");
                            _continue = false;

                        }
                        else if (stringComparer.Equals("busy", an2))
                        {
                            MessageBox.Show("占线!稍后再拨。");
                            _serialPort.WriteLine("ath");
                            Thread.Sleep(10000);
                            goto mylable;
                        }
                        else if (stringComparer.Equals("no answer", an2))
                        {
                            MessageBox.Show("无人接听,稍后再拨。");
                            _serialPort.WriteLine("ath");
                            Thread.Sleep(10000);
                            goto mylable;
                        }
                        else if (stringComparer.Equals("no carrier", an2))
                        {
                            MessageBox.Show("不支持此项业务!");
                            _serialPort.WriteLine("ath");
                            Thread.Sleep(10000);
                            goto mylable;
                        }
                        else if (stringComparer.Equals("ERROR", an2))
                        {
                            MessageBox.Show("手机卡未安装好!");
                            _serialPort.WriteLine("ath");
                           _continue = false;
                        }
                  }
                  else
                  {
                        listBox1.Items.Add("设备未安装好!");
                        return;
                  }
                }
                catch (TimeoutException)
                {
                  listBox1.Items.Add("通讯装置未连接好或欠费停机");
                }
            }
      }
      private void button2_Click(object sender, EventArgs e)
      {
         
            string str = " Provider=Microsoft.Jet.OLEDB.4.0;Data Source=G:\\Time-angle.mdb";
            OleDbConnection connect = new OleDbConnection(str);
            string sql_data1 = "select * from datatime";
            connect.Open();
            OleDbDataAdapter adapter_data1 = new OleDbDataAdapter(sql_data1, connect);
            DataSet dataset = new DataSet();
            OleDbCommandBuilder builder_data1 = new OleDbCommandBuilder(adapter_data1);
            adapter_data1.Fill(dataset, "datatime");
            foreach (DataRow row1 in dataset.Tables["datatime"].Rows)
            {
                connectandsend.WriteData(row1);
                _serialPort.Write(connectandsend.bytes1, 0, connectandsend.bytes1.Length);
                for (int k = 0; k < connectandsend.bytes1.Length; k++)
               
                  listBox1.Items.Add(connectandsend.bytes1.ToString());
                  listBox1.Items.Add(row1.ToString());
                  listBox1.Items.Add(row1.ToString());
                  listBox1.Items.Add(row1.ToString());
                  listBox1.Items.Add(row1.ToString());
               
               
            }
            connect.Close();
         
      }
      private void button3_Click(object sender, EventArgs e)
      {
            thread1.Join();
            for (; ; )
            {
                _serialPort.WriteLine("+++");
                DateTime t1 = DateTime.Now;
                for (; ; )
                {
                  DateTime t2 = DateTime.Now;
                  TimeSpan tt = t2 - t1;
                  double tt1 = tt.Seconds;
                  if (tt1 > 3) break;
                }
                _serialPort.WriteLine("ath");
                try
                {
                  string message3 = _serialPort.ReadLine();
                  if (String.Compare(message3, "OK") == 0) return;
                }
                catch (TimeoutException) { };
            }
            
            _serialPort.Close();
         // listBox1.Items.Clear();
      }
    }
}

l_2011 发表于 2011-5-23 21:17:12

谢谢,我刚接触AT指令,得到不少启发呀.
页: [1]
查看完整版本: 基于at指令串口通讯程序

招聘斑竹