基本信息
源码名称:C#串口调试助手工具源码
源码大小:0.20M
文件格式:.rar
开发语言:C#
更新时间:2018-03-22
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 10 元×
微信扫码支付:10 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
串口调试助手
串口调试助手
using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.IO.Ports;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public string fasongwb;//发送文件的文本
string jieshouwb;//接收到的文本
int SHUjieshou = 0;//接收计数
int SHUfasong = 0;//发送计数
int dsjs = 0;//定时发送次数
short kongzhi = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)//窗体加载
{
//MessageBox.Show(Algorithm.StringTo16("ff",Enum16进制隔离符.空格));
int b = 0;//数组下标
short i = 0;
string[] a = System.IO.Ports.SerialPort.GetPortNames();//获取已有的端口
b = a.Length ;//获取元素的个数
Timer2.Interval = 100;//启动写文件定时器
Timer2.Start();
SerialPort1.DataReceived = new SerialDataReceivedEventHandler(SerialPort1_DataReceived);//添加串口接收DataReceived事件
//---------初始化---------------------------
//Duankou.SelectedIndex = 0;
Botelv.SelectedIndex = 5;
Jiaoyanwei.SelectedIndex = 0;
Shujuwei.SelectedIndex = 2;
Tingzhiwei.SelectedIndex = 0;
Control.CheckForIllegalCrossThreadCalls = false;//屏蔽非法跨线程调用将始终引发异常
// ---------初始化---------------------------
//MessageBox.Show(b.ToString() );
if (b < 0)
{
MessageBox.Show("没有找到端口!", "警告");
}
else
{
//MessageBox .Show (b);
for (; i < b; i )
{
if (JieDuanKouShu (a[i])>6)
{
Duankou.Items .Add (a[i]);
}
}
i = 0;//将i清零
go:for (; i < b 6; i )
// 自动打开端口
try
{
if (SerialPort1.IsOpen == true)
{
break;
}
else
{
Duankou.SelectedIndex = i;
// MessageBox.Show(i.ToString ());
SerialPort1.PortName = Duankou.Text;
//MessageBox.Show(i.ToString());
SerialPort1.Open();
if (SerialPort1.IsOpen == true)
{
Duankou.SelectedIndex = i;
Label6.Text = "端口已打开";
PictureBox1.BackColor = Color.Green;
Button8.Text = "关闭端口";
}
}
}
catch (IOException)//串口不存在异常
{
i ;
goto go;
}
catch (UnauthorizedAccessException)//串口资源占用异常
{
i ;
goto go;
}
catch (InvalidOperationException)//串口打开后,不能在赋值端口异常
{
i ;
goto go;
}
}
if(SerialPort1.IsOpen==false)
{
Duankou.SelectedIndex = 0;
}
RichTextBox2.Text = "保存接收数据的路径";
this.Button2.Select(); //开始运行程序时,让发送区被选择,激活控件
CheckBox2.Checked = true ;
CheckBox3.Checked = true;
}
private int JieDuanKouShu(string str)// 将获得的端口,如COM1转化成int类型的1
{
str=str.Remove(0, 3);//从零开始删除3个字符
return (Convert.ToInt32(str));
}
private void Button5_Click(object sender, EventArgs e)//打开文本按钮
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.ShowDialog();
if (ofd.FileName == "")
{
RichTextBox1.Text = "要打开的文件路径";
}
else
{
Textfasong.Text = "";
RichTextBox1.Text = ofd.FileName;
StreamReader sr = new StreamReader(ofd .FileName);
Textfasong.Text = sr.ReadToEnd();//读出文本
fasongwb = Textfasong.Text;
sr.Close();//关闭当前文本读取流
if (CheckBox4.Checked ==false )
Textfasong.Text = "";
}
}
private void Button6_Click(object sender, EventArgs e)//保存文件按钮
{
FolderBrowserDialog ofd = new FolderBrowserDialog();
ofd.ShowDialog();
if (ofd.SelectedPath == "")
{
RichTextBox2.Text = "保存接收数据的路径";
}
else
RichTextBox2.Text = ofd.SelectedPath;
}
private void Button3_Click(object sender, EventArgs e)//清空接收
{
Textjieshou.Text = "";
}
private void Button4_Click(object sender, EventArgs e)//发送清空
{
Textfasong.Text = "";
}
private void Button1_Click(object sender, EventArgs e)//清空计数器
{
SHUjieshou = 0;
SHUfasong = 0;
Label8.Text = "发送" SHUfasong.ToString () "次";
Label9.Text = Label9.Text = "接收" SHUjieshou.ToString () "次";
}
private void CheckBox4_CheckedChanged(object sender, EventArgs e)//发送文本选择
{
if (CheckBox4.Checked == true)
{
CheckBox2.Checked = false;
Button2.Text = "发送文本";
Textfasong.Text = "";
Textfasong.Text = fasongwb;
}
else
{
Button2.Text = "发送数据";
Textfasong.Text = "";
}
}
private void CheckBox2_CheckedChanged(object sender, EventArgs e)//发送十六进制
{
if (CheckBox2.Checked == true)
CheckBox4.Checked = false;
}
private void CheckBox5_CheckedChanged(object sender, EventArgs e)//接收文件
{
if (CheckBox5.Checked == true )
{
CheckBox3.Checked = false;
if (wenjianlx(RichTextBox2.Text) == false)
{
CheckBox5.Checked = false;
CheckBox3.Checked = true;
MessageBox.Show("你的保存路径中没有文件名或文件类型" "\n" "如在保存路径中输入: C:" "\\" "baocun.txt" "\n" "\"" ".txt" "\"" "根据你接收的文件类型自己设置", "提示");
}
}
}
private bool wenjianlx(string str)//判断是有带有后缀名
{
string[] a;
string b;
char []st={'\\'};
a = str.Split(st);//分割文本str
if (a.Length < 0)
{
return (false);
}
else
{
b = a[a.Length-1];
foreach (char ch in b)
{
if (ch == '.')
{
return (true);
}
}
}
return (false );
}
private void CheckBox3_CheckedChanged(object sender, EventArgs e)
{
if (CheckBox3.Checked == true)
CheckBox5.Checked = false;
}
private void CheckBox1_CheckedChanged(object sender, EventArgs e)//定时发送数据
{
if (CheckBox1.Checked == true)
{
if ((RichTextBox3.Text == "")||(Convert.ToInt32(RichTextBox3.Text) <= 0) )//设置时间
{
MessageBox.Show("请设置发送时间!", "提示");
CheckBox1.Checked = false;
return;
}
else
Timer1.Interval = Convert.ToInt32(RichTextBox3.Text);
if ((RichTextBox4.Text == "")||(Convert.ToInt32(RichTextBox4.Text) <= 0) )//设置发送次数,0为不停的发送
{
dsjs = 0;
}
else
dsjs = Convert.ToInt32(RichTextBox4.Text);
Timer1.Start();
}
else
Timer1.Stop();
}
private void Timer1_Tick(object sender, EventArgs e)//定时器1事件
{
if (dsjs == 0)
FaSong();
else if (dsjs > 1)
{
dsjs = dsjs - 1;
FaSong();
}
else if (dsjs == 1)
{
FaSong();
dsjs = 0;
Timer1.Stop ();
CheckBox1.Checked = false;
}
// If dsjs = 0 Then
// 'SHUfasong = SHUfasong 1 '发送计数
// FaSong()
// 'MsgBox(dsjs)
//ElseIf dsjs > 1 Then
// 'SHUfasong = SHUfasong 1 '发送计数
// dsjs = dsjs - 1
// FaSong()
// 'MsgBox(dsjs 1)
//ElseIf dsjs = 1 Then
// 'SHUfasong = SHUfasong 1 '发送计数
// 'MsgBox(dsjs)
// FaSong()
// dsjs = 0
// Timer1.Enabled = False
// CheckBox1.Checked = False
//End If
}
private void FaSong()//发送子函数
{
int a,shuju=0;
string str;
str = Textfasong.Text;
foreach (char ch in str)//取字符长度
{
shuju ;
}
//MessageBox.Show(shuju.ToString ());
if (SerialPort1.IsOpen == false)
{
CheckBox1.Checked = false;
MessageBox.Show("端口未打开,请打开端口!", "提示");
}
else
{
// ------------------------------------发送十六进制数---------------------------------------------
if (CheckBox2.Checked == true)//发送十六进制数
{
if (shuju % 2 == 0)
{
byte[] data = Algorithm._16ToBtyes(Textfasong.Text);
SerialPort1.Write(data, 0, data.Length);
SHUfasong = SHUfasong data.Length;//发送计数
}
else
MessageBox.Show("十六进制错误,请您检查!","提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
// ------------------------------------发送十六进制数-------------------------------
//-------------发送文本----------------
else if (CheckBox4.Checked == true)
{
byte[] bt = Algorithm.StringToBytes(fasongwb,false);
SerialPort1.Write(bt, 0, bt.Length);
//SerialPort1.Write(fasongwb);
SHUfasong = SHUfasong 1;//发送计数
}
// Else '------------------------------------发送字符串----------------------------------
else
{
if (Textfasong.Text == "")
{
CheckBox1.Checked = false;
MessageBox.Show("请在发送数据框内输入数据!", "提示");
}
else
{
//SerialPort1.Write(Textfasong.Text);
byte[] bt = Algorithm.StringToBytes(Textfasong.Text, false);
SerialPort1.Write(bt, 0, bt.Length);
SHUfasong = SHUfasong 1; //发送计数
}
}
}
}