![基于C#的串口通信上位機(jī)和下位機(jī)源程序[優(yōu)選材料]_第1頁](http://file1.renrendoc.com/fileroot_temp2/2020-10/4/cd8ed696-caa0-48ec-9541-1ae12f25dcf6/cd8ed696-caa0-48ec-9541-1ae12f25dcf61.gif)
![基于C#的串口通信上位機(jī)和下位機(jī)源程序[優(yōu)選材料]_第2頁](http://file1.renrendoc.com/fileroot_temp2/2020-10/4/cd8ed696-caa0-48ec-9541-1ae12f25dcf6/cd8ed696-caa0-48ec-9541-1ae12f25dcf62.gif)
![基于C#的串口通信上位機(jī)和下位機(jī)源程序[優(yōu)選材料]_第3頁](http://file1.renrendoc.com/fileroot_temp2/2020-10/4/cd8ed696-caa0-48ec-9541-1ae12f25dcf6/cd8ed696-caa0-48ec-9541-1ae12f25dcf63.gif)
下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、 基于單片機(jī)串口通信的上位機(jī)和下位機(jī)實(shí)踐串口是計(jì)算機(jī)上一種非常通用設(shè)備通信的協(xié)議(不要與通用串行總線Universal Serial Bus或者USB混淆)。大多數(shù)計(jì)算機(jī)包含兩個(gè)基于RS232的串口。串口同時(shí)也是儀器儀表設(shè)備通用的通信協(xié)議;很多GPIB兼容的設(shè)備也帶有RS-232口。同時(shí),串口通信協(xié)議也可以用于獲取遠(yuǎn)程采集設(shè)備的數(shù)據(jù)。 串口通信的概念非常簡單,串口按位(bit)發(fā)送和接收字節(jié)。盡管比按字節(jié)(byte)的并行通信慢,但是串口可以在使用一根線發(fā)送數(shù)據(jù)的同時(shí)用另一根線接收數(shù)據(jù)。它很簡單并且能夠?qū)崿F(xiàn)遠(yuǎn)距離通信。比如IEEE488定義并行通行狀態(tài)時(shí),規(guī)定設(shè)備線總常不得超過20米,并且任意
2、兩個(gè)設(shè)備間的長度不得超過2米;而對于串口而言,長度可達(dá)1200米。首先亮出C#的源程序吧。主要界面:只是作為簡單的運(yùn)用,可以擴(kuò)展的。源代碼: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.Timers;namespace 單片機(jī)功能控制 public parti
3、al class Form1 : Form public Form1() InitializeComponent(); SerialPort sp = new SerialPort(); private void button1_Click(object sender, EventArgs e) String str1 = comboBox1.Text;/串口號 String str2 = comboBox2.Text;/波特率 String str3 = comboBox3.Text;/校驗(yàn)位 String str4 = comboBox5.Text;/停止位 String str5 = c
4、omboBox4.Text;/數(shù)據(jù)位 Int32 int2 = Convert.ToInt32(str2);/將字符串轉(zhuǎn)為整型 Int32 int5 = Convert.ToInt32(str5);/將字符串轉(zhuǎn)為整型 groupBox3.Enabled = true;/LED控制界面變可選 try if (button1.Text = 打開串口) if (str1 = null) MessageBox.Show(請先選擇串口!, Error); return; sp.Close(); sp = new SerialPort(); sp.PortName = comboBox1.Text;/串口
5、編號 sp.BaudRate = int2;/波特率 switch (str4)/停止位 case 1: sp.StopBits = StopBits.One; break; case 1.5: sp.StopBits = StopBits.OnePointFive; break; case 2: sp.StopBits = StopBits.Two; break; default: MessageBox.Show(Error:參數(shù)不正確, Error); break; switch (str3) case NONE: sp.Parity = Parity.None; break; case
6、ODD: sp.Parity = Parity.Odd; break; case EVEN: sp.Parity = Parity.Even; break; default: MessageBox.Show(Error:參數(shù)不正確, Error); break; sp.DataBits = int5;/數(shù)據(jù)位 sp.Parity = Parity.Even;/設(shè)置串口屬性 sp.Open();/打開串口 button1.Text = 關(guān)閉串口; textBox1.Text = Convert.ToString(sp.PortName) + 已開啟!; else sp.Close(); butt
7、on1.Text = 打開串口; groupBox3.Enabled = false;/LED控制界面變灰色 textBox1.Text = Convert.ToString(sp.PortName) + 已關(guān)閉!; catch (Exception er) MessageBox.Show(Error: + er.Message, Error); return; private void Form1_Load(object sender, EventArgs e) /初始化 textBox1.Text = 歡迎使用簡易的串口助手!; groupBox3.Enabled = false;/LED
8、控制界面變灰色 groupBox6.Enabled = false; groupBox7.Enabled = false; groupBox8.Enabled = false; button3.Enabled = false; button6.Enabled = false; timer1.Start(); try foreach (string com in System.IO.Ports.SerialPort.GetPortNames() /自動獲取串行口名稱 boBox1.Items.Add(com); /默認(rèn)設(shè)置 comboBox1.SelectedIndex = 0;
9、/選擇第一個(gè)com口 comboBox2.SelectedIndex = 4;/波特率4800 comboBox3.SelectedIndex = 0;/校驗(yàn)位NONE comboBox4.SelectedIndex = 0;/停止位為1 comboBox5.SelectedIndex = 0;/數(shù)據(jù)位為8 catch MessageBox.Show(找不到通訊端口!, 串口調(diào)試助手); private void timer1_Tick(object sender, EventArgs e) label6.Text = DateTime.Now.ToString(); private void
10、 button2_Click(object sender, EventArgs e) try if (button2.Text = 開啟) groupBox6.Enabled = true; radioButton1.Checked = false; radioButton2.Checked = false; radioButton3.Checked = false; radioButton4.Checked = false; checkBox1.Checked = false; checkBox2.Checked = false; checkBox3.Checked = false; che
11、ckBox4.Checked = false; checkBox5.Checked = false; checkBox6.Checked = false; checkBox7.Checked = false; checkBox8.Checked = false; button3.Enabled = true; textBox2.Text = String.Empty; button2.Text = 關(guān)閉; else groupBox6.Enabled = false; button3.Enabled = false; button2.Text = 開啟; textBox2.Text = Str
12、ing.Empty; catch (Exception er) MessageBox.Show(Error: + er.Message, Error); return; private void button3_Click(object sender, EventArgs e) groupBox6.Enabled = true; label7.Text = 已發(fā)送; if (textBox2.Text = ) MessageBox.Show(發(fā)送失敗,請選擇發(fā)送的數(shù)據(jù)!); else sp.WriteLine(textBox2.Text);/往串口寫數(shù)據(jù) private void checkB
13、ox1_CheckedChanged(object sender, EventArgs e) try if (checkBox1.Checked) checkBox1.Checked = true; checkBox2.Checked = false; checkBox3.Checked = false; checkBox4.Checked = false; checkBox5.Checked = false; checkBox6.Checked = false; checkBox7.Checked = false; checkBox8.Checked = false; label7.Text
14、 = 準(zhǔn)備發(fā)送; textBox2.Text = 1; catch (Exception er) MessageBox.Show(Error: + er.Message, Error); return; private void checkBox2_CheckedChanged(object sender, EventArgs e) try if (checkBox2.Checked) checkBox1.Checked = false; checkBox2.Checked = true; checkBox3.Checked = false; checkBox4.Checked = false
15、; checkBox5.Checked = false; checkBox6.Checked = false; checkBox7.Checked = false; checkBox8.Checked = false; label7.Text = 準(zhǔn)備發(fā)送; textBox2.Text = 2; radioButton1.Checked = false; radioButton2.Checked = false; radioButton3.Checked = false; radioButton4.Checked = false; catch (Exception er) MessageBox
16、.Show(Error: + er.Message, Error); return; private void checkBox3_CheckedChanged(object sender, EventArgs e) try if (checkBox3.Checked) checkBox1.Checked = false; checkBox2.Checked = false; checkBox3.Checked = true; checkBox4.Checked = false; checkBox5.Checked = false; checkBox6.Checked = false; che
17、ckBox7.Checked = false; checkBox8.Checked = false; radioButton1.Checked = false; radioButton2.Checked = false; radioButton3.Checked = false; radioButton4.Checked = false; label7.Text = 準(zhǔn)備發(fā)送; textBox2.Text = 3; catch (Exception er) MessageBox.Show(Error: + er.Message, Error); return; private void che
18、ckBox4_CheckedChanged(object sender, EventArgs e) try if (checkBox4.Checked) checkBox1.Checked = false; checkBox2.Checked = false; checkBox3.Checked = false; checkBox4.Checked = true; checkBox5.Checked = false; checkBox6.Checked = false; checkBox7.Checked = false; checkBox8.Checked = false; radioBut
19、ton1.Checked = false; radioButton2.Checked = false; radioButton3.Checked = false; radioButton4.Checked = false; label7.Text = 準(zhǔn)備發(fā)送; textBox2.Text = 4; catch (Exception er) MessageBox.Show(Error: + er.Message, Error); return; private void checkBox5_CheckedChanged(object sender, EventArgs e) try if (c
20、heckBox5.Checked) checkBox1.Checked = false; checkBox2.Checked = false; checkBox3.Checked = false; checkBox4.Checked = false; checkBox5.Checked = true; checkBox6.Checked = false; checkBox7.Checked = false; checkBox8.Checked = false; radioButton1.Checked = false; radioButton2.Checked = false; radioBu
21、tton3.Checked = false; radioButton4.Checked = false; label7.Text = 準(zhǔn)備發(fā)送; textBox2.Text = 5; catch (Exception er) MessageBox.Show(Error: + er.Message, Error); return; private void checkBox6_CheckedChanged(object sender, EventArgs e) try if (checkBox6.Checked) checkBox1.Checked = false; checkBox2.Chec
22、ked = false; checkBox3.Checked = false; checkBox4.Checked = false; checkBox5.Checked = false; checkBox6.Checked = true; checkBox7.Checked = false; checkBox8.Checked = false; radioButton1.Checked = false; radioButton2.Checked = false; radioButton3.Checked = false; radioButton4.Checked = false; label7
23、.Text = 準(zhǔn)備發(fā)送; textBox2.Text = 6; catch (Exception er) MessageBox.Show(Error: + er.Message, Error); return; private void checkBox7_CheckedChanged(object sender, EventArgs e) try if (checkBox7.Checked) checkBox1.Checked = false; checkBox2.Checked = false; checkBox3.Checked = false; checkBox4.Checked =
24、 false; checkBox5.Checked = false; checkBox6.Checked = false; checkBox7.Checked = true; checkBox8.Checked = false; radioButton1.Checked = false; radioButton2.Checked = false; radioButton3.Checked = false; radioButton4.Checked = false; label7.Text = 準(zhǔn)備發(fā)送; textBox2.Text = 7; catch (Exception er) Messa
25、geBox.Show(Error: + er.Message, Error); return; private void checkBox8_CheckedChanged(object sender, EventArgs e) try if (checkBox8.Checked) checkBox1.Checked = false; checkBox2.Checked = false; checkBox3.Checked = false; checkBox4.Checked = false; checkBox5.Checked = false; checkBox6.Checked = fals
26、e; checkBox7.Checked = false; checkBox8.Checked = true; radioButton1.Checked = false; radioButton2.Checked = false; radioButton3.Checked = false; radioButton4.Checked = false; label7.Text = 準(zhǔn)備發(fā)送; textBox2.Text = 8; catch (Exception er) MessageBox.Show(Error: + er.Message, Error); return; private voi
27、d button5_Click(object sender, EventArgs e) try if (button5.Text = 開啟) radioButton1.Checked = false; radioButton2.Checked = false; radioButton3.Checked = false; radioButton4.Checked = false; checkBox1.Checked = false; checkBox2.Checked = false; checkBox3.Checked = false; checkBox4.Checked = false; c
28、heckBox5.Checked = false; checkBox6.Checked = false; checkBox7.Checked = false; checkBox8.Checked = false; groupBox7.Enabled = true; button6.Enabled = true; textBox2.Text = String.Empty; button5.Text = 關(guān)閉; else groupBox7.Enabled = false; button6.Enabled = false; button5.Text = 開啟; textBox2.Text = St
29、ring.Empty; catch (Exception er) MessageBox.Show(Error: + er.Message, Error); return; private void button6_Click(object sender, EventArgs e) label7.Text = 已發(fā)送; if (textBox2.Text = ) MessageBox.Show(發(fā)送失敗。請選擇發(fā)送的數(shù)據(jù)!); else sp.WriteLine(textBox2.Text);/往串口寫數(shù)據(jù) private void radioButton1_CheckedChanged(obj
30、ect sender, EventArgs e) try if (radioButton1.Checked) label7.Text = 準(zhǔn)備發(fā)送; textBox2.Text = 9; checkBox1.Checked = false; checkBox2.Checked = false; checkBox3.Checked = false; checkBox4.Checked = false; checkBox5.Checked = false; checkBox6.Checked = false; checkBox7.Checked = false; checkBox8.Checked
31、 = false; catch (Exception er) MessageBox.Show(Error: + er.Message, Error); return; private void radioButton2_CheckedChanged(object sender, EventArgs e) try if (radioButton2.Checked) label7.Text = 準(zhǔn)備發(fā)送; textBox2.Text = a; checkBox1.Checked = false; checkBox2.Checked = false; checkBox3.Checked = false; checkBox4.Checked = false; checkBox5.Checked = false; checkBox6.Checked = false; ch
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 跨國公司出口業(yè)務(wù)委托管理三方合同
- 倉儲物流場地租賃及一體化服務(wù)合同
- 貝類養(yǎng)殖產(chǎn)業(yè)技術(shù)評價(jià)合同
- 碑刻與政治史研究合同
- 疏港工程拆除與港口設(shè)施重建合同
- 成都寫字樓租賃合同示范文本
- 美術(shù)課件制作技能
- 美術(shù)外寫生課件
- 安全風(fēng)險(xiǎn)管控的內(nèi)容
- 檢驗(yàn)崗位的安全職責(zé)
- 加油站安全生產(chǎn)隱患排查治理制度
- 學(xué)科建設(shè)研討活動方案
- 千川投手培訓(xùn)課件
- 廣東省佛山禪城區(qū)七校聯(lián)考2025屆七下英語期末預(yù)測試題含答案
- 佛山市2024-2025高一下期末-物理試卷
- 建設(shè)工程(更新)融資投資立項(xiàng)項(xiàng)目可行性研究報(bào)告(非常詳細(xì))
- Unit 3 Same or Different?Section A 課件 人教版英語八年級上冊
- 2025年廣東省高考語文試卷(含標(biāo)準(zhǔn)答案)
- DL∕T 5342-2018 110kV~750kV架空輸電線路鐵塔組立施工工藝導(dǎo)則
- (高清版)TDT 1056-2019 縣級國土資源調(diào)查生產(chǎn)成本定額
- 小學(xué)語文人教五年級上冊第七單元《四季之美》課件 市賽獲獎
評論
0/150
提交評論