串口调试助手源代码
更新时间:2024-06-04 08:31:01 阅读量: 综合文库 文档下载
串口调试助手预源代码
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;
using System.IO.Ports; using System.Threading;
using DevExpress.XtraEditors;
using System.Text.RegularExpressions;
namespace WindowsFormsApplication3 {
public partial class Form1 : XtraForm {
SerialPort sp1 = new SerialPort(); int Flag = 0;
int StateChanged = 0;
int StateChanged_Ascii = 1; int StateChanged_16 = 0;
string Temp = null;
string Temp_memoSend = null;
private void memoSend_EditValueChanged(object sender, EventArgs e) {
}
public Form1() {
InitializeComponent();
sp1.DataReceived += sp1_DataReceived; }
private void Form1_Load(object sender, EventArgs e) {
this.MaximizeBox = false; //检查是否有串口
string[] str = SerialPort.GetPortNames(); if (str == null) {
MessageBox.Show(\本机没有串口!\ return; }
//添加串口项目
foreach (string port in System.IO.Ports.SerialPort.GetPortNames()) {
cbSerial.Properties.Items.Add(port); }
//串口设置默认选择项
cbSerial.SelectedIndex = 0;
Control.CheckForIllegalCrossThreadCalls = false; sp1.DataReceived += new
SerialDataReceivedEventHandler(sp1_DataReceived); //订阅委托
}
private void comboBoxEdit1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void OpenSerial_Click(object sender, EventArgs e) {
sp1.Close();
//SerialPort sp1 = new SerialPort();
if (!sp1.IsOpen)//串口是关闭的,设置参数,打开串口 {
try {
//获取串口号
string serialName = cbSerial.SelectedItem.ToString(); sp1.PortName = serialName;
//设置各参数
string strBaudRate = cbBaudRate.Text; string strDateBits = cbDataBits.Text; string strStopBits = cbStop.Text;
sp1.BaudRate = Convert.ToInt32(strBaudRate);//波特率 sp1.DataBits = Convert.ToInt32(strDateBits);//数据位 switch (cbStop.Text) //停止位 {
case \
sp1.StopBits = StopBits.One; break; case \
sp1.StopBits = StopBits.One; break; case \
sp1.StopBits = StopBits.Two; break; default:
MessageBox.Show(\参数不正确!\ break; }
switch (cbParity.Text) //校验位 {
case \无\
sp1.Parity = Parity.None; break; case \奇校验\
sp1.Parity = Parity.Odd; break; case \偶校验\
sp1.Parity = Parity.Even; break; default:
MessageBox.Show(\参数不正确!\ break; }
if (sp1.IsOpen == true) {
sp1.Close(); }
sp1.Open();
//btnOpen.Enabled = false;
labSerial.Text = \串口号:\ labState.Text = \状态:打开\ }
catch (System.Exception ex) {
MessageBox.Show(\ return; } } }
//private void CloseSerial_Click(object sender, EventArgs e) //{
// if (sp1.IsOpen) // {
// sp1.Close();
// labSerial.Text = \串口号:\ // labState.Text = \状态:关闭\ // } //}
private void Send_Click(object sender, EventArgs e) {
string StrSend = memoSend.Text; if (!sp1.IsOpen) {
MessageBox.Show(\请先打开串口!\ return; } else {
if (rbtnSendStr.Checked) //字符串发送 {
if (ckTimeSend.Checked
&& !(string.IsNullOrEmpty(textTime.Text)))//判断是否定时发送数据 {
string Time = textTime.Text;
tmSend.Interval = int.Parse(Time) * 1000;//时间单位是秒,化成毫秒单位
tmSend.Start(); //开启定时器 } else {
sp1.Write(StrSend); //写入数据 }
}
else //十六进制发送 {
if (ckTimeSend.Checked && !(string.IsNullOrEmpty(textTime.Text))) {
string Time = textTime.Text;
tmSend.Interval = int.Parse(Time) * 1000; tmSend.Start(); } else {
string StrSend_1 = StrSend.Trim(); //去掉前后的空格
//string StrSend_2 = StrSend_1.Replace(\ //string StrSend_3 = string.Join(\Regex.Matches(StrSend_2, @\
string[] StrArray = StrSend_1.Split(' '); //用空格符隔开字符串数组
var Str_Send = new List
// int byteBufferLength = StrArray.Length; //数组StrArray的长度
for (int i = 0; i < StrArray.Length; i++) {
if (StrArray[i].Trim() == \ {
//byteBufferLength--; continue; } else
{
if (StrArray[i].Length < 3) {
Str_Send.Add(StrArray[i]); } else {
Str_Send.AddRange(Devide(StrArray[i])); } } }
int byteBufferLength = Str_Send.Count;
byte[] byteBuffer = new byte[byteBufferLength]; int ii = 0;
for (int i = 0; i < Str_Send.Count; i++) {
//Byte[] bytesOfStr = Encoding.Default.GetBytes(StrArray[i]); int decNum = 0;
//if (StrArray[i] == \ //{
// continue; //} //else //{
//decNum = Convert.ToInt32(Str_Send[i], 16); //把字符串转成16进制数
//byteBuffer[ii] = Convert.ToByte(decNum); //} try {
decNum = Convert.ToInt32(Str_Send[i], 16); //把字符串转成16进制数
byteBuffer[ii] = Convert.ToByte(decNum); }
catch (System.Exception ex) {
MessageBox.Show(\输入错误!\提示\ return; } ii++; }
sp1.Write(byteBuffer, 0, byteBuffer.Length);//从第0个开始写入byteBuffer,长度bytebuffer的长度。 }
} } }
private List
var ret = Regex.Matches(str,
@\ return ret;
}
private void tmSend_Tick(object sender, EventArgs e) {
string StrSend = memoSend.Text; if (rbtnSendStr.Checked) {
sp1.Write(StrSend); } else {
string StrSend_1 = StrSend.Trim(); //去掉前后的空格
string[] StrArray = StrSend_1.Split(' '); //用空格符隔开字符串数组
var Str_Send = new List
for (int i = 0; i < StrArray.Length; i++) {
if (StrArray[i].Trim() == \ {
continue; } else {
if (StrArray[i].Length < 3) {
Str_Send.Add(StrArray[i]); } else {
Str_Send.AddRange(Devide(StrArray[i]));
} } }
int byteBufferLength = Str_Send.Count;
byte[] byteBuffer = new byte[byteBufferLength]; int ii = 0;
for (int i = 0; i < Str_Send.Count; i++) {
int decNum = 0;
try {
decNum = Convert.ToInt32(Str_Send[i], 16); //把字符串转成16进制数
byteBuffer[ii] = Convert.ToByte(decNum); }
catch (System.Exception ex) {
MessageBox.Show(\输入错误!\提示\ return; } ii++; }
sp1.Write(byteBuffer, 0, byteBuffer.Length);//从第0个开始写入byteBuffer,长度bytebuffer的长度。 } }
void sp1_DataReceived(object sender, SerialDataReceivedEventArgs e) {
byte[] byteRead = new byte[sp1.BytesToRead];
if (rbtnRceStr.Checked) {
memoRcv.Text += sp1.ReadExisting();
memoRcv.SelectionStart = memoRcv.Text.Length; memoRcv.ScrollToCaret(); } else {
if (rbtnRce16.Checked) {
Byte[] receivedData = new Byte[sp1.BytesToRead]; //创建接收字节数组
sp1.Read(receivedData, 0, receivedData.Length);
string strRcv = null;
for (int i = 0; i < receivedData.Length; i++) {
strRcv += receivedData[i].ToString(\ }
memoRcv.Text += strRcv;
memoRcv.SelectionStart = memoRcv.Text.Length; memoRcv.ScrollToCaret(); }
} }
private void Stop_Click(object sender, EventArgs e) //退出接收数据 {
tmSend.Stop(); }
private void Clear_Click(object sender, EventArgs e) //清空接收数据 {
memoRcv.Text = \ }
private void Time_Press(object sender, KeyPressEventArgs e) {
if (!Char.IsLetterOrDigit(e.KeyChar)) {
e.Handled = true; } else {
e.Handled = false; } }
private void TextTime_KeyPress(object sender, KeyPressEventArgs e) //限制定时发送时间只能输入数字 {
if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar != (char)8) {
MessageBox.Show(\请输入数字\提示\ e.Handled = true;
} else {
e.Handled = false; } }
private void memoSend_KeyPress(object sender, KeyPressEventArgs e) {
}
private void btnClose_Click(object sender, EventArgs e) //关闭串口
{
if (sp1.IsOpen) {
sp1.Close();
labSerial.Text = \串口号:\ labState.Text = \状态:关闭\ } }
private void Send16_KeyPress(object sender, KeyPressEventArgs e) {
//string Send16 = memoRcv.Text;
//byte[] buff = new byte[Send16.Length / 2]; //int index = 0;
//for (int i = 0; i < Send16.Length; i += 2) //{
// buff[index] = Convert.ToByte(Send16.Substring(i, 2), 16); // ++index; //}
//string result = Encoding.Default.GetString(buff); //// MessageBox.Show(result); //memoRcv.Text = result; }
private void SendStr_KeyPress(object sender, KeyPressEventArgs e) {
//string SendStr = memoRcv.Text;
//byte[] ba = System.Text.ASCIIEncoding.Default.GetBytes(SendStr); //StringBuilder sb = new StringBuilder(); //foreach (byte b in ba) //{
// sb.Append(b.ToString(\
//}
//// MessageBox.Show(sb.ToString()); //memoRcv.Text = sb.ToString(); }
private void rbtnSend16_CheckedChanged(object sender, EventArgs e) {
StateChanged_16 = 1;
StateChanged_Ascii = 0;
string StrSend = memoSend.Text;
if (Temp_memoSend==StrSend.Trim()&& Flag == 1) {
memoSend.Text = Temp; Flag = 0; } else {
memoSend.Text = \
foreach (char S in StrSend) {
memoSend.Text += ((byte)S).ToString(\ } } }
private void rbtnSendStr_CheckedChanged(object sender, EventArgs e) {
if (StateChanged_16 == 1) { Flag = 1; }
StateChanged_16 = 0; StateChanged_Ascii = 1;
string StrSend = memoSend.Text; Temp = memoSend.Text;
string StrSend_1 = StrSend.Trim(); //去掉前后的空格 string[] StrArray = StrSend_1.Split(' '); //用空格符隔开字符串数组
var Str_Send = new List
for (int i = 0; i < StrArray.Length; i++) {
if (StrArray.Length == 0) {
break; } else {
if (StrArray[i].Trim() == \ { continue; } else {
if (StrArray[i].Length < 3) {
Str_Send.Add(StrArray[i]); } else {
Str_Send.AddRange(Devide(StrArray[i])); } } }
int byteBufferLength = Str_Send.Count;
byte[] byteBuffer = new byte[byteBufferLength]; int ii = 0;
for (int j = 0; j < Str_Send.Count; j++) {
int decNum = 0; try {
decNum = Convert.ToInt32(Str_Send[j], 16); //把字符串转成16进制数
if (decNum >= 48 && decNum <= 57 || decNum >= 65 && decNum <= 90 || decNum >= 97 && decNum <= 122)//除了数字和字母,其他都转成问号 {
byteBuffer[ii] = Convert.ToByte(decNum); } else {
byteBuffer[ii] = Convert.ToByte(63); } }
catch (System.Exception ex) {
MessageBox.Show(\输入错误!\提示\ rbtnSendStr.Checked = false; rbtnSend16.Checked = true;
memoSend.Text = StrSend; return;
} ii++; }
memoSend.Text = \
foreach (byte b in byteBuffer) {
memoSend.Text += (char)b; }
Temp_memoSend =memoSend.Text; } } } }
正在阅读:
串口调试助手源代码06-04
高二物理上学期月考试题07-12
关于电子磁罗盘和双GPS组合两种定向方式的比较分析07-06
《金版学案》2017-2018年高中化学人教版选修5练习: 章末系统总结5 Word版含解析09-03
管理沟通作业11-28
第5章流动阻力和水头损失03-14
史学导论复习题10-24
3000t选矿厂设计说明书01-29
热处理炉课程设计09-28
新建筑对老建筑遮光是否应当赔偿05-25
- 多层物业服务方案
- (审判实务)习惯法与少数民族地区民间纠纷解决问题(孙 潋)
- 人教版新课标六年级下册语文全册教案
- 词语打卡
- photoshop实习报告
- 钢结构设计原理综合测试2
- 2014年期末练习题
- 高中数学中的逆向思维解题方法探讨
- 名师原创 全国通用2014-2015学年高二寒假作业 政治(一)Word版
- 北航《建筑结构检测鉴定与加固》在线作业三
- XX县卫生监督所工程建设项目可行性研究报告
- 小学四年级观察作文经典评语
- 浅谈110KV变电站电气一次设计-程泉焱(1)
- 安全员考试题库
- 国家电网公司变电运维管理规定(试行)
- 义务教育课程标准稿征求意见提纲
- 教学秘书面试技巧
- 钢结构工程施工组织设计
- 水利工程概论论文
- 09届九年级数学第四次模拟试卷
- 串口
- 源代码
- 调试
- 助手
- 2012附中分班考试数学试卷
- 精选苏教版一年级语文下册第二单元测试卷
- 西北局研究综述
- 数字电路与逻辑设计
- 铸钢的金相组织及检验
- 采用季节指数法预测油井清防蜡费用
- 公路工程施工测量技术交底
- 乡镇安全生产工作领导组织职责
- 幼儿心理学填空题
- 《中国共产党党和国家机关基层组织工作条例》测试题
- 体验经济与营销
- 上海牛津英语7BU5试题
- 2008年椒江区农村街道小学五年级抽查测试
- 辽宁省沈阳二中2012届高三上学期10月月考(地理) - 图文
- 税收筹划练习题
- 西南大学2016年12月大作业A卷答案(现代汉语语法与修辞1159)
- 关于强化企业通讯员队伍能力建设的实践与思考
- 五四红旗团支部 - 图文
- 39万科成本优化与控制作业指引
- 电力系统通信技术结课论文 doc