首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

小弟我用C#写了个上位机,经测试串口是有用的,但是连到短信猫上就不行了,短信猫一直没回信,但是短信猫用 超级终端测试完全正确

2012-03-02 
我用C#写了个上位机,经测试串口是有用的,但是连到短信猫上就不行了,短信猫一直没回信,但是短信猫用 超级终

我用C#写了个上位机,经测试串口是有用的,但是连到短信猫上就不行了,短信猫一直没回信,但是短信猫用 超级终端测试完全正确
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 Uart
{
  public partial class Uart : Form
  {
  public Uart()
  {
  InitializeComponent();
  }
  //声明串口
  public SerialPort sp = new SerialPort();
  //定义委托
  delegate void HandleInterfaceUpdateDelegate(string text);
  //实例化委托
  HandleInterfaceUpdateDelegate interfaceUpdateHandle = null;
  //开始初始化
  private void Uart_Load(object sender, EventArgs e)
  {
  this.label_Uart_Info.Text = "";
  //初始化tabcontrol
  this.tabControl1.SelectedIndex = 1;
  //initialize combobox_enter
  this.cbxTransfer.SelectedIndex = 0;
  interfaceUpdateHandle = new HandleInterfaceUpdateDelegate(Updata_Text_Rec);
  }
  private void Updata_Text_Rec(string text)
  {
  richTextBox_Rec.Text += text;
  }
  public int sendCount = 0;
  public int recCount = 0;
  //打开串口
  private void btn_Link_Uart_Click(object sender, EventArgs e)
  {
  sp.PortName = "COM3";
  sp.BaudRate = 9600;
  sp.DataBits = 8;
  sp.Parity = Parity.None;
  sp.StopBits = StopBits.One;
  sp.ReceivedBytesThreshold = 1;
  sp.Encoding = Encoding.GetEncoding("utf-8");
  try
  {
  sp.Open();
  }
  catch (System.IO.IOException ex)
  {
  MessageBox.Show(ex.ToString());
  }
  sp.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(this.sp_DataReceive);
  label_Uart_Info.Text = sp.PortName + ',' + sp.BaudRate + ',' + sp.DataBits + ',' + sp.Parity + ',' + sp.StopBits;
  if (sp.IsOpen == true)
  {
  MessageBox.Show("串口连接成功");
  }
  else
  {
  MessageBox.Show("串口连接失败");
  }
  }
  //关闭串口
  private void btn_Break_Uart_Click(object sender, EventArgs e)
  {
  sp.Close();
  richTextBox_Rec.Text = "";
  richTextBox_Send.Text = "";
  label_Uart_Info.Text = "未连接";
  if (sp.IsOpen == false)
  {
  MessageBox.Show("串口正常关闭");
  this.lbSendCount.Text = "发送计数";
  this.lbRecCount.Text = "接收计数";
  }
  else
  {
  MessageBox.Show("串口未能关闭");
  }
  }
  //DataReceived事件委托方法
  private void sp_DataReceive(object sender, EventArgs e)
  {
  byte[] databuf = new byte[sp.BytesToRead];
  sp.Read(databuf, 0, sp.BytesToRead);
  this.recCount += databuf.Length;
  //this.lbRecCount.Text = string.Format("接收到{0:X}Byte", this.recCount);


  if (this.checkHex.Checked == true)
  {
  string readbuf = null;
  //十六进制格式化数据
  for (int i = 0; i < databuf.Length; i++)
  {
  string s = String.Format("{0:X}", Convert.ToInt32(databuf[i]));
  if (s.Length > 1)
  {
  readbuf += s + " ";
  }
  else
  {
  readbuf += "0" + s + " ";
  }
  }
  this.Invoke(interfaceUpdateHandle, readbuf);
  }
  else
  {
  this.Invoke(interfaceUpdateHandle, Encoding.UTF8.GetString(databuf));
  }
  }
  //断开连接

  private void textBox_Send_Period_TextChanged(object sender, EventArgs e)
  {
  if (textBox_Send_Period.Text == null || textBox_Send_Period.Text == "")
  {
  timer_Uart_Send.Interval = 1000;
  }
  else
  {
  timer_Uart_Send.Interval = Convert.ToInt32(textBox_Send_Period.Text);
  }
  }

  private void timer_Uart_Send_Tick(object sender, EventArgs e)
  {
  if (cbxAutoSend.Checked == true)
  {
  if (sp.IsOpen == true)
  {
  sp.Write(richTextBox_Send.Text);
  }
  else
  {
  MessageBox.Show("串口未打开");
  }
  }
  }

  private void btnSendByHand_Click(object sender, EventArgs e)
  {
  if (cbxAutoSend.Checked == false)
  {
  sp.Write(richTextBox_Send.Text);
  }
  }

  private void Uart_FormClosing(object sender, FormClosingEventArgs e)
  {
  if (sp.IsOpen == true)
  {
  sp.Close();
  }
  }
  //发送数据
  private void richTextBox_Send_TextChanged(object sender, EventArgs e)
  {
  if (richTextBox_Send.Text != "")
  {
  string str = richTextBox_Send.Text;
  str = str.Substring(str.Length - 1, 1);
  sendCount += str.Length;
  lbSendCount.Text = string.Format("发送{0:D}Byte", sendCount);
  if (sp.IsOpen == true)
  {
  if(str.Equals("\n"))
  {
  if(cbxTransfer.SelectedItem.ToString().Equals("CR"))
  sp.Write("\r");
  else if(cbxTransfer.SelectedItem.ToString().Equals("CR+LF"))
  sp.Write("\r\n");
  else if(cbxTransfer.SelectedItem.ToString().Equals("LF"))
  sp.Write("\n");
  else
  sp.Write("\r");
  }
  else


  {
  sp.Write(str);
  }
  }
  }
  }

  private void button1_Click(object sender, EventArgs e)
  {
  sp.Write("AT\r");
  sendCount += ("AT\r").Length;
  lbSendCount.Text = string.Format("发送{0:D}Byte", sendCount);
  }  
  }
}


[解决办法]
连接短信猫后,有没有用串口调试助手监视收发的数据。看数据在这种情况下是否能发送和接收。如果发不出,接不到,检查一下发送的数据格式是否正确。
[解决办法]
短信猫不要采用二进制方式发送和接收数据,就是文本方式
连接好以后,先发送一些“AT\r”,看看返回是不是OK
[解决办法]
1) 单 发 英文 可以吗? 
2) 你的短信毛支持直接发送中文码 
3)发中文 的话如果不用第三方 lib 的好像要用到 PDU 

同 13 楼


[解决办法]
估计是楼主的编码写错了~先看看AT指令再写短信猫的程序吧,之前的我也有碰到过这个问题,是我的AT指令写的有问题,楼主好好检查下吧~看看是不是指令的问题

热点排行