以前在一个硬件设备上用C#开发的测试GPRS的Demo,希望对你有用。using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Threading;using System.IO;using System.Xml;using System.Reflection;namespace TestGPRS{ public partial class Form1 : Form { public Form1() { InitializeComponent(); for(int i = 1; i < 9; i++) { CoBComm.Items.Add("COM" + i.ToString()); } CoBComm.SelectedIndex = 3; CoBBaudRate.Items.Add("9600"); CoBBaudRate.Items.Add("115200"); CoBBaudRate.SelectedIndex = 1; CoBCommand.Items.Add("查找模块 AT"); CoBCommand.Items.Add("开始连接 AT+CIPSTART=/"TCP/",/"221.219.0.0/",/"2020/""); CoBCommand.Items.Add("断开连接 AT+CIPSHUT"); CoBCommand.Items.Add("发送数据 AT+CIPSEND/r123456789" + Convert.ToChar(26)); CoBCommand.Items.Add("覆盖网络 AT+CGATT?"); CoBCommand.Items.Add("关闭连接 AT+CIPCLOSE"); CoBCommand.Items.Add("注册网络 AT+CREG?"); CoBCommand.Items.Add("内网地址 AT+CIFSR"); CoBCommand.Items.Add("拨打电话 ATD13704755747;"); CoBCommand.Items.Add("挂断电话 ATH"); CoBCommand.Items.Add("设置模式 AT+CGCLASS=/"B/""); CoBCommand.Items.Add("设置APNA AT+CGDCONT=1,/"IP/",/"CMNET/""); CoBCommand.Items.Add("信号强度 AT+CSQ"); CoBCommand.Items.Add("激活模块 AT+CGACT=1,1"); CoBCommand.Items.Add("注册网络 AT+CGREG?"); CoBCommand.Items.Add("重复命令 A/"); CoBCommand.Items.Add("保存设置 AT&W"); CoBCommand.Items.Add("改波特率 AT+IPR=115200"); CoBCommand.Items.Add("查看版本 ATI"); CoBCommand.SelectedIndex = 0; TeBCommand.Text = "AT"; if(File.Exists(AppPath + "Config.xml")) { LoadConfig(); } } string State { get { return TeBInfor.Text; } set { TeBInfor.Text += value + "/r/n"; TeBInfor.ScrollToCaret(); } } private void BtnOpen_Click(object sender, EventArgs e) { try { SP.Close(); SP.PortName = CoBComm.Text; SP.ReadTimeout = 2000; SP.WriteTimeout = 2000; SP.BaudRate = int.Parse(CoBBaudRate.Text); SP.Open(); State = "成功打开串口 [" + CoBComm.Text + "]"; } catch { State = "不能打开串口 [" + CoBComm.Text + "]"; } } private void BtnSend_Click(object sender, EventArgs e) { if (!SP.IsOpen) { State = "请先打开一个串口"; return; } if(TeBCommand.Text.Trim() == "") return; if (ChBClear.Checked) { TeBInfor.Text = ""; } string StrCommand = TeBCommand.Text.Trim(); if (ChBKey.Checked) { StrCommand += Convert.ToChar(26); } SendCommand(StrCommand); } void SendCommand(string StrCommand) { try { byte[] bSend = new byte[StrCommand.Length + 1]; for (int i = 0; i < StrCommand.Length; i++) { bSend = Convert.ToByte(StrCommand); } bSend[bSend.Length - 1] = Convert.ToByte('/r'); SP.Write(bSend, 0, bSend.Length); State = "命令 " + Encoding.Default.GetString(bSend, 0, bSend.Length) + " 发送完成..."; Thread.Sleep(200); if (SP.BytesToRead == 0) { State = "没有收到返回的信息"; return; } byte[] bData = new byte[256]; State = "收到以下信息"; int iCount = SP.Read(bData, 0, 255); State = Encoding.Default.GetString(bData, 0, iCount); } catch { } return; } private void Form1_Closed(object sender, EventArgs e) { SP.Close(); } private void BtnClear_Click(object sender, EventArgs e) { TeBInfor.Text = ""; } private void CoBCommand_SelectedIndexChanged(object sender, EventArgs e) { string StrData = CoBCommand.Items[CoBCommand.SelectedIndex].ToString(); int iPos = StrData.IndexOf(' '); if (iPos == 0) return; string StrCommand = StrData.Substring(iPos + 1, StrData.Length - iPos - 1); TeBCommand.Text = StrCommand; BtnSend.Focus(); } private void BtnSave_Click(object sender, EventArgs e) { string StrFile = AppPath + "Config.xml"; XmlTextWriter xw = new XmlTextWriter(StrFile, Encoding.UTF8); xw.WriteStartDocument(); xw.WriteStartElement("NewDataSet"); xw.WriteStartElement("Data"); xw.WriteElementString("COM", CoBComm.Text); xw.WriteElementString("BaudRate", CoBBaudRate.Text); xw.WriteElementString("COMMANDCOUNT", CoBCommand.Items.Count.ToString()); for (int i = 0; i < CoBCommand.Items.Count; i++) { if (i == CoBCommand.SelectedIndex) { string StrTemp = CoBCommand.Items.ToString(); int iPos = StrTemp.IndexOf(" "); if(iPos > 0) { StrTemp = StrTemp.Substring(0, iPos + 1); xw.WriteElementString("CoBCommand" + i.ToString(), StrTemp + TeBCommand.Text); } else { xw.WriteElementString("CoBCommand" + i.ToString(), CoBCommand.Items.ToString()); } } else { xw.WriteElementString("CoBCommand" + i.ToString(), CoBCommand.Items.ToString()); } } xw.WriteEndElement(); xw.WriteEndElement(); xw.WriteEndDocument(); xw.Flush(); xw.Close(); LoadConfig(); } private bool LoadConfig() { bool Result = false; try { string StrFile = AppPath + "Config.xml"; DataSet DSTemp = new DataSet(); DSTemp.ReadXml(StrFile); CoBComm.Text = DSTemp.Tables[0].Rows[0]["COM"].ToString(); CoBBaudRate.Text = DSTemp.Tables[0].Rows[0]["BaudRate"].ToString(); int index = CoBCommand.SelectedIndex; CoBCommand.Items.Clear(); int iCount = int.Parse(DSTemp.Tables[0].Rows[0]["COMMANDCOUNT"].ToString()); for (int i = 0; i < iCount; i++) CoBCommand.Items.Add(DSTemp.Tables[0].Rows[0]["CoBCommand" + i.ToString()].ToString()); CoBCommand.SelectedIndex = index; DSTemp.Dispose(); DSTemp = null; Result = true; } catch { } return Result; } public string AppPath { get { if (Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase).Equals("//")) { return "//"; } else { return Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase) + "//"; } } } }}