找传奇、传世资源到传世资源站!

C# 串口调试助手 工具源码

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

c# 写的串口调试助手from clipboard 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; } }}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复