c# 写的串口调试助手 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 MySerialPorts{ public partial class MainForm : Form { public bool OpenButton = true; public int ireadDataBox = 0; public MainForm() { InitializeComponent(); } private void MainForm_Load(object sender, EventArgs e) { PortCom.DropDownStyle = ComboBoxStyle.DropDownList; string[] portlist = SerialPort.GetPortNames(); foreach(string port in portlist) { PortCom.Items.Add(port); } if (PortCom.Items.Count != 0) { PortCom.SelectedIndex = 0; } baudBox.DropDownStyle = ComboBoxStyle.DropDownList; baudBox.SelectedIndex = 4; stopBitBox.DropDownStyle = ComboBoxStyle.DropDownList; stopBitBox.SelectedIndex = 0; dataBitBox.DropDownStyle = ComboBoxStyle.DropDownList; dataBitBox.SelectedIndex = 3; parityBox.DropDownStyle = ComboBoxStyle.DropDownList; parityBox.SelectedIndex = 0; } public void PortSet() { if (serialPort1.IsOpen == false) { serialPort1.PortName = PortCom.Text; } serialPort1.BaudRate = int.Parse(baudBox.Text); serialPort1.DataBits = int.Parse(dataBitBox.Text); switch (stopBitBox.Text) { case "1": serialPort1.StopBits = StopBits.One; break; case "1.5": serialPort1.StopBits = StopBits.OnePointFive; break; case "2": serialPort1.StopBits = StopBits.Two; break; default: break; } switch (parityBox.Text) { case "无": serialPort1.Parity = Parity.None; break; case "奇校验": serialPort1.Parity = Parity.Odd; break; case "偶校验": serialPort1.Parity = Parity.Even; break; default: break; } } public void btnComConnet_Click(object sender, EventArgs e) { if (PortCom.Items.Count != 0) { if (serialPort1.IsOpen == false) { PortSet(); try { serialPort1.Open(); serialPort1.ReadTimeout = 1000; serialPort1.WriteTimeout = 1000; PortCom.Enabled = false; btnComConnet.Text = "关闭串口"; } catch { MessageBox.Show("打开串口失败"); } } else { try { serialPort1.Close(); PortCom.Enabled = true; btnComConnet.Text = "打开串口"; } catch { MessageBox.Show("串口关闭失败"); } } } else { MessageBox.Show("没有串口,请检查电脑"); } } private void btnSendData_Click(object sender, EventArgs e) { if (serialPort1.IsOpen) { byte[] data = Encoding.Default.GetBytes(SendDataBox.Text); if (selectHexBox.Checked == true) { for(int i=0; i<data.Length; i ) { byte temp = data[i]; string tempHex = temp.ToString("X2") ""; serialPort1.Write(tempHex); } } else { serialPort1.Write(data, 0, data.Length); } } else { MessageBox.Show("请先打开串口"); } } private void MainForm_Activated(object sender, EventArgs e) { if(serialPort1.IsOpen) { try { string message = serialPort1.ReadLine(); readDataBox.Text = message; } catch { } } } private void btnClearData_Click(object sender, EventArgs e) { SendDataBox.Clear(); } private void selectHexBox_CheckedChanged(object sender, EventArgs e) { //if (serialPort1.IsOpen) //{ //} //else //{ // MessageBox.Show("请先打开串口"); //} } public delegate void DeleUpdateTextbox(string dataRe); private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e) { string dataRe; byte[] byteRead = new byte[serialPort1.BytesToRead]; DeleUpdateTextbox deleupdatetextbox = new DeleUpdateTextbox(UpdateTextBox); serialPort1.Read(byteRead, 0, byteRead.Length); if (DisplayHex.Checked == true) { for (int i = 0; i < byteRead.Length; i ) { byte temp = byteRead[i]; dataRe = temp.ToString("X2") ""; readDataBox.Invoke(deleupdatetextbox, dataRe); } } else { dataRe = Encoding.Default.GetString(byteRead); readDataBox.Invoke(deleupdatetextbox, dataRe); } } private void UpdateTextBox(string dataRe) { if(ireadDataBox == 0) { this.readDataBox.Text = dataRe; ireadDataBox ; } else { readDataBox.AppendText(dataRe); } } private void DisplayHex_CheckedChanged(object sender, EventArgs e) { //if (serialPort1.IsOpen) //{ //} //else //{ // MessageBox.Show("请先打开串口"); //} } private void ClearRead_Click(object sender, EventArgs e) { readDataBox.Clear(); } private void PortSetValueChange(object sender, EventArgs e) { if (serialPort1.IsOpen == true) { PortSet(); } } private void PortCom_DropDown(object sender, EventArgs e) { timer1.Enabled = false; } private void timer1_Tick(object sender, EventArgs e) { int num = PortCom.SelectedIndex; PortCom.Items.Clear(); string[] portlist = SerialPort.GetPortNames(); PortCom.DropDownStyle = ComboBoxStyle.DropDownList; PortCom.Items.AddRange(portlist); if (PortCom.Items.Count != 0) { if ((PortCom.Items.Count-1) < num) { PortCom.SelectedIndex = (PortCom.Items.Count-1); } else { PortCom.SelectedIndex = num; } } } private void PortCom_DropDownClosed(object sender, EventArgs e) { timer1.Enabled = true; } }}
下载C# 串口调试助手 工具源码用户还喜欢
- 18480 文章数
- 500万+ 热度
作者专栏
编辑推荐
- 淡抹u2引擎,修复内容较多,物有所值
- 界域传说·经典巨作=传世单机(一键安装)
- 丸子版本(175个传世版本大集合)
- GS版本:神话公益服务端+客户端
- 图片放大工具(放大图片不模糊)
- 剪映无限制VIP版
- 传奇世界客户端下载器,史上最全传世客户端
- 传世GS20220920商业引擎注册+登录配置器 解压密码是1
- U2官方排行榜游戏网关 支持元神,支持传家宝
- GS开战传世客户端+服务端
- (淡漠夕阳)u2引擎合区工具
- 传世GS引擎消除“你的游戏客户端版本号过旧,请及时更新”提示
- 传世一机多区双线路配置器--免密码版本
- 传世凤凰登陆器劫持修复软件
- SQLite3 for Navicat
- 传奇世界npc对话框编辑工具
- 传世GS落霞铭文服务器端
- gs_20210409引擎包+注册机(无限制)
- 传奇世界NPC对话封包查看器[支持时长版和极速版]
- 彩虹引擎传世脚本编辑工具1.7版来了,支持函数脚本翻译
评论