C# 读取AT命令结果代码
我正在做一个项目,其中包括向手机调制解调器发送C# 读取AT命令结果代码,c#,command,at-command,C#,Command,At Command,我正在做一个项目,其中包括向手机调制解调器发送AT命令并接收结果代码。 我已经成功地发送了at命令,但我无法理解的是从命令中读取结果代码,例如当您发送“at”时,它返回“OK”,如果您发送“at+cbc”,它返回电池状态。 在发送命令后,我尝试使用Read、ReadLine和ReadExisting,但不起作用,我还尝试为接收到的数据创建事件并使用这些函数,但也不起作用。 这是我的源代码,希望你能给我一些帮助 using System; using System.Collections.Gene
AT
命令并接收结果代码。
我已经成功地发送了at命令,但我无法理解的是从命令中读取结果代码,例如当您发送“at”时,它返回“OK”,如果您发送“at+cbc”,它返回电池状态。
在发送命令后,我尝试使用Read、ReadLine和ReadExisting,但不起作用,我还尝试为接收到的数据创建事件并使用这些函数,但也不起作用。
这是我的源代码,希望你能给我一些帮助
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();
//}
}
}
我在诺基亚论坛上看到,诺基亚60系列手机不支持阅读短信命令,而此at命令在诺基亚手机上不起作用@motevallizadeh:可能是包含应通过COM端口发送的at命令的文本框。。。
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;
}
}