在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
因为项目的需要,简简单单的做了个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.IO.Ports; namespace testPhoneInfo_Csharp { public partial class frmMain : Form { public frmMain() { InitializeComponent(); Form.CheckForIllegalCrossThreadCalls = false; } private void frmMain_Load(object sender, EventArgs e) { sp.Open(); } private void btnPhone_Click(object sender, EventArgs e) { sp.WriteLine("ATD"+txtPhoneNum.Text .Trim ()+";"+"\r\n"); } private void btnSendMsg_Click(object sender, EventArgs e) { sp.WriteLine("AT+CMGF=0"+"\r\n");//将短消息格式设置为PDU模式 System.Threading.Thread.Sleep(300); sp.WriteLine("AT+CSCA=" + "+8613010314500" + "\r\n"); System.Threading.Thread.Sleep(300); sp.WriteLine("AT+CMGS=0" + this.getLenght(txtMsg.Text.Trim()) + "\r\n"); System.Threading.Thread.Sleep(500); sp.WriteLine("0011000D91" + this.reverserNumber(txtPhoneNum.Text.Trim()) + "000801" + this.contentEncoding(txtMsg.Text.Trim()) + Convert.ToChar(26).ToString()); } //获取短信内容的字节数 private string getLenght(string txt) { int i = 0; string s = ""; i = txt.Length * 2; i += 15; s = i.ToString(); return s; } //将手机号码转换为内存编码 private string reverserNumber(string phone) { string str = ""; //检查手机号码是否按照标准格式写,如果不是则补上 if (phone.Substring(0, 2) != "86") { phone = string.Format("86{0}", phone); } char[] c = this.getChar(phone); for (int i = 0; i <= c.Length - 2; i += 2) { str += c[i + 1].ToString() + c[i].ToString(); } return str; } //汉字解码为16进制 private string contentEncoding(string content) { Encoding encodingUTF = System.Text.Encoding.BigEndianUnicode; string s = ""; byte[] encodeByte = encodingUTF.GetBytes(content); for (int i = 0; i <= encodeByte.Length - 1;i++ ) { s += BitConverter.ToString(encodeByte, i, 1); } s=string.Format ("{0:X2}{1}",s.Length /2,s); return s; } private char[] getChar(string phone) { if (phone.Length % 2 == 0) { return Convert.ToString(phone).ToCharArray(); } else { return Convert.ToString(phone + "F").ToCharArray(); } } private void sp_DataReceived(object sender, SerialDataReceivedEventArgs e) { txtReturnData.Text += sp.ReadExisting()+"\r\n"; } } } 其实都很简单,大家一看都会了! |
请发表评论