实例介绍
【实例简介】串口调试助手
【实例截图】
【核心代码】
using System; using System.Drawing; using System.IO.Ports; using System.Text; using System.Windows.Forms; namespace SerialCommunications { public partial class Form1 : Form { public Form1() { InitializeComponent(); Init(); } private SerialPort ComDevice = new SerialPort(); Timer timeSend; private void Form1_Load(object sender, EventArgs e) { System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath(); path.AddEllipse(this.pictureBox1.ClientRectangle); Region reg = new Region(path); this.pictureBox1.Region = reg; this.pictureBox1.BackColor = Color.Red; ZDsend.Enabled = false; } public void Init() { comboBox_Port.Items.AddRange(SerialPort.GetPortNames()); if (comboBox_Port.Items.Count > 0) { comboBox_Port.SelectedIndex = 0; } else { MessageBox.Show("未检测到串口"); } comboBox_BaudRate.SelectedIndex = 5; comboBox_CheckBits.SelectedIndex = 0; comboBox_DataBits.SelectedIndex = 3; comboBox_StopBits.SelectedIndex = 0; } void ComDevice_DataReceived(object sender, SerialDataReceivedEventArgs e) { byte[] ReDatas = new byte[ComDevice.BytesToRead]; ComDevice.Read(ReDatas, 0, ReDatas.Length); AddData(ReDatas); } void AddData(byte[] ReDatas) { if (cbResvTo16.Checked==true) { foreach (byte b in ReDatas) { AddContent(b.ToString("X2")); } } else { AddContent(Encoding.Default.GetString(ReDatas)); } } void AddContent(string str) { BeginInvoke(new MethodInvoker(delegate { textBox_Receive.AppendText(str); })); } private void openOrStop_Click(object sender, EventArgs e) { if (openOrStop.Text == "开启端口") { if (comboBox_Port.Items.Count <= 0) { MessageBox.Show("未发现可用串口"); return; } if (ComDevice.IsOpen == false) { ComDevice.PortName = comboBox_Port.SelectedItem.ToString(); ComDevice.BaudRate = Convert.ToInt32(comboBox_BaudRate.SelectedItem.ToString()); ComDevice.Parity = (Parity)Convert.ToInt32(comboBox_CheckBits.SelectedIndex.ToString()); ComDevice.DataBits = Convert.ToInt32(comboBox_DataBits.SelectedItem.ToString()); ComDevice.StopBits = (StopBits)Convert.ToInt32(comboBox_StopBits.SelectedItem.ToString()); try { ComDevice.ReceivedBytesThreshold = 1; this.ComDevice.ReadBufferSize = 0x100000; this.ComDevice.WriteBufferSize = 0x5000; ComDevice.DataReceived = ComDevice_DataReceived; //开启串口 ComDevice.Open(); button_Send.Enabled = true; ZDsend.Enabled = true; } catch (Exception ex) { MessageBox.Show(ex.Message, "未能成功开启串口", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } openOrStop.Text = "关闭"; pictureBox1.BackColor = Color.Green; } else { try { //关闭串口 ComDevice.Close(); button_Send.Enabled = false; } catch (Exception ex) { MessageBox.Show(ex.Message, "串口关闭错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } openOrStop.Text = "开启端口"; pictureBox1.BackColor = Color.Red; } comboBox_Port.Enabled = !ComDevice.IsOpen; comboBox_BaudRate.Enabled = !ComDevice.IsOpen; comboBox_DataBits.Enabled = !ComDevice.IsOpen; comboBox_StopBits.Enabled = !ComDevice.IsOpen; comboBox_CheckBits.Enabled = !ComDevice.IsOpen; } else if (openOrStop.Text == "关闭") { if (ComDevice.IsOpen == true) { ComDevice.Close(); comboBox_Port.Enabled = !ComDevice.IsOpen; comboBox_BaudRate.Enabled = !ComDevice.IsOpen; comboBox_DataBits.Enabled = !ComDevice.IsOpen; comboBox_StopBits.Enabled = !ComDevice.IsOpen; comboBox_CheckBits.Enabled = !ComDevice.IsOpen; pictureBox1.BackColor = Color.Red; openOrStop.Text = "开启端口"; button_Send.Enabled = false; ZDsend.Checked = false; ZDsend.Enabled = false; } } } private void ZDsend_CheckedChanged(object sender, EventArgs e) { if (ZDsend.Checked == true) { try { if (string.IsNullOrEmpty(sendTextBox.Text)) { MessageBox.Show("请输入发送内容"); ZDsend.Checked = false; return; } if (ZDsend.Checked == true) { timeSend = new Timer(); timeSend.Interval = Convert.ToInt32(numTime.Value); timeSend.Tick = timeSend_Tick; timeSend.Start(); } } catch (Exception) { throw; } } } byte[] strToHexByte(string sendData) { string hexString = sendData.Replace(" ", ""); if ((hexString.Length % 2) != 0) hexString = " "; byte[] returnBytes = new byte[hexString.Length / 2]; for (int i = 0; i < returnBytes.Length; i ) returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2).Replace(" ", ""), 16); return returnBytes; } void send() { if (ComDevice.IsOpen) { if (cbResvTo16Send.Checked == true) { string sendData = sendTextBox.Text; ComDevice.Write(strToHexByte(sendData), 0, strToHexByte(sendData).Length); } else { string sendData = sendTextBox.Text; ComDevice.Write(Encoding.Default.GetBytes(sendData), 0, Encoding.Default.GetBytes(sendData).Length); } // MessageBox.Show("打开"); } } void timeSend_Tick(object sender, EventArgs e) { try { timeSend.Stop(); send(); } catch (Exception) { throw; } finally { timeSend.Start(); } } private void button2_Click(object sender, EventArgs e) { textBox_Receive.Text = string.Empty; } private void button3_Click(object sender, EventArgs e) { sendTextBox.Text = string.Empty; } private void button_Send_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(sendTextBox.Text)) { MessageBox.Show("请输入发送内容"); return; } send(); } } }
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论