2012-12-01 100 views
3

我正在開發一個項目女巫,其中包括將AT命令發送到手機調制解調器並接收其結果代碼。 我已經成功地發送了at命令,但我無法弄清楚的是從命令中讀取結果代碼,就像發送「at」時返回「OK」一樣,如果發送「at + cbc」,它將返回電池狀態。 發送命令後,我嘗試使用Read,ReadLine和ReadExisting,但它不起作用,我也嘗試創建接收數據的事件,並使用這些功能,但它也沒有工作。 這是我的源代碼,我希望你能給我一些幫助。閱讀AT命令結果代碼

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.IO.Ports; 

namespace WindowsFormsApplication1 
{ 
    public partial class check : Form 
    { 
     SerialPort sp; 

     public check() 
     { 
      InitializeComponent(); 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      sp = new SerialPort(); 
      sp.PortName = "COM3"; 
      sp.BaudRate = 9600; 
      sp.Parity = Parity.None; 
      sp.DataBits = 8; 
      sp.StopBits = StopBits.One; 
      sp.ReadTimeout = 3000; 
      sp.WriteTimeout = 3000; 
      //sp.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived); 
     } 

     private void btn1_Click(object sender, EventArgs e) 
     { 
      try 
      { 
       if(!sp.IsOpen) 
       { 
        sp.Open(); 
       } 
       sp.WriteLine(tbCmd+"\r\n"); 
       System.Threading.Thread.Sleep(2000); 
       tbStatus.Text = sp.ReadLine();  
      } 
      catch(Exception ex) 
      { 
       tbStatus.Text = "Error!\r\n" + ex.ToString(); 
      } 
     } 

     //void sp_DataReceived(object sender, SerialDataReceivedEventArgs e) 
     //{ 
     // tbStatus.Text = sp.ReadExisting(); 
     //} 
    } 
} 
+0

是什麼tbCmd作品???? – motevalizadeh

+0

@motevallizadeh:可能是包含應通過COM端口發送的AT命令的TextBox ... –

回答

0
sp.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived); 

    void sp_DataReceived(object sender, SerialDataReceivedEventArgs e) 
    { 
     if(e.EventType != SerialData.Chars) 
     { 
      tbStatus.Text = "IGNORED: " + e.EventType.ToString(); 
      return; 
     } 
     try 
     { 
      int nBytesToRead = sp.BytesToRead; 
      char[] receivedData = new char[nBytesToRead]; 
      sp.Read(receivedData, 0, nBytesToRead); 
      tbStatus.Text = "RECEIVED: " + new string(receivedData); 
     } 
     catch (Exception ex) 
     { 
      tbStatus.Text = "ERROR: " + ex.Message; 
     } 
    } 
0

我在諾基亞論壇讀到諾基亞手機60系列不支持讀取短信命令,這在命令不是諾基亞手機