您好,欢迎光临本网站![请登录][注册会员]  
文件名称: 串口助手源码
  所属分类: C#
  开发工具:
  文件大小: 291kb
  下载次数: 0
  上传时间: 2017-10-20
  提 供 者: liut****
 详细说明: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO.Ports; namespace UART1 { pu blic partial class Form1 : Form { public Form1() { InitializeComponent(); COMNUM.SelectedIndex = 2; BaudRate.SelectedIndex = 3; DataBit.SelectedIndex = 3; StopBit.SelectedIndex = 1; CheckBit.SelectedIndex = 0; CharDisp.Checked = true; SendChar.Checked = true; CloseComm.Enabled = false; DataSend.Enabled = false; } private void OpenComm_Click(object sender, EventArgs e) { serialPort1.Close(); serialPort1.PortName = COMNUM.Text; serialPort1.BaudRate = Convert.ToInt32(BaudRate.Text); serialPort1.DataBits = Convert.ToInt32(DataBit.Text); int SPnum = 4; SPnum = StopBit.SelectedIndex; switch (SPnum) { case 0: serialPort1.StopBits = StopBits.None; break; case 1: serialPort1.StopBits = StopBits.One; break; case 2: serialPort1.StopBits = StopBits.OnePointFive; break; case 3: serialPort1.StopBits = StopBits.Two; break; default: break; } SPnum = CheckBit.SelectedIndex; switch (SPnum) { case 0: serialPort1.Parity = Parity.None; break; case 1: serialPort1.Parity = Parity.Odd; break; case 2: serialPort1.Parity = Parity.Even; break; default: break; } /***************异常处理!****************/ try { serialPort1.Open(); } catch (Exception ex) { MessageBox.Show("错误:"+ex.Message); } /*****************************************/ if (serialPort1.IsOpen) { CloseComm.Enabled = true; DataSend.Enabled = true; OpenComm.Enabled = false; COMNUM.Enabled = false; BaudRate.Enabled = false; DataBit.Enabled = false; StopBit.Enabled = false; CheckBit.Enabled = false; } } private void CloseComm_Click(object sender, EventArgs e) { try { serialPort1.Close(); CloseComm.Enabled = false; DataSend.Enabled = false; OpenComm.Enabled = true; COMNUM.Enabled = true; BaudRate.Enabled = true; DataBit.Enabled = true; StopBit.Enabled = true; CheckBit.Enabled = true; } catch (Exception ex) { MessageBox.Show("错误:" + ex.Message); CloseComm.Enabled = true; DataSend.Enabled = true; OpenComm.Enabled = false; COMNUM.Enabled = false; BaudRate.Enabled = false; DataBit.Enabled = false; StopBit.Enabled = false; CheckBit.Enabled = false; } } private void DataSend_Click(object sender, EventArgs e) { string InputString = SendBox.Text; /**********发送byte[]实例*********/ //byte[] SendBytes = new byte[]{0xff,0xe0,0xd0,0xc0,0xb0,0xa0,0x90,0x80,0x70,0x60}; //int DatasLength = SendBytes.Length; //serialPort1.Write(SendBytes, 0, DatasLength); /*******************************/ if (SendChar.Checked == true) //以字符形式发送,直接发送字符串 { try { char[] SendChars = InputString.ToCharArray(); int CharsLength = SendChars.Length; serialPort1.Encoding = System.Text.Encoding.GetEncoding("GB2312"); //解决中午乱码问题,国标2312编码格式 serialPort1.Write(SendChars, 0, CharsLength); } catch (Exception ex) { MessageBox.Show("错误:" + ex.Message); } } else { if (SendHEX.Checked == true) //以HEX形式发送,输入范围:0~255,即0~FF。 { try { byte SendByte = Convert.ToByte(InputString); try { serialPort1.Write(String.Format("{0:X}",SendByte)); } catch (Exception ex) { MessageBox.Show("错误:" + ex.Message); } } catch (Exception) { MessageBox.Show("请输入0~255,即0x00~0xFF!"); } } } } private void SerialPortDataReceive(object sender, SerialDataReceivedEventArgs e) { int ReceiveNums = serialPort1.BytesToRead; if (HEXDisp.Checked == true) { for (int i = 0; i < ReceiveNums; i++) { ReceiveBox.AppendText(String.Format("{0:X2} ", serialPort1.ReadByte())); } } else { if (CharDisp.Checked == true) { /* for (int i = 0; i < ReceiveNums; i++) { ReceiveBox.AppendText(String.Format("{0:X2}", Convert.ToChar(serialPort1.ReadByte()))); }*/ serialPort1.Encoding = System.Text.Encoding.GetEncoding("GB2312");//解决中午乱码问题,国标2312编码格式 ReceiveBox.AppendText(serialPort1.ReadExisting()); } } } private void ClearSend_Click(object sender, EventArgs e) { SendBox.Clear(); } private void ClearReceive_Click(object sender, EventArgs e) { ReceiveBox.Clear(); } private void Exit_Click(object sender, EventArgs e) { try { serialPort1.Close(); } catch (TimeoutException ex) { MessageBox.Show("错误:" + ex.Message); } Close(); } } } ...展开收缩
(系统自动生成,下载前可以参看下载内容)

下载文件列表

相关说明

  • 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
  • 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度
  • 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
  • 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
  • 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
  • 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.
 相关搜索: 串口
 输入关键字,在本站1000多万海量源码库中尽情搜索: