收藏 分享(赏)

串口调试助手源代码.doc

上传人:精品资料 文档编号:11237395 上传时间:2020-02-22 格式:DOC 页数:13 大小:62.56KB
下载 相关 举报
串口调试助手源代码.doc_第1页
第1页 / 共13页
串口调试助手源代码.doc_第2页
第2页 / 共13页
串口调试助手源代码.doc_第3页
第3页 / 共13页
串口调试助手源代码.doc_第4页
第4页 / 共13页
串口调试助手源代码.doc_第5页
第5页 / 共13页
点击查看更多>>
资源描述

1、串口调试助手预源代码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.XtraEdi

2、tors;using System.Text.RegularExpressions;namespace WindowsFormsApplication3public partial class Form1 : XtraFormSerialPort 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

3、_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(“本机没有串口!“, “error“);return;/

4、添加串口项目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_S

5、electedIndexChanged(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 = c

6、bBaudRate.Text;string strDateBits = cbDataBits.Text;string strStopBits = cbStop.Text;sp1.BaudRate = Convert.ToInt32(strBaudRate);/波特率sp1.DataBits = Convert.ToInt32(strDateBits);/数据位switch (cbStop.Text) /停止位case “1“:sp1.StopBits = StopBits.One;break;case “1.5“:sp1.StopBits = StopBits.One;break;case “

7、2“:sp1.StopBits = StopBits.Two;break;default:MessageBox.Show(“Error:参数不正确!“, “Error“);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(“Error:参数不正确!“, “Error“);break;if

8、(sp1.IsOpen = true)sp1.Close();sp1.Open();/btnOpen.Enabled = false;labSerial.Text = “串口号:“ + sp1.PortName;labState.Text = “状 态:打开“;catch (System.Exception ex)MessageBox.Show(“Error:“ + ex.Message, “Error“);return;/private void CloseSerial_Click(object sender, EventArgs e)/ if (sp1.IsOpen)/ / sp1.Clo

9、se();/ labSerial.Text = “串口号:“ + sp1.PortName;/ labState.Text = “状 态:关闭“;/ /private void Send_Click(object sender, EventArgs e)string StrSend = memoSend.Text;if (!sp1.IsOpen)MessageBox.Show(“请先打开串口!“, “Error“);return;elseif (rbtnSendStr.Checked) /字符串发送if (ckTimeSend.Checked tmSend.Interval = int.Par

10、se(Time) * 1000;/时间单位是秒,化成毫秒单位tmSend.Start(); /开启定时器elsesp1.Write(StrSend); /写入数据else /十六进制发送if (ckTimeSend.Checked tmSend.Interval = int.Parse(Time) * 1000;tmSend.Start();elsestring StrSend_1 = StrSend.Trim(); /去掉前后的空格/string StrSend_2 = StrSend_1.Replace(“ “, “);/string StrSend_3 = string.Join(“ “

11、, Regex.Matches(StrSend_2, “|.“).Cast().ToList();string StrArray = StrSend_1.Split( ); /用空格符隔开字符串数组var Str_Send = new List();/ int byteBufferLength = StrArray.Length; /数组StrArray的长度for (int i = 0; i Devide(string str)var ret = Regex.Matches(str, “|.“).Cast().ToList().ConvertAll(s = s.Value.ToString(

12、);return ret;private void tmSend_Tick(object sender, EventArgs e)string StrSend = memoSend.Text;if (rbtnSendStr.Checked)sp1.Write(StrSend);elsestring StrSend_1 = StrSend.Trim(); /去掉前后的空格string StrArray = StrSend_1.Split( ); /用空格符隔开字符串数组var Str_Send = new List();for (int i = 0; i ();for (int i = 0; i = 48 elsebyteBufferii = 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;

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 企业管理 > 管理学资料

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报