資源簡介
C#實現(xiàn)打開串口,關(guān)閉串口,利用串口接收和發(fā)送數(shù)據(jù),并且能把數(shù)據(jù)實時顯示在屏幕上,實現(xiàn)世博功能!

代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.ComponentModel;
using?System.Data;
using?System.Drawing;
using?System.Text;
using?System.Windows.Forms;
using?System.IO.Ports;
using?System.IO;
namespace?RFCOMM
{
????public?partial?class?Form1?:?Form
????{
????????public?Form1()
????????{
????????????InitializeComponent();
????????}
????????public?SerialPort?Port=new?SerialPort();
????????//public?delegate?void?HandleInterfaceUpdataDelegate(string?text);
????????//private?HandleInterfaceUpdataDelegate?interfaceUpdataHandle;
?????????
????????private?void?Form1_Load(object?sender?EventArgs?e)
????????{
???????????//?this.CreatFile();//創(chuàng)建文件
????????????//this.WriteData();
????????}
///////////////////////////////////////////////////////////////////////////////////////
????????//沒用到
????????private?void?portnum_comboBox_SelectedIndexChanged(object?sender?EventArgs?e)
????????{
????????}
????????private?void?data_textBox_TextChanged(object?sender?EventArgs?e)
????????{
????????}
????????private?void?baud_comboBox_SelectedIndexChanged(object?sender?EventArgs?e)
????????{
????????}
///////////////////////////////////////////////////////////////////////////////////
///?
///?啟動串口
///?
///?
///?
????????private?void?listen_button_Click(object?sender?EventArgs?e)
????????{
????????????Port.PortName?=?portnum_comboBox.SelectedItem?as?string;
????????????Port.Parity?=?Parity.None;
????????????Port.BaudRate?=?Convert.ToInt32(baud_comboBox.SelectedItem);
????????????Port.StopBits?=?StopBits.One;
????????????Port.DataBits?=?8;
????????????Port.ReadBufferSize?=?4096;
????????????Port.DataReceived?+=new?SerialDataReceivedEventHandler(Port_DataReceived);
????????????Port.ReceivedBytesThreshold?=?1;
????????????//listen_button.Text?=?“停止串口“;
????????????if?(Port.PortName.Trim().Equals(string.Empty)?||?Port.BaudRate<=0)
????????????{
????????????????information.Text?=?“請選擇正確的端口號和波特率!“;
????????????}
????????????else
????????????{
????????????????try
????????????????{
????????????????????Port.Open();
????????????????????information.Text?=?“端口打開成功!“;
????????????????}
????????????????catch
????????????????{
????????????????????information.Text?=?“端口打開失敗!“;
????????????????}
????????????}
????????}
????????private?int?result;
????????public?void?Port_DataReceived(object?sender?System.IO.Ports.SerialDataReceivedEventArgs?e)
????????{
????????????byte[]?ReadDataBuffer?=?new?byte[100];//new?byte[Port.ReadBufferSize];
????????????result?=?Port.ReadByte();
????????????data_textBox.Text?=?data_textBox.Text+“?“+Convert.ToString(result);//?Convert.ToString(ReadDataBuffer);
????????????
??//寫文件
???????????/*?try
????????????{
????????????????//StreamWriter?m_SW?=?new?StreamWriter(@“e:\data.txt“?false);
????????????????m_SW.WriteLine(result);
????????????????m_SW.Close();
??????
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????9728??2008-08-02?20:08??RFCOMM1\RFCOMM\bin\Debug\RFCOMM.exe
?????文件??????24064??2008-08-02?20:08??RFCOMM1\RFCOMM\bin\Debug\RFCOMM.pdb
?????目錄??????????0??2008-08-01?10:36??RFCOMM1\RFCOMM\bin\Debug
?????目錄??????????0??2008-08-01?10:36??RFCOMM1\RFCOMM\bin
?????文件???????4650??2008-08-03?11:21??RFCOMM1\RFCOMM\Form1.cs
?????文件???????8433??2008-08-02?17:14??RFCOMM1\RFCOMM\Form1.Designer.cs
?????文件???????6300??2008-08-02?17:14??RFCOMM1\RFCOMM\Form1.resx
?????目錄??????????0??2004-08-02?09:27??RFCOMM1\RFCOMM\obj\Debug\Refactor
?????文件???????5053??2008-08-02?18:13??RFCOMM1\RFCOMM\obj\Debug\ResolveAssemblyReference.cache
?????文件????????842??2008-08-02?17:14??RFCOMM1\RFCOMM\obj\Debug\RFCOMM.csproj.GenerateResource.Cache
?????文件???????9728??2008-08-02?20:08??RFCOMM1\RFCOMM\obj\Debug\RFCOMM.exe
?????文件????????180??2008-08-02?17:14??RFCOMM1\RFCOMM\obj\Debug\RFCOMM.Form1.resources
?????文件??????24064??2008-08-02?20:08??RFCOMM1\RFCOMM\obj\Debug\RFCOMM.pdb
?????文件????????180??2008-08-02?16:15??RFCOMM1\RFCOMM\obj\Debug\RFCOMM.Properties.Resources.resources
?????目錄??????????0??2008-08-01?10:36??RFCOMM1\RFCOMM\obj\Debug\TempPE
?????目錄??????????0??2008-08-01?10:36??RFCOMM1\RFCOMM\obj\Debug
?????文件????????261??2008-08-02?20:08??RFCOMM1\RFCOMM\obj\RFCOMM.csproj.FileList.txt
?????目錄??????????0??2008-08-01?10:36??RFCOMM1\RFCOMM\obj
?????文件????????351??2008-08-01?10:36??RFCOMM1\RFCOMM\Program.cs
?????文件???????1141??2008-08-01?10:36??RFCOMM1\RFCOMM\Properties\AssemblyInfo.cs
?????文件???????2347??2008-08-01?10:36??RFCOMM1\RFCOMM\Properties\Resources.Designer.cs
?????文件???????5618??2008-08-01?10:36??RFCOMM1\RFCOMM\Properties\Resources.resx
?????目錄??????????0??2008-08-01?10:36??RFCOMM1\RFCOMM\Properties
?????文件???????4063??2004-08-02?09:14??RFCOMM1\RFCOMM\RFCOMM.csproj
?????文件????????234??2008-08-02?20:08??RFCOMM1\RFCOMM\RFCOMM.csproj.user
?????目錄??????????0??2008-08-01?10:36??RFCOMM1\RFCOMM
?????文件???????1073??2008-08-01?10:37??RFCOMM1\RFCOMM.sln
????..A..H.?????13824??2008-08-27?17:16??RFCOMM1\RFCOMM.suo
?????目錄??????????0??2008-08-01?10:36??RFCOMM1
-----------?---------??----------?-----??----
............此處省略2個文件信息
評論
共有 條評論