实例介绍
【实例简介】
【实例截图】
【核心代码】
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; using System.Text.RegularExpressions; namespace APP1 { public partial class fRame : Form { private StringBuilder builder = new StringBuilder();//避免在事件处理方法中反复的创建,定义到外面。 public fRame() { InitializeComponent(); } private void fRame_Load(object sender, EventArgs e) //装载主窗口 { search_serial(); //搜寻可用串口号 bandrate.SelectedIndex = bandrate.Items.IndexOf("9600"); //设置默认波特率 datalen.SelectedIndex = datalen.Items.IndexOf("8"); //设置默认数据位 stoplen.SelectedIndex = stoplen.Items.IndexOf("1"); //设置默认停止位 crclen.SelectedIndex = crclen.Items.IndexOf("0"); //设置默认校验位 //初始化SerialPort对象 serialPort.NewLine = "\r\n"; serialPort.RtsEnable = true; serialPort.Encoding = Encoding.GetEncoding("GB2312"); oPen.Enabled = true; cLose.Enabled = false; } private void oPen_Click(object sender, EventArgs e) //打开串口按钮功能 { serialPort.PortName = portname.Text; //串口名称 serialPort.BaudRate = int.Parse(bandrate.Text); //波特率 serialPort.DataBits = int.Parse(datalen.Text); //数据长度 serialPort.StopBits = (StopBits)Enum.Parse(typeof(StopBits),stoplen.Text); //停止位 serialPort.Parity = (Parity)Enum.Parse(typeof(Parity), crclen.Text); //校验位 try { serialPort.Open(); sTate.Text = "串口打开"; oPen.Enabled = false; //打开之后,失能 cLose.Enabled = true; } catch { MessageBox.Show("串口打开错误","提示"); } } private void cLose_Click(object sender, EventArgs e) { try { serialPort.Close(); sTate.Text = ""; oPen.Enabled = true; cLose.Enabled = false; } catch { MessageBox.Show("串口关闭错误","提示"); } } private void sEarch_Click(object sender, EventArgs e) //搜寻串口按钮功能 { search_serial(); } public void search_serial() //搜寻可用串口并添加到Port选择框 { portname.Items.Clear(); string[] ports = SerialPort.GetPortNames(); Array.Sort(ports); portname.Items.AddRange(ports); if (portname.Items.Count == 0) { portname.Items.Add("COM1"); } portname.SelectedIndex = portname.Items.Count > 0 ? 0 : -1; } private void sErialport_receive(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { if (sWitch2.Checked) { this.Invoke(new Action(() => { txGet.AppendText(serialPort.ReadExisting()); })); } else { this.Invoke(new Action(() => { int n = serialPort.BytesToRead;//先记录下来,避免某种原因,人为的原因,操作几次之间时间长,缓存不一致 byte[] buf = new byte[n];//声明一个临时数组存储当前来的串口数据 serialPort.Read(buf, 0, n);//读取缓冲数据 builder.Clear();//清除字符串构造器的内容 //依次的拼接出16进制字符串 foreach (byte b in buf) { builder.Append(b.ToString("X2") " "); } //追加的形式添加到文本框末端,并滚动到最后。 this.txGet.AppendText(builder.ToString()); })); } } private void send1_Click(object sender, EventArgs e) //第一队列发送按钮功能 { int send_num = 0; byte[] send_data = new byte[1]; string Send_Data; if (serialPort.IsOpen) { if (sWitch4.Checked) //选择字符格式发送 { try { serialPort.Write(txSend1.Text); } catch { MessageBox.Show("发送失败", "提示"); } } else { Send_Data = txSend1.Text; Send_Data = Send_Data.Replace(" ",""); Send_Data = Send_Data.Replace("0X",""); Send_Data = Send_Data.Replace("0x",""); for (int i = 0; i < (Send_Data.Length - Send_Data.Length % 2) / 2; i )//转换偶数个 { send_data[0] = Convert.ToByte(Send_Data.Substring(i * 2, 2), 16); //转换 serialPort.Write(send_data,0,1); } if (Send_Data.Length % 2 != 0) { send_data[0] = Convert.ToByte(txSend1.Text.Substring(Send_Data.Length - 1, 1), 16);//单独处理最后一个字符 serialPort.Write(send_data, 0, 1); //写入 } } } else { MessageBox.Show("串口未打开","提示"); } } private void send2_Click(object sender, EventArgs e) { } private void send3_Click(object sender, EventArgs e) { } private void cLear_Click(object sender, EventArgs e) { this.Invoke(new Action(() => { txGet.Text = ""; //txSend1.Text = ""; //txSend2.Text = ""; //txSend3.Text = ""; })); } } }
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论