基本信息
源码名称:C# 串口调试助手源码
源码大小:0.38M
文件格式:.rar
开发语言:C#
更新时间:2019-04-03
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading.Tasks;
using System.Linq;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.IO;
namespace 串口调试助手
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/********************************************************************************************************/
private void Form1_Load(object sender, EventArgs e)
{
COM();// 解决多次添加相同端口的BUG
}
/********************************************************************************************************/
// 获取端口
private void COM()
{
string[] a = SerialPort.GetPortNames();
if (a.Length > 0)
{
for (int i = 0; i < a.Length; i )
{
comboBox1.Items.Add(a[i]);
}
comboBox1.Text = a[0]; // 将a[0]设置为默认端口
}
}
/*******************************************************************************************************/
//字符串转换成16进制字符
private string ToHex(string str)
{
if (str == "") return "";
byte[] Char = Encoding.UTF8.GetBytes(str); // 编码,将字符串转成单个字节
string str1 = "";
for (int i = 0; i < Char.Length; i )
{
str1 = Char[i].ToString("X2") " "; // 将字符转成16进制
}
return str1;
}
/********************************************************************************************************/
private void label3_Click(object sender, EventArgs e)
{
}
/********************************************************************************************************/
int button1_Click_mark = 0; // 打开标志
// 打开或关闭按钮
private void button1_Click(object sender, EventArgs e)
{
// 打开串口
if (button1_Click_mark == 0)
{
if (UART.IsOpen == false)
{
//COM();
UART.PortName = comboBox1.Text;
UART.BaudRate = Convert.ToInt32(comboBox2.Text);
UART.DataBits = Convert.ToInt32(comboBox4.Text);
//UART.StopBits = (StopBits)comboBox3.Text;
//string n = Convert.ToString(comboBox3.Text);
try
{
UART.Open();
//MessageBox.Show("打开串口成功");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
button1.Text = "关闭串口";
}
else // 关闭串口
{
try
{
UART.Close();
//COM();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
button1.Text = "打开串口";
}
}
}
/********************************************************************************************************/
private void richTextBox2_TextChanged(object sender, EventArgs e)
{
}
/********************************************************************************************************/
// 串口接收数据
private void UART_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
richTextBox2.Invoke(new MethodInvoker(delegate
// delegate 申明匿名方法,强制转换为ThreadStart委托
// MethodInvoker 强制转换为MethodInvoker委托
{
if (checkBox1.Checked)
{
// string a = ToHex(sp.ReadExisting());解决之前的十六进制转成十进制的的BUG
string a = ToHex(sp.ReadExisting()); // 获取接收缓冲区的数据
richTextBox2.AppendText(a); // 经转成16进制放置到追加文本
}
else
{
UART.Encoding = System.Text.Encoding.GetEncoding("GB2312");// 解决第一次接收出现乱码的BUG
richTextBox2.AppendText(sp.ReadExisting());
richTextBox2.Text = richTextBox2.Text.ToString();
}
richTextBox2.Text = "";
// 获取接收到数据的字节数
string c = richTextBox2.Text;
int x = c.Length;
toolStripStatusLabel2.Text = x.ToString();
}));
}
/********************************************************************************************************/
private void button4_Click(object sender, EventArgs e)
{
richTextBox2.Clear(); // 清空接收文本框
toolStripStatusLabel2.Text = " ";
}
/********************************************************************************************************/
// 串口发送数据
private void button2_Click(object sender, EventArgs e)
{
// 设置定时器的时间周期
timer1.Interval = Convert.ToInt16(textBox2.Text);
// 实现定时发送数据
if (checkBox4.Checked) timer1.Enabled = true;
else timer1.Enabled = false;
try
{
if (UART.IsOpen == true)
{
if (comboBox1.CanSelect)
{
if (checkBox2.Checked)
{
if (checkBox11.Checked) // 发送新行
{
string sendContent_1 = richTextBox1.Text.ToString(); // 获取输入的文本
UART.Write(ToHex(sendContent_1) "\r\n"); // 将字符串转换成16进制再发送数据
}
else
{
string sendContent_1 = richTextBox1.Text.ToString(); // 获取输入的文本
UART.Write(ToHex(sendContent_1) " "); // 将字符串转换成16进制再发送数据
}
}
else
{
if (checkBox11.Checked) // 发送新行
{
UART.Encoding = System.Text.Encoding.GetEncoding("GB2312");// 解决发送中文出现的乱码
string sendContent_2 = richTextBox1.Text.ToString(); // 获取输入的文本
UART.Write(sendContent_2 "\r\n"); // 发送数据
}
else
{
UART.Encoding = System.Text.Encoding.GetEncoding("GB2312");// 解决发送中文出现的乱码
string sendContent_2 = richTextBox1.Text.ToString(); // 获取输入的文本
UART.Write(sendContent_2 " "); // 发送数据
}
}
}
}
// 获取发送的字节数
string c = richTextBox1.Text;
int x = c.Length;
toolStripStatusLabel1.Text = x.ToString();
}
catch (Exception eio)
{
MessageBox.Show("串口发送异常:" eio);
}
}
/********************************************************************************************************/
private void button3_Click(object sender, EventArgs e)
{
richTextBox1.Clear();// 清空发送文本框
toolStripStatusLabel1.Text = " ";
}
/********************************************************************************************************/
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
}
/********************************************************************************************************/
// 接收框背景颜色
private void checkBox3_CheckedChanged(object sender, EventArgs e)
{
if (checkBox3.Checked)
{
richTextBox2.BackColor = System.Drawing.SystemColors.InfoText;// 颜色:InfoText
richTextBox2.ForeColor = System.Drawing.Color.LawnGreen; // 颜色:LawnGreen
}
else
{
richTextBox2.BackColor = System.Drawing.SystemColors.ControlLightLight;// 颜色:ControlLightLight
richTextBox2.ForeColor = System.Drawing.Color.Black; // 颜色:Black
}
}
/********************************************************************************************************/
private void label7_Click(object sender, EventArgs e)
{
}
/********************************************************************************************************/
private void button5_Click(object sender, EventArgs e)
{
if (UART.IsOpen == true)
{
if (comboBox1.CanSelect)
{
if (checkBox5.Checked)
{
string sendContent_3 = richTextBox3.Text.ToString(); // 获取输入的文本
UART.Write(ToHex(sendContent_3)); // 将字符串转换成16进制再发送数据
}
else
{
UART.Encoding = System.Text.Encoding.GetEncoding("GB2312");// 解决发送中文出现的乱码
string sendContent_4 = richTextBox3.Text.ToString(); // 获取输入的文本
UART.Write(sendContent_4); // 发送数据
}
}
}
}
/********************************************************************************************************/
private void button6_Click(object sender, EventArgs e)
{
if (UART.IsOpen == true)
{
if (comboBox1.CanSelect)
{
if (checkBox6.Checked)
{
string sendContent_3 = richTextBox4.Text.ToString(); // 获取输入的文本
UART.Write(ToHex(sendContent_3)); // 将字符串转换成16进制再发送数据
}
else
{
UART.Encoding = System.Text.Encoding.GetEncoding("GB2312");// 解决发送中文出现的乱码
string sendContent_4 = richTextBox4.Text.ToString(); // 获取输入的文本
UART.Write(sendContent_4); // 发送数据
}
}
}
}
/********************************************************************************************************/
private void button7_Click(object sender, EventArgs e)
{
if (UART.IsOpen == true)
{
if (comboBox1.CanSelect)
{
if (checkBox7.Checked)
{
string sendContent_3 = richTextBox5.Text.ToString(); // 获取输入的文本
UART.Write(ToHex(sendContent_3)); // 将字符串转换成16进制再发送数据
}
else
{
UART.Encoding = System.Text.Encoding.GetEncoding("GB2312");// 解决发送中文出现的乱码
string sendContent_4 = richTextBox5.Text.ToString(); // 获取输入的文本
UART.Write(sendContent_4); // 发送数据
}
}
}
}
/********************************************************************************************************/
private void button10_Click(object sender, EventArgs e)
{
if (UART.IsOpen == true)
{
if (comboBox1.CanSelect)
{
if (checkBox8.Checked)
{
string sendContent_3 = richTextBox8.Text.ToString(); // 获取输入的文本
UART.Write(ToHex(sendContent_3)); // 将字符串转换成16进制再发送数据
}
else
{
UART.Encoding = System.Text.Encoding.GetEncoding("GB2312");// 解决发送中文出现的乱码
string sendContent_4 = richTextBox8.Text.ToString(); // 获取输入的文本
UART.Write(sendContent_4); // 发送数据
}
}
}
}
/********************************************************************************************************/
private void button9_Click(object sender, EventArgs e)
{
if (UART.IsOpen == true)
{
if (comboBox1.CanSelect)
{
if (checkBox9.Checked)
{
string sendContent_3 = richTextBox7.Text.ToString(); // 获取输入的文本
UART.Write(ToHex(sendContent_3)); // 将字符串转换成16进制再发送数据
}
else
{
UART.Encoding = System.Text.Encoding.GetEncoding("GB2312");// 解决发送中文出现的乱码
string sendContent_4 = richTextBox7.Text.ToString(); // 获取输入的文本
UART.Write(sendContent_4); // 发送数据
}
}
}
}
/********************************************************************************************************/
private void button8_Click(object sender, EventArgs e)
{
if (UART.IsOpen == true)
{
if (comboBox1.CanSelect)
{
if (checkBox10.Checked)
{
string sendContent_3 = richTextBox6.Text.ToString(); // 获取输入的文本
UART.Write(ToHex(sendContent_3)); // 将字符串转换成16进制再发送数据
}
else
{
UART.Encoding = System.Text.Encoding.GetEncoding("GB2312");// 解决发送中文出现的乱码
string sendContent_4 = richTextBox6.Text.ToString(); // 获取输入的文本
UART.Write(sendContent_4); // 发送数据
}
}
}
}
private void 计算机ToolStripMenuItem_Click(object sender, EventArgs e)
{
// System.Diagnostics 系统进程、事件日志、和性能计数器进行交互的类库
ProcessStartInfo Info = new ProcessStartInfo(); // 启动进程数值的实例化
Info.FileName = "calc.exe "; // "calc.exe"为计算器,"notepad.exe"为记事本
Process Proc = Process.Start(Info); // 启动与关闭本地进程
}
private void timer2_Tick(object sender, EventArgs e)
{
// 显示当前时间
toolStripStatusLabel4.Text = DateTime.Now.ToString();
}
private void button13_Click(object sender, EventArgs e)
{
string path = textBox3.Text;
if (File.Exists(@path))
{
// 文件输入流 1、文件路径 2、编码(默认)
// Default 默认编码
StreamReader rd =
new StreamReader(@path, Encoding.Default);
string line;
do
{
line = rd.ReadLine();// 读一行数据
Console.WriteLine(line);
UART.Encoding = System.Text.Encoding.GetEncoding("GB2312"); // 解决发送中文出现的乱码
UART.Write(line "\r\n"); // 发送数据
}
while (line != null); // 文件读完了
}
}
private void button11_Click(object sender, EventArgs e)
{
OpenFileDialog file = new OpenFileDialog();//打开用户文件对话框
file.Filter = "文本文件|*.txt";
if (file.ShowDialog() == DialogResult.OK)
{
textBox3.SelectedText = file.FileName;
}
}
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
}
//private void nn()//发送进度
//{
// int space = progressBar1.Value;
// FileInfo fileinfo = null;
// double space_2 = System.Math.Ceiling(fileinfo.Length / 1024.0);
// double space_3 = space_2 / 100;
// int space_4 = Convert.ToInt16(space_3);
// space = space_4;
// //space = file.f
// progressBar1.Value = space;
//}
}
}