




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、C#使用SerialPort控件用類及線程實(shí)現(xiàn)串口通信C# 使用SerialPort 源碼實(shí)例編輯源碼復(fù)制到剪貼板打印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.Threading; namespace TestSerialPort publ
2、ic partial class frmTESTSerialPort : Form public frmTESTSerialPort() InitializeComponent(); Control.CheckForIllegalCrossThreadCalls = false; private Button button1; private TextBox txtSend; private TextBox txtReceive; private Label label1; private Label label2; / / 必需的設(shè)計(jì)器變量。 / private System.Compone
3、ntModel.IContainer components = null; / / 清理所有正在使用的資源。 / / 如果應(yīng)釋放托管資源,為 true;否則為 false。 protected override void Dispose(bool disposing) if (disposing & (components != null) components.Dispose(); base.Dispose(disposing); #region Windows 窗體設(shè)計(jì)器生成的代碼 / / 設(shè)計(jì)器支持所需的方法 - 不要 / 使用代碼編輯器修改此方法的內(nèi)容。 / private void
4、InitializeComponent() this.button1 = new System.Windows.Forms.Button(); this.txtSend = new System.Windows.Forms.TextBox(); this.txtReceive = new System.Windows.Forms.TextBox(); this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.SuspendLayout(); / / b
5、utton1 / this.button1.Location = new System.Drawing.Point(440, 379); this.button1.Name = button1; this.button1.Size = new System.Drawing.Size(75, 23); this.button1.TabIndex = 0; this.button1.Text = 發(fā)送; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.bu
6、tton1_Click); / / txtSend / this.txtSend.Location = new System.Drawing.Point(59, 12); this.txtSend.Multiline = true; this.txtSend.Name = txtSend; this.txtSend.Size = new System.Drawing.Size(456, 164); this.txtSend.TabIndex = 2; / / txtReceive / this.txtReceive.Location = new System.Drawing.Point(59,
7、 200); this.txtReceive.Multiline = true; this.txtReceive.Name = txtReceive; this.txtReceive.Size = new System.Drawing.Size(456, 164); this.txtReceive.TabIndex = 2; / / label1 / this.label1.Location = new System.Drawing.Point(13, 15); this.label1.Name = label1; this.label1.Size = new System.Drawing.S
8、ize(41, 12); this.label1.TabIndex = 0; this.label1.Text = 發(fā)送; / / label2 / this.label2.Location = new System.Drawing.Point(13, 213); this.label2.Name = label2; this.label2.Size = new System.Drawing.Size(41, 12); this.label2.TabIndex = 0; this.label2.Text = 接收; / / frmTESTSerialPort / this.AutoScaleD
9、imensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(546, 434); this.Controls.Add(this.label2); this.Controls.Add(this.label1); this.Controls.Add(this.txtReceive); this.Controls.Add(this.txtSend); this.
10、Controls.Add(this.button1); this.Name = frmTESTSerialPort; this.Text = 串口試驗(yàn); this.ResumeLayout(false); this.PerformLayout(); #endregion private void button1_Click(object sender, EventArgs e) /實(shí)例化串口對(duì)象(默認(rèn):COMM1,9600,e,8,1) SerialPort serialPort1 = new SerialPort(); /更改參數(shù) serialPort1.PortName = COM1; s
11、erialPort1.BaudRate = 19200; serialPort1.Parity = Parity.Odd; serialPort1.StopBits = StopBits.Two; /上述步驟可以用在實(shí)例化時(shí)調(diào)用SerialPort類的重載構(gòu)造函數(shù) /SerialPort serialPort = new SerialPort(COM1, 19200, Parity.Odd, StopBits.Two); /打開(kāi)串口(打開(kāi)串口后不能修改端口名,波特率等參數(shù),修改參數(shù)要在串口關(guān)閉后修改) serialPort1.Open(); /發(fā)送數(shù)據(jù) SendStringData(seria
12、lPort1); /也可用字節(jié)的形式發(fā)送數(shù)據(jù) /SendBytesData(serialPort1); /開(kāi)啟接收數(shù)據(jù)線程 ReceiveData(serialPort1); /發(fā)送字符串?dāng)?shù)據(jù) private void SendStringData(SerialPort serialPort) serialPort.Write(txtSend.Text); / / 開(kāi)啟接收數(shù)據(jù)線程 / private void ReceiveData(SerialPort serialPort) /同步阻塞接收數(shù)據(jù)線程 Thread threadReceive=new Thread(new Parameter
13、izedThreadStart(SynReceiveData); threadReceive.Start(serialPort); /也可用異步接收數(shù)據(jù)線程 /Thread threadReceiveSub = new Thread(new ParameterizedThreadStart(AsyReceiveData); /threadReceiveSub.Start(serialPort); /發(fā)送二進(jìn)制數(shù)據(jù) private void SendBytesData(SerialPort serialPort) byte bytesSend=System.Text.Encoding.Defau
14、lt.GetBytes(txtSend.Text ); serialPort.Write(bytesSend, 0, bytesSend.Length); /同步阻塞讀取 private void SynReceiveData(object serialPortobj) SerialPort serialPort = (SerialPort)serialPortobj; System.Threading.Thread.Sleep(0); serialPort.ReadTimeout = 1000; try /阻塞到讀取數(shù)據(jù)或超時(shí)(這里為2秒) byte firstByte=Convert.To
15、Byte(serialPort.ReadByte(); int bytesRead=serialPort.BytesToRead ; byte bytesData=new bytebytesRead+1; bytesData0 = firstByte; for (int i = 1; i =bytesRead; i+) bytesData = Convert.ToByte( serialPort.ReadByte(); txtReceive.Text = System.Text.Encoding.Default.GetString(bytesData); catch(Exception e) MessageBox.Show(e.Message); /處理超時(shí)錯(cuò)誤 serialPort.Close(); /異步讀取 private void AsyReceiveData(object serialPortobj) SerialPort serialPort = (SerialPort)serialPortobj; System.Threading.Thread.Sleep(500); try txtReceive.Text = serialPort.ReadExisting(); catch (Exception e)
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 合同中止或作廢的協(xié)議書
- 礦產(chǎn)資源供貨合同
- 跨國(guó)借款合同樣本
- 合同協(xié)議書 圖片高清版
- 2025電梯采購(gòu)合同范本
- 2025酒店管理合同協(xié)議模板
- 2025年環(huán)境檢測(cè)與評(píng)估技能考試題及答案
- 導(dǎo)游業(yè)務(wù)試題及答案電大
- 時(shí)鐘測(cè)試題目大全圖片及答案
- float面試題及答案
- 干部履歷表填寫范本(中共中央組織部1999年)
- 勞動(dòng)教育視角下高職院校學(xué)生工匠精神培育研究
- 最簡(jiǎn)單封陽(yáng)臺(tái)安全免責(zé)協(xié)議書
- SH/T 3533-2024 石油化工給水排水管道工程施工及驗(yàn)收規(guī)范(正式版)
- 用友人力資源管理HR解決方案樣本
- 北京市西城區(qū)三帆中學(xué)2023-2024學(xué)年七年級(jí)下學(xué)期期中數(shù)學(xué)試題(無(wú)答案)
- 藥物殘留溶劑分析報(bào)告書
- 腫瘤醫(yī)院推廣方案
- 動(dòng)物出血性肺炎預(yù)防與治療
- 公路工程安全風(fēng)險(xiǎn)辨識(shí)與防控手冊(cè)
- 研究生開(kāi)題報(bào)告評(píng)審表
評(píng)論
0/150
提交評(píng)論