串口调试助手源代码

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

串口调试助手预源代码usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;usingSystem.IO;usingSystem.IO.Ports;usingSystem.Threading;usingDevExpress.XtraEditors;usingSystem.Text.RegularExpressions;namespaceWindowsFormsApplication3{publicpartialclassForm1:XtraForm{SerialPortsp1=newSerialPort();intFlag=0;intStateChanged=0;intStateChanged_Ascii=1;intStateChanged_16=0;stringTemp=null;stringTemp_memoSend=null;privatevoidmemoSend_EditValueChanged(objectsender,EventArgse){}publicForm1(){InitializeComponent();sp1.DataReceived+=sp1_DataReceived;}privatevoidForm1_Load(objectsender,EventArgse){this.MaximizeBox=false;//检查是否有串口string[]str=SerialPort.GetPortNames();if(str==null){MessageBox.Show(本机没有串口!,error);return;}//添加串口项目foreach(stringportinSystem.IO.Ports.SerialPort.GetPortNames()){cbSerial.Properties.Items.Add(port);}//串口设置默认选择项cbSerial.SelectedIndex=0;Control.CheckForIllegalCrossThreadCalls=false;sp1.DataReceived+=newSerialDataReceivedEventHandler(sp1_DataReceived);//订阅委托}privatevoidcomboBoxEdit1_SelectedIndexChanged(objectsender,EventArgse){}privatevoidOpenSerial_Click(objectsender,EventArgse){sp1.Close();//SerialPortsp1=newSerialPort();if(!sp1.IsOpen)//串口是关闭的,设置参数,打开串口{try{//获取串口号stringserialName=cbSerial.SelectedItem.ToString();sp1.PortName=serialName;//设置各参数stringstrBaudRate=cbBaudRate.Text;stringstrDateBits=cbDataBits.Text;stringstrStopBits=cbStop.Text;sp1.BaudRate=Convert.ToInt32(strBaudRate);//波特率sp1.DataBits=Convert.ToInt32(strDateBits);//数据位switch(cbStop.Text)//停止位{case1:sp1.StopBits=StopBits.One;break;case1.5:sp1.StopBits=StopBits.One;break;case2: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(sp1.IsOpen==true){sp1.Close();}sp1.Open();//btnOpen.Enabled=false;labSerial.Text=串口号:+sp1.PortName;labState.Text=状态:打开;}catch(System.Exceptionex){MessageBox.Show(Error:+ex.Message,Error);return;}}}//privatevoidCloseSerial_Click(objectsender,EventArgse)//{//if(sp1.IsOpen)//{//sp1.Close();//labSerial.Text=串口号:+sp1.PortName;//labState.Text=状态:关闭;//}//}privatevoidSend_Click(objectsender,EventArgse){stringStrSend=memoSend.Text;if(!sp1.IsOpen){MessageBox.Show(请先打开串口!,Error);return;}else{if(rbtnSendStr.Checked)//字符串发送{if(ckTimeSend.Checked&&!(string.IsNullOrEmpty(textTime.Text)))//判断是否定时发送数据{stringTime=textTime.Text;tmSend.Interval=int.Parse(Time)*1000;//时间单位是秒,化成毫秒单位tmSend.Start();//开启定时器}else{sp1.Write(StrSend);//写入数据}}else//十六进制发送{if(ckTimeSend.Checked&&!(string.IsNullOrEmpty(textTime.Text))){stringTime=textTime.Text;tmSend.Interval=int.Parse(Time)*1000;tmSend.Start();}else{stringStrSend_1=StrSend.Trim();//去掉前后的空格//stringStrSend_2=StrSend_1.Replace(,);//stringStrSend_3=string.Join(,Regex.Matches(StrSend_2,@..|.).CastMatch().ToList());string[]StrArray=StrSend_1.Split('');//用空格符隔开字符串数组varStr_Send=newListstring();//intbyteBufferLength=StrArray.Length;//数组StrArray的长度for(inti=0;iStrArray.Length;i++){if(StrArray[i].Trim()==){//byteBufferLength--;continue;}else{if(StrArray[i].Length3){Str_Send.Add(StrArray[i]);}else{Str_Send.AddRange(Devide(StrArray[i]));}}}intbyteBufferLength=Str_Send.Count;byte[]byteBuffer=newbyte[byteBufferLength];intii=0;for(inti=0;iStr_Send.Count;i++){//Byte[]bytesOfStr=Encoding.Default.GetBytes(StrArray[i]);intdecNum=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.Exceptionex){MessageBox.Show(输入错误!,提示);return;}ii++;}sp1.Write(byteBuffer,0,byteBuffer.Length);//从第0个开始写入byteBuffer,长度bytebuffer的长度。}}}}privateListstringDevide(stringstr){varret=Regex.Matches(str,@..|.).CastMatch().ToList().ConvertAll(s=s.Value.ToString());returnret;}privatevoidtmSend_Tick(objectsender,EventArgse){stringStrSend=memoSend.Text;if(rbtnSendStr.Checked){sp1.Write(StrSend);}else{stringStrSend_1=StrSend.Trim();//去掉前后的空格string[]StrArray=StrSend_1.Split('');//用空格符隔开字符串数组varStr_Send=newListstring();for(inti=0;iStrArray.Length;i++){if(StrArray[i].Trim()==){continue;}else{if(StrArray[i].Length3){Str_Send.Add(StrArray[i]);}else{Str_Send.AddRange(Devide(StrArray[i]));}}}intbyteBufferLength=Str_Send.Count;byte[]byteBuffer=newbyte[byteBufferLength];intii=0;for(inti=0;iStr_Send.Count;i++){intdecNum=0;try{decNum=Convert.ToInt32(Str_Send[i],16);//把字符串转成16进制数byteBuffer[ii]=Convert.ToByte(decNum);}catch(System.Exceptionex){MessageBox.Show(输入错误!,提示);return;}ii++;}sp1.Write(byteBuffer,0,byteBuffer.Length);//从第0个开始写入byteBuffer,长度bytebuffer的长度。}}voidsp1_DataReceived(objectsender,SerialDataReceivedEventArgse){byte[]byteRead=newbyte[sp1.BytesToRead];if(rbtnRceStr.Checked){memoRcv.Text+=sp

1 / 13
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功