C#串口通信代码

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

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

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

资源描述

很久以前C#编的串口小程序,希望对初学C#串口编程的童鞋有所帮助~运行界面如下图:以下是源代码:usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.IO;usingSystem.IO.Ports;namespaceSerialPortTest_1{publicpartialclassFormSerialPortTest1:Form{privateStreamReadersRead;publicintiTextbox2=0;publicFormSerialPortTest1(){InitializeComponent();}privatevoidForm1_Load(objectsender,EventArgse){string[]str=SerialPort.GetPortNames();if(str==null){MessageBox.Show(本机没有串口!,Error);return;}comboBox1.Items.AddRange(str);comboBox1.SelectedIndex=0;comboBox2.SelectedIndex=6;this.toolStripStatusLabel1.Text=端口号:端口未打开;this.toolStripStatusLabel2.Text=波特率:端口未打开;this.toolStripStatusLabel3.Text=数据位:端口未打开;this.toolStripStatusLabel4.Text=停止位:端口未打开;}privatevoidbutton1_Click(objectsender,EventArgse){Stringstr1=comboBox1.Text;Stringstr2=comboBox2.Text;Stringstr3=comboBox3.Text;Stringstr4=comboBox4.Text;Int32int2=Convert.ToInt32(str2);Int32int3=Convert.ToInt32(str3);try{if(str1==null){MessageBox.Show(请先选择串口!,Error);return;}serialPort1.PortName=str1;serialPort1.BaudRate=int2;serialPort1.DataBits=int3;switch(comboBox4.Text){case1:serialPort1.StopBits=StopBits.One;break;case1.5:serialPort1.StopBits=StopBits.OnePointFive;break;case2:serialPort1.StopBits=StopBits.Two;break;default:MessageBox.Show(Error:参数不正确,Error);break;}if(serialPort1.IsOpen==true){serialPort1.Close();}serialPort1.Open();MessageBox.Show(串口打开成功!,str1);this.toolStripStatusLabel1.Text=端口号:+serialPort1.PortName+;this.toolStripStatusLabel2.Text=波特率:+serialPort1.BaudRate+;this.toolStripStatusLabel3.Text=数据位:+serialPort1.DataBits+;this.toolStripStatusLabel4.Text=停止位:+serialPort1.StopBits+;button1.Enabled=false;comboBox1.Enabled=false;comboBox2.Enabled=false;comboBox3.Enabled=false;comboBox4.Enabled=false;}catch(Exceptioner){MessageBox.Show(Error:+er.Message,Error);return;}}privatevoidbutton2_Click(objectsender,EventArgse){button1.Enabled=true;comboBox1.Enabled=true;comboBox2.Enabled=true;comboBox3.Enabled=true;comboBox4.Enabled=true;serialPort1.Close();this.toolStripStatusLabel1.Text=端口号:端口未打开;this.toolStripStatusLabel2.Text=波特率:端口未打开;this.toolStripStatusLabel3.Text=数据位:端口未打开;this.toolStripStatusLabel4.Text=停止位:端口未打开;}privatevoid退出XToolStripMenuItem_Click(objectsender,EventArgse){Application.Exit();}//发送privatevoidbutton3_Click(objectsender,EventArgse){if(button1.Enabled==true){MessageBox.Show(请先打开串口!,Error);return;}Stringstr1;str1=textBox1.Text;byte[]data=Encoding.Default.GetBytes(str1);if(checkBox1.Checked==true){for(inti=0;idata.Length;i++){bytetemp=data[i];stringtempHex=temp.ToString(X2)+;serialPort1.Write(tempHex);}}else{serialPort1.Write(data,0,data.Length);}}//使用Control.InvokepublicdelegatevoidDeleUpdateTextbox(stringdataRe);privatevoidserialPort1_DataReceived(objectsender,SerialDataReceivedEventArgse){stringdataRe;byte[]byteRead=newbyte[serialPort1.BytesToRead];DeleUpdateTextboxdeleupdatetextbox=newDeleUpdateTextbox(UpdateTextbox);serialPort1.Read(byteRead,0,byteRead.Length);if(checkBox2.Checked==false){dataRe=Encoding.Default.GetString(byteRead);textBox2.Invoke(deleupdatetextbox,dataRe);}else{for(inti=0;ibyteRead.Length;i++){bytetemp=byteRead[i];dataRe=temp.ToString(X2)+;textBox2.Invoke(deleupdatetextbox,dataRe);}}}privatevoidUpdateTextbox(stringdataRe){if(iTextbox2==0){this.textBox2.Text=dataRe;iTextbox2++;}else{textBox2.AppendText(dataRe);}}privatevoid关于AToolStripMenuItem_Click(objectsender,EventArgse){MessageBox.Show(\n敢攀世界科技高峰\n\n版本号V0.2\n\n,爱岗敬业以德为尚);}privatevoidbutton4_Click(objectsender,EventArgse){Stringfilename;openFileDialog1.FileName=;openFileDialog1.ShowDialog();filename=openFileDialog1.FileName;if(filename==){MessageBox.Show(请选择要发送的文件!,Error);return;}textBox3.Text=filename;if(filename!=null){sRead=newStreamReader(filename);}button5.Enabled=true;}privatevoidbutton5_Click(objectsender,EventArgse){stringstr3=textBox3.Text;if(button1.Enabled==true){MessageBox.Show(请先打开串口!,Error);return;}if(str3==){MessageBox.Show(请选择要发送的文件!,Error);return;}stringstr1;str1=textBox4.Text;timer1.Interval=Convert.ToInt32(str1);timer1.Start();button2.Enabled=false;button3.Enabled=false;button4.Enabled=false;button5.Enabled=false;textBox1.Enabled=false;textBox3.Enabled=false;textBox4.Enabled=false;}privatevoidtimer1_Tick(objectsender,EventArgse){stringstr1;str1=sRead.ReadLine();if(str1==null){timer1.Stop();sRead.Close();MessageBox.Show(发送完毕!,NICE);button2.Enabled=true;button3.Enabled=true;button4.Enabled=true;textBox1.Enabled=true;textBox3.Enabled=true;textBox4.Enabled=true;return;}byte[]data=Encoding.Default.GetBytes(str1);serialPort1.Write(data,0,data.Length);}privatevoidbutton6_Click(objectsender,EventArgse){timer1.Stop();button2.Enabled=true;button3.Enabled=true;button4.Enabled=true;textBox1.Enabled=true;textBox3.Enabled=true;textBox4.Enabled=true;}privatevoidtextBox1_KeyDown(objectsender,KeyEventArgse){if(e.KeyValue==13){if(button1.Enabled==true){MessageBox.Show(请先打开串口!,Error);return;}Stringstr1;str1=textB

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

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

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

×
保存成功