串口调试助手源代码._第1页
串口调试助手源代码._第2页
串口调试助手源代码._第3页
串口调试助手源代码._第4页
串口调试助手源代码._第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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 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

3、 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(本机没有串口!, e

4、rror); 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); /订阅委托 privat

5、e 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 = serialNam

6、e; /设置各参数 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 1: sp1.StopBits = StopBits.One; break; case 1.5: sp

7、1.StopBits = StopBits.One; break; case 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: Messa

8、geBox.Show(Error:参数不正确!, Error); break; if (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 send

9、er, EventArgs e) / / if (sp1.IsOpen) / / sp1.Close(); / 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; else if (rbtnSendStr.Checked) /字符串

10、发送 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 T

11、ime = 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, .|.).Cast().ToList(); string StrArray = StrSend_1.Split( ); /用空格符隔开字

12、符串数组 var Str_Send = new List(); / int byteBufferLength = StrArray.Length; /数组StrArray的长度 for (int i = 0; i StrArray.Length; i+) if (StrArrayi.Trim() = ) /byteBufferLength-; continue; else if (StrArrayi.Length 3) Str_Send.Add(StrArrayi); else Str_Send.AddRange(Devide(StrArrayi); int byteBufferLength

13、= Str_Send.Count; byte byteBuffer = new bytebyteBufferLength; int ii = 0; for (int i = 0; i Str_Send.Count; i+) /Byte bytesOfStr = Encoding.Default.GetBytes(StrArrayi); int decNum = 0; /if (StrArrayi = ) / / continue; / /else / /decNum = Convert.ToInt32(Str_Sendi, 16); /把字符串转成16进制数 /byteBufferii = C

14、onvert.ToByte(decNum); / try decNum = Convert.ToInt32(Str_Sendi, 16); /把字符串转成16进制数 byteBufferii = Convert.ToByte(decNum); catch (System.Exception ex) MessageBox.Show(输入错误!, 提示); return; ii+; sp1.Write(byteBuffer, 0, byteBuffer.Length);/从第0个开始写入byteBuffer,长度bytebuffer的长度。 private List Devide(string s

15、tr) var ret = Regex.Matches(str, .|.).Cast().ToList().ConvertAll(s = s.Value.ToString(); 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 = St

16、rSend_1.Split( ); /用空格符隔开字符串数组 var Str_Send = new List(); for (int i = 0; i StrArray.Length; i+) if (StrArrayi.Trim() = ) continue; else if (StrArrayi.Length 3) Str_Send.Add(StrArrayi); else Str_Send.AddRange(Devide(StrArrayi); int byteBufferLength = Str_Send.Count; byte byteBuffer = new bytebyteBuf

17、ferLength; int ii = 0; for (int i = 0; i Str_Send.Count; i+) int decNum = 0; try decNum = Convert.ToInt32(Str_Sendi, 16); /把字符串转成16进制数 byteBufferii = Convert.ToByte(decNum); catch (System.Exception ex) MessageBox.Show(输入错误!, 提示); return; ii+; sp1.Write(byteBuffer, 0, byteBuffer.Length);/从第0个开始写入byte

18、Buffer,长度bytebuffer的长度。 void sp1_DataReceived(object sender, SerialDataReceivedEventArgs e) byte byteRead = new bytesp1.BytesToRead; if (rbtnRceStr.Checked) memoRcv.Text += sp1.ReadExisting(); memoRcv.SelectionStart = memoRcv.Text.Length; memoRcv.ScrollToCaret(); else if (rbtnRce16.Checked) Byte rec

19、eivedData = new Bytesp1.BytesToRead; /创建接收字节数组 sp1.Read(receivedData, 0, receivedData.Length); string strRcv = null; for (int i = 0; i receivedData.Length; i+) strRcv += receivedDatai.ToString(X2); memoRcv.Text += strRcv; memoRcv.SelectionStart = memoRcv.Text.Length; memoRcv.ScrollToCaret(); private

20、 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 Te

21、xtTime_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 sende

22、r, EventArgs e) /关闭串口 if (sp1.IsOpen) sp1.Close(); labSerial.Text = 串口号: + sp1.PortName; labState.Text = 状 态:关闭; private void Send16_KeyPress(object sender, KeyPressEventArgs e) /string Send16 = memoRcv.Text; /byte buff = new byteSend16.Length / 2; /int index = 0; /for (int i = 0; i Send16.Length; i

23、 += 2) / / buffindex = 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.Tex

24、t.ASCIIEncoding.Default.GetBytes(SendStr); /StringBuilder sb = new StringBuilder(); /foreach (byte b in ba) / / sb.Append(b.ToString(x); / / MessageBox.Show(sb.ToString(); /memoRcv.Text = sb.ToString(); private void rbtnSend16_CheckedChanged(object sender, EventArgs e) StateChanged_16 = 1; StateChan

25、ged_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(X2); 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.Te

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论