串口调试助手源代码文档格式.docx

上传人:b****5 文档编号:19056978 上传时间:2023-01-03 格式:DOCX 页数:17 大小:54.60KB
下载 相关 举报
串口调试助手源代码文档格式.docx_第1页
第1页 / 共17页
串口调试助手源代码文档格式.docx_第2页
第2页 / 共17页
串口调试助手源代码文档格式.docx_第3页
第3页 / 共17页
串口调试助手源代码文档格式.docx_第4页
第4页 / 共17页
串口调试助手源代码文档格式.docx_第5页
第5页 / 共17页
点击查看更多>>
下载资源
资源描述

串口调试助手源代码文档格式.docx

《串口调试助手源代码文档格式.docx》由会员分享,可在线阅读,更多相关《串口调试助手源代码文档格式.docx(17页珍藏版)》请在冰豆网上搜索。

串口调试助手源代码文档格式.docx

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)//停止位

case"

1"

:

sp1.StopBits=StopBits.One;

break;

1.5"

2"

sp1.StopBits=StopBits.Two;

default:

Error:

参数不正确!

Error"

switch(cbParity.Text)//校验位

无"

sp1.Parity=Parity.None;

奇校验"

sp1.Parity=Parity.Odd;

偶校验"

sp1.Parity=Parity.Even;

if(sp1.IsOpen==true)

sp1.Open();

//btnOpen.Enabled=false;

labSerial.Text="

串口号:

+sp1.PortName;

labState.Text="

状态:

打开"

;

catch(System.Exceptionex)

+ex.Message,"

//privatevoidCloseSerial_Click(objectsender,EventArgse)

//{

//if(sp1.IsOpen)

//{

//sp1.Close();

//labSerial.Text="

//labState.Text="

关闭"

//}

//}

privatevoidSend_Click(objectsender,EventArgse)

stringStrSend=memoSend.Text;

sp1.IsOpen)

请先打开串口!

else

if(rbtnSendStr.Checked)//字符串发送

if(ckTimeSend.Checked&

&

!

(string.IsNullOrEmpty(textTime.Text)))//判断是否定时发送数据

stringTime=textTime.Text;

tmSend.Interval=int.Parse(Time)*1000;

//时间单位是秒,化成毫秒单位

tmSend.Start();

//开启定时器

sp1.Write(StrSend);

//写入数据

else//十六进制发送

(string.IsNullOrEmpty(textTime.Text)))

stringStrSend_1=StrSend.Trim();

//去掉前后的空格

//stringStrSend_2=StrSend_1.Replace("

"

//stringStrSend_3=string.Join("

Regex.Matches(StrSend_2,@"

..|."

).Cast<

Match>

().ToList());

string[]StrArray=StrSend_1.Split('

'

//用空格符隔开字符串数组

varStr_Send=newList<

string>

();

//intbyteBufferLength=StrArray.Length;

//数组StrArray的长度

for(inti=0;

i<

StrArray.Length;

i++)

if(StrArray[i].Trim()=="

//byteBufferLength--;

continue;

if(StrArray[i].Length<

3)

Str_Send.Add(StrArray[i]);

Str_Send.AddRange(Devide(StrArray[i]));

intbyteBufferLength=Str_Send.Count;

byte[]byteBuffer=newbyte[byteBufferLength];

intii=0;

Str_Send.Count;

//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);

decNum=Convert.ToInt32(Str_Send[i],16);

byteBuffer[ii]=Convert.ToByte(decNum);

输入错误!

提示"

ii++;

sp1.Write(byteBuffer,0,byteBuffer.Length);

//从第0个开始写入byteBuffer,长度bytebuffer的长度。

privateList<

Devide(stringstr)

varret=Regex.Matches(str,@"

().ToList().ConvertAll(s=>

s.Value.ToString());

returnret;

privatevoidtmSend_Tick(objectsender,EventArgse)

if(rbtnSendStr.Checked)

voidsp1_DataReceived(objectsender,SerialDataReceivedEventArgse)

byte[]byteRead=newbyte[sp1.BytesToRead];

if(rbtnRceStr.Checked)

memoRcv.Text+=sp1.ReadExisting();

memoRcv.SelectionStart=memoRcv.Text.Length;

memoRcv.ScrollToCaret();

if(rbtnRce16.Checked)

Byte[]receivedData=newByte[sp1.BytesToRead];

//创建接收字节数组

sp1.Read(receivedData,0,receivedData.Length);

stringstrRcv=null;

receivedData.Length;

strRcv+=receivedData[i].ToString("

X2"

memoRcv.Text+=strRcv;

privatevoidStop_Click(objectsender,EventArgse)//退出接收数据

tmSend.Stop();

privatevoidClear_Click(objectsender,EventArgse)//清空接收数据

memoRcv.Text="

privatevoidTime_Press(objectsender,KeyPressEventArgse)

Char.IsLetterOrDigit(e.KeyChar))

e.Handled=true;

e.Handled=false;

privatevoidTextTime_KeyPress(objectsender,KeyPressEventArgse)//限制定时发送时间只能输入数字

(Char.IsNumber(e.KeyChar))&

e.KeyChar!

=(char)8)

请输入数字"

privatevoidmemoSend_KeyPress(objectsender,KeyPressEventArgse)

privatevoidbtnClose_Click(objectsender,EventArgse)//关闭串口

if(sp1.IsOpen)

privatevoidSend16_KeyPress(objectsender,KeyPressEventArgse)

//stringSend16=memoRcv.Text;

//byte[]buff=newbyte[Send16.Length/2];

//intindex=0;

//for(inti=0;

Send16.Length;

i+=2)

//buff[index]=Convert.ToByte(Send16.Substring(i,2),16);

//++index;

//stringresult=Encoding.Default.GetString(buff);

////MessageBox.Show(result);

//memoRcv.Text=result;

privatevoidSendStr_KeyPress(objectsender,KeyPressEventArgse)

//stringSendStr=memoRcv.Text;

//byte[]ba=System.Text.ASCIIEncoding.Default.GetBytes(SendStr);

//StringBuildersb=newStringBuilder();

//foreach(bytebinba)

//sb.Append(b.ToString("

x"

));

////MessageBox.Show(sb.ToString());

//memoRcv.Text=sb.ToString();

privatevoidrbtnSend16_CheckedChanged(objectsender,EventArgse)

StateChanged_16=1;

StateChanged_Ascii=0;

if(Temp_memoSend==StrSend.Trim()&

Flag==1)

memoSend.Text=Temp;

Flag=0;

memoSend.Text="

foreach(charSinStrSend)

memoSend.Text+=((byte)S).ToString("

privatevoidrbtnSendStr_CheckedChanged(objectsender,EventArgse)

if(StateChanged_16==1)

{

Flag=1;

StateChanged_16=0;

StateChanged_Ascii=1;

Temp=memoSend.Text;

//去掉前后的空格

if(StrArray.Length==0)

for(intj=0;

j<

j++)

decNum=Convert.ToInt32(Str_Send[j],16);

if(decNum>

=48&

decNum<

=57||decNum>

=65&

=90||decNum>

=97&

=122)//除了数字和字母,其他都转成问号

byteBuffer[ii]=Convert.To

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

当前位置:首页 > 解决方案 > 解决方案

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1