本人原创作品 希望提供给大家学习 共同学习 共同进步
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;
namespace SerialPortDebuggingTools
{
public partial class SerialPortDebugging : Form
{
public SerialPortDebugging()
{
InitializeComponent();
}
public byte[] recb;
delegate void SetTextCallback(string text, int count);
public bool song16jinz = false;
/// <summary>
/// 代理方法给UI输出值
/// </summary>
/// <param name="text"></param>
private void SetText(string text, int count)
{
if (this.richTextBoxjieshou.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text, count });
}
else { if (this.checkBoxenter.Checked) { RemoveText(); this.richTextBoxjieshou.AppendText(text + "\r\n"); } else { RemoveText(); this.richTextBoxjieshou.AppendText(text); } textBoxRx.Text = string.Format("RX:{0}", JShou(count)); }
}
public void RemoveText()
{
int count=richTextBoxjieshou.Lines.Length;
if (count>=18)
{
richTextBoxjieshou.Text = "";
}
}
/// <summary>
/// 打开串口或者关闭串口
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void butCorO_Click(object sender, EventArgs e)
{
if (SerialProtTools.IsOpen)
{
if (!this.checkBoxzhouqi.Checked)
{
SerialProtTools.Close(); this.butCorO.Text = "打开串口";
this.pictureBoxEnabled.Image = global::SerialPortDebuggingTools.Properties.Resources.colse;
this.textBoxstatus.Text = SerialProtTools.PortName + " COLSED," + SerialProtTools.BaudRate + "," + this.comboBoxJyw.Text + "," + SerialProtTools.DataBits + "," + this.comboBoxTzw.Text;
}
else
{
MessageBox.Show("请先关闭自动发送才能关闭串口", "温馨提示!", MessageBoxButtons.OK, MessageBoxIcon.Hand);
}
}
else
{
try
{
SerialProtTools.Open();
this.butCorO.Text = "关闭串口";
this.pictureBoxEnabled.Image = global::SerialPortDebuggingTools.Properties.Resources.open;
this.textBoxstatus.Text = SerialProtTools.PortName + " OPENED," + SerialProtTools.BaudRate + "," + this.comboBoxJyw.Text + "," + SerialProtTools.DataBits + "," + this.comboBoxTzw.Text;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "温馨提示!", MessageBoxButtons.OK, MessageBoxIcon.Hand);
this.butCorO.Text = "打开串口";
this.pictureBoxEnabled.Image = global::SerialPortDebuggingTools.Properties.Resources.colse;
this.textBoxstatus.Text = SerialProtTools.PortName + " COLSED," + SerialProtTools.BaudRate + "," + this.comboBoxJyw.Text + "," + SerialProtTools.DataBits + "," + this.comboBoxTzw.Text;
}
}
}
/// <summary>
/// 初始化串口
/// </summary>
public void initSerialProt()
{
bool YmY = false;
string[] ports = SerialPort.GetPortNames();
foreach (string port in ports)
{
this.comboBoxserialPort.Items.Add(port);
YmY = true;
}
if (YmY)
{
this.comboBoxserialPort.SelectedIndex = 0;
}
}
/// <summary>
/// 初始化所有串口相关的列
/// </summary>
public void initAll()
{
this.comboBoxbtv.SelectedItem = "9600";
this.comboBoxJyw.SelectedItem = "NONE";
this.comboBoxSjw.SelectedItem = "8";
this.comboBoxTzw.SelectedItem = "1";
}
private void SerialPortDebugging_Load(object sender, EventArgs e)
{
initSerialProt();
initAll();
comboBoxserialPort_SelectionChangeCommitted(sender, e);
}
/// <summary>
/// 配置串口的所有信息
/// </summary>
/// <param name="SerPort">串口对象</param>
/// <param name="SerialName">串口名称</param>
/// <param name="SerialBtv">串口波特率4800,9600,14400,19200,38400,56000,57600,115200,128000,256000</param>
/// <param name="SerialJyw">串口校验位NONE,ODD,EVEN</param>
/// <param name="SerialSjw">串口数据位8,7,6</param>
/// <param name="SerialTzw">串口停止位1,2</param>
/// <returns></returns>
public SerialPort ConfigProt(SerialPort SerPort, string SerialName, string SerialBtv, string SerialJyw, string SerialSjw, string SerialTzw)
{
if (SerialName != null && !SerialName.Equals(""))
{
SerPort.PortName = SerialName;
}
//波特率设置
switch (SerialBtv)
{
case "4800":
SerPort.BaudRate = 4800;
break;
case "9600":
SerPort.BaudRate = 9600;
break;
case "14400":
SerPort.BaudRate = 14400;
break;
case "19200":
SerPort.BaudRate = 19200;
break;
case "38400":
SerPort.BaudRate = 38400;
break;
case "56000":
SerPort.BaudRate = 56000;
break;
case "57600":
SerPort.BaudRate = 57600;
break;
case "115200":
SerPort.BaudRate = 115200;
break;
default:
SerPort.BaudRate = 9600;
break;
}
//设置校验位
switch (SerialJyw)
{
case "NONE":
SerPort.Parity = System.IO.Ports.Parity.None;
break;
case "ODD":
SerPort.Parity = System.IO.Ports.Parity.Odd;
break;
case "EVEN":
SerPort.Parity = System.IO.Ports.Parity.Even;
break;
default:
SerPort.Parity = System.IO.Ports.Parity.None;
break;
}
//设置数据位
switch (SerialSjw)
{
case "8":
SerPort.DataBits = 8;
break;
case "7":
SerPort.DataBits = 7;
break;
case "6":
SerPort.DataBits = 6;
break;
default:
SerPort.DataBits = 8;
break;
}
switch (SerialTzw)
{
case "1":
SerPort.StopBits = System.IO.Ports.StopBits.One;
break;
case "2":
SerPort.StopBits = System.IO.Ports.StopBits.Two;
break;
default:
SerPort.StopBits = System.IO.Ports.StopBits.One;
break;
}
return SerPort;
}
private void comboBoxserialPort_SelectionChangeCommitted(object sender, EventArgs e)
{
if (SerialProtTools.IsOpen) SerialProtTools.Close();
this.SerialProtTools = ConfigProt(this.SerialProtTools, this.comboBoxserialPort.Text, this.comboBoxbtv.Text, this.comboBoxJyw.Text, this.comboBoxSjw.Text, this.comboBoxTzw.Text);
try
{
SerialProtTools.Open();
this.butCorO.Text = "关闭串口";
this.pictureBoxEnabled.Image = global::SerialPortDebuggingTools.Properties.Resources.open;
if (SerialProtTools.IsOpen)
{
this.textBoxstatus.Text = SerialProtTools.PortName + " OPENED," + SerialProtTools.BaudRate + "," + this.comboBoxJyw.Text + "," + SerialProtTools.DataBits + "," + this.comboBoxTzw.Text;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "温馨提示!", MessageBoxButtons.OK, MessageBoxIcon.Hand);
this.butCorO.Text = "打开串口";
this.checkBoxzhouqi.Checked = false;
this.timerSong.Stop();
this.pictureBoxEnabled.Image = global::SerialPortDebuggingTools.Properties.Resources.colse;
}
}
private void butcolsed_Click(object sender, EventArgs e)
{
try
{
this.timerSong.Stop();
Application.Exit();
}
catch (Exception)
{
Application.Exit();
}
finally {
Application.Exit();
}
}
private void butqingkongjies_Click(object sender, EventArgs e)
{
richTextBoxjieshou.Text = "";
}
private void butqingkongSong_Click(object sender, EventArgs e)
{
richTextBoxSong.Text = "";
}
private void textBoxhangmiao_KeyPress(object sender, KeyPressEventArgs e)
{
this.checkBoxzhouqi.Checked = false;
if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != 46)
{
e.Handled = true;
}
}
private void butqingling_Click(object sender, EventArgs e)
{
textBoxRx.Text = "RX:0";
textBoxtx.Text = "TX:0";
}
private void checkBox16jinz_CheckedChanged(object sender, EventArgs e)
{
if (checkBox16jinz.Checked)
{
song16jinz = true;
checkBoxjieshou.Checked = true;
string temp = this.richTextBoxSong.Text;
temp = temp.Replace(" ", "");
if (delyichang(temp))
{
this.richTextBoxSong.Text = "";
}
}
else
{
song16jinz = false;
string temp = this.richTextBoxSong.Text;
temp = temp.Replace(" ", "");
if (delyichang(temp))
{
this.richTextBoxSong.Text = "";
}
checkBoxjieshou.Checked = false;
}
}
private void checkBoxjieshou_CheckedChanged(object sender, EventArgs e)
{
if (checkBoxjieshou.Checked)
{
song16jinz = true;
checkBox16jinz.Checked = true;
string temp = this.richTextBoxSong.Text;
temp = temp.Replace(" ", "");
if (delyichang(temp))
{
this.richTextBoxSong.Text = "";
}
}
else
{
song16jinz = false;
string temp = this.richTextBoxSong.Text;
temp = temp.Replace(" ", "");
if (delyichang(temp))
{
this.richTextBoxSong.Text = "";
}
checkBox16jinz.Checked = false;
}
}
string jieshou = String.Empty;
private void SerialProtTools_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
if (SerialProtTools.IsOpen)
{
Thread.Sleep(100);
if (song16jinz)
{
int n = SerialProtTools.BytesToRead;
recb = new byte[n];
SerialProtTools.Read(recb, 0, recb.Length);
if (recb != null && recb.Length > 0)
{
SetText(dispackage(recb), n);
}
}
else
{
jieshou = SerialProtTools.ReadExisting();
if (jieshou != String.Empty)
{
SetText(jieshou, jieshou.Length);
}
}
}
}
/// <summary>
/// DIY方法的解析方法
/// </summary>
/// <param name="reb"></param>
/// <returns></returns>
public string dispackage(byte[] reb)
{
string temp = "";
foreach (byte b in reb)
temp += b.ToString("X2") + " ";
return temp;
}
/// <summary>
/// 16进制字符串转字节数组
/// </summary>
/// <param name="hexString"></param>
/// <returns></returns>
private static byte[] strToToHexByte(string hexString)
{
hexString = hexString.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).Trim(), 16);
return returnBytes;
}
/// <summary>
/// 发送信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void butshoudongsong_Click(object sender, EventArgs e)
{
SongSJ();
}
public void SongSJ()
{
if (SerialProtTools.IsOpen)
{
//代发送的值
int i = 0;
string liushu = this.richTextBoxSong.Text;
if (liushu != null && !liushu.Equals(""))
{
if (song16jinz)
{
liushu = liushu.Replace(" ", "");
i = liushu.Length / 2;
if (!delyichang(liushu))
{
SerialProtTools.Write(strToToHexByte(liushu), 0, i);
}
}
else
{
SerialProtTools.Encoding = Encoding.GetEncoding("GB2312");
i = liushu.Length;
SerialProtTools.Write(liushu);
}
textBoxtx.Text = string.Format("TX:{0}", jiaFan(i));
}
}
else
{
this.timerSong.Stop();
MessageBox.Show("串口没打开请打开串口再操作", "温馨提示!", MessageBoxButtons.OK, MessageBoxIcon.Hand);
}
}
public bool delyichang(string bigstr)
{
string jiache = "0123456789ABCDEF";
char[] charshuz = bigstr.ToCharArray();
for (int i = 0; i < charshuz.Length; i++)
{
char value = char.ToUpper(charshuz[i]);
if (jiache.IndexOf(value) < 0)
{
return true;
}
}
return false;
}
/// <summary>
/// 接受计数
/// </summary>
/// <param name="count"></param>
/// <returns></returns>
public int JShou(int count)
{
string temp = textBoxRx.Text;
temp = temp.Replace("RX:", "");
try { int cc = Convert.ToInt32(temp); return (cc + count); }
catch { return count; }
}
/// <summary>
/// 发送计数
/// </summary>
/// <param name="count"></param>
/// <returns></returns>
public int jiaFan(int count)
{
string temp = textBoxtx.Text;
temp = temp.Replace("TX:", "");
try { int cc = Convert.ToInt32(temp); return (cc + count); }
catch { return count; }
}
private void checkBoxzhouqi_CheckedChanged(object sender, EventArgs e)
{
string shijian = this.textBoxhangmiao.Text;
if (checkBoxzhouqi.Checked)
{
if (shijian != null && !shijian.Equals(""))
{
int zhouq = Convert.ToInt32(shijian);
if (zhouq > 0)
{
this.timerSong.Interval = zhouq;
this.timerSong.Start();
}
else
{
MessageBox.Show("自动发送周期必须大于0毫秒", "温馨提示!", MessageBoxButtons.OK, MessageBoxIcon.Hand);
}
}
}
else
{
this.timerSong.Stop();
}
}
private void timerSong_Tick(object sender, EventArgs e)
{
SongSJ();
}
private void butBoxAuthor_Click(object sender, EventArgs e)
{
}
}
}
- 大小: 62.7 KB
分享到:
相关推荐
串口调试助手是串口调试相关工具,支持9600,19200等常用各种波特率及自定义波特率,可以自动识别串口,能设置校验、数据位和停止位,能以ASCII码或十六进制接收或发送任何数据或字符, 主要实现的功能: 1、自动...
public static ArrayList Str16ToArrayList(string strIn) { string sParse = ""; ArrayList myAL = new ArrayList(); int i = 0; foreach (char cc in strIn) ... if (cc == ' ' || sParse.Length == 2) ...
C#编写的串口调试助手 很多和我一样学习软件专业的学生,有的时候对一些小项目真的是无可奈何 上次和我同学闲聊,他说C#写一个串口调试助手好难, 见此情景,我就不自觉得也来写了一个,亲测可用 主要功能有: 1....
本文将深入探讨以C#编写的串口调试助手,讲解其核心功能——数据收发与字节格式转换。 一、C#串口通信基础 C#中的串口通信主要依赖于System.IO.Ports命名空间,它包含了SerialPort类,用于处理串行端口的各种操作。...
基于C#的串口调试助手源码,供大家学习参考,也当自己备份!
《C#串口调试助手源码程序解析与应用》 在IT行业中,串口通信是一种常见且基础的设备间通信方式,尤其在嵌入式系统、工业自动化以及物联网设备等领域广泛应用。C#作为.NET框架的主要编程语言,为开发串口调试工具...
本文将详细探讨基于C#编写的串口调试助手,帮助初学者理解串口通信的基本概念,以及如何使用C#进行串口调试。 串口通信,也称为UART(通用异步接收/发送器)通信,是计算机硬件中的一种接口,用于与外部设备进行...
C#串口调试助手的核心功能主要包括以下几点: 1. **打开串口**:使用SerialPort类的Open()方法可以打开一个指定的串口。需要设置串口的波特率(如9600)、数据位(通常为8位)、停止位(1或2位)和校验位(无校验、...
本教程是针对C#初学者的一个傻瓜式教程,所谓傻瓜式教程即一步一步教你怎么操作,手把手的教,保证每个C#学者都能掌握,哪怕你是一年级小娃娃,只要能看得懂中国字,哪怕是不动脑筋也会把这个小项目搞出来。...
C#做的串口调试助手,实现一般串口调试助手的常用功能。用于是开放源代码的,对不满意的部分可以按自己需要进行修改。 内有一套皮肤控件并用皮肤控件的大致用法。 小特性:用户在指定时间为对程序作出任何反应,程序...
这个C#串口调试助手可以帮助开发者发送和接收串行数据,同时提供了一些基本的调试功能,如设置波特率、数据位、停止位、校验位等参数,以适应不同设备的通信需求。 在C#中,`System.IO.Ports`命名空间提供了...
为了方便开发人员进行串口调试,本文将深入探讨一款由C#编程语言编写的自制串口调试助手。这款工具以其简洁友好的用户界面和对不同进制数据的支持,为开发者提供了强大的辅助功能。 首先,我们要理解C#的基础。C#是...
本文将深入探讨一个由C#编写的串口调试助手,它具有显示接收数据时间、自动存储数据以及自定义存储路径的功能,对于初学者来说,是学习串口通信与C#编程的理想实践项目。 首先,我们要理解串口通信的基础概念。串口...
本文将深入探讨使用C#编程语言开发的串口调试助手,它不仅能处理串行通信,还支持TCP和UDP网络协议的数据收发。 首先,让我们来了解一下串口通信。串口,也称为COM口或串行接口,是计算机硬件的一种通信方式,用于...
总结,使用WPF和C#编写串口调试助手,不仅可以帮助开发者熟悉这两项技术,还能为实际项目提供一个实用的工具。通过这个助手,用户可以方便地测试串口设备,排查通信问题,对于学习和工作都有很大的帮助。在实践中...
本文将深入探讨一款用C#编写的串口调试助手程序,该程序由VS2010开发,源码注释详尽,旨在帮助初学者理解并掌握C#串口通信的实现。 串口通信的基础知识是理解串行接口,它通过串行数据线发送和接收数据,通常包括RX...