




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、 基于單片機串口通信的上位機和下位機實踐串口是計算機上一種非常通用設(shè)備通信的協(xié)議(不要與通用串行總線Universal Serial Bus或者USB混淆)。大多數(shù)計算機包含兩個基于RS232的串口。串口同時也是儀器儀表設(shè)備通用的通信協(xié)議;很多GPIB兼容的設(shè)備也帶有RS-232口。同時,串口通信協(xié)議也可以用于獲取遠程采集設(shè)備的數(shù)據(jù)。 串口通信的概念非常簡單,串口按位(bit)發(fā)送和接收字節(jié)。盡管比按字節(jié)(byte)的并行通信慢,但是串口可以在使用一根線發(fā)送數(shù)據(jù)的同時用另一根線接收數(shù)據(jù)。它很簡單并且能夠?qū)崿F(xiàn)遠距離通信。比如IEEE488定義并行通行狀態(tài)時,規(guī)定設(shè)備線總常不得超過20米,并且任意
2、兩個設(shè)備間的長度不得超過2米;而對于串口而言,長度可達1200米。首先亮出C#的源程序吧。主要界面:只是作為簡單的運用,可以擴展的。源代碼: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 單片機功能控制 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;/校驗位 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
5、(); sp.PortName = comboBox1.Text;/串口編號 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ù)不正確&
6、quot;, "Error"); break; switch (str3) case "NONE": sp.Parity = Parity.None; break; case "ODD": sp.Parity = Parity.Odd; break; case "EVEN": sp.Parity = Parity.Even; break; default: MessageBox.Show("Error:參數(shù)不正確", "Error"); break; sp.DataBits
7、= int5;/數(shù)據(jù)位 sp.Parity = Parity.Even;/設(shè)置串口屬性 sp.Open();/打開串口 button1.Text = "關(guān)閉串口" textBox1.Text = Convert.ToString(sp.PortName) + "已開啟!" else sp.Close(); button1.Text = "打開串口" groupBox3.Enabled = false;/LED控制界面變灰色 textBox1.Text = Convert.ToString(sp.PortName) + "已關(guān)
8、閉!" catch (Exception er) MessageBox.Show("Error:" + er.Message, "Error"); return; private void Form1_Load(object sender, EventArgs e) /初始化 textBox1.Text = "歡迎使用簡易的串口助手!" groupBox3.Enabled = false;/LED控制界面變灰色 groupBox6.Enabled = false; groupBox7.Enabled = false; gro
9、upBox8.Enabled = false; button3.Enabled = false; button6.Enabled = false; timer1.Start(); try foreach (string com in System.IO.Ports.SerialPort.GetPortNames() /自動獲取串行口名稱 boBox1.Items.Add(com); /默認設(shè)置 comboBox1.SelectedIndex = 0;/選擇第一個com口 comboBox2.SelectedIndex = 4;/波特率4800 comboBox3.SelectedIndex =
10、 0;/校驗位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 button2_Click(object sender, EventArgs e) try if
11、 (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; checkBox4.Checked = false; checkBox5.Che
12、cked = 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 = String.Empty; catc
13、h (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.WriteLin
14、e(textBox2.Text);/往串口寫數(shù)據(jù) private void checkBox1_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 = f
15、alse; checkBox8.Checked = false; label7.Text = "準備發(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.Check
16、ed = false; checkBox2.Checked = true; checkBox3.Checked = false; checkBox4.Checked = false; checkBox5.Checked = false; checkBox6.Checked = false; checkBox7.Checked = false; checkBox8.Checked = false; label7.Text = "準備發(fā)送" textBox2.Text = "2" radioButton1.Checked = false; radioButt
17、on2.Checked = false; radioButton3.Checked = false; radioButton4.Checked = false; catch (Exception er) MessageBox.Show("Error:" + er.Message, "Error"); return; private void checkBox3_CheckedChanged(object sender, EventArgs e) try if (checkBox3.Checked) checkBox1.Checked = false; c
18、heckBox2.Checked = false; checkBox3.Checked = true; checkBox4.Checked = false; checkBox5.Checked = false; checkBox6.Checked = false; checkBox7.Checked = false; checkBox8.Checked = false; radioButton1.Checked = false; radioButton2.Checked = false; radioButton3.Checked = false; radioButton4.Checked =
19、false; label7.Text = "準備發(fā)送" textBox2.Text = "3" catch (Exception er) MessageBox.Show("Error:" + er.Message, "Error"); return; private void checkBox4_CheckedChanged(object sender, EventArgs e) try if (checkBox4.Checked) checkBox1.Checked = false; checkBox2.Chec
20、ked = false; checkBox3.Checked = false; checkBox4.Checked = true; checkBox5.Checked = false; checkBox6.Checked = false; checkBox7.Checked = false; checkBox8.Checked = false; radioButton1.Checked = false; radioButton2.Checked = false; radioButton3.Checked = false; radioButton4.Checked = false; label7
21、.Text = "準備發(fā)送" textBox2.Text = "4" catch (Exception er) MessageBox.Show("Error:" + er.Message, "Error"); return; private void checkBox5_CheckedChanged(object sender, EventArgs e) try if (checkBox5.Checked) checkBox1.Checked = false; checkBox2.Checked = false;
22、checkBox3.Checked = false; checkBox4.Checked = false; checkBox5.Checked = true; checkBox6.Checked = false; checkBox7.Checked = false; checkBox8.Checked = false; radioButton1.Checked = false; radioButton2.Checked = false; radioButton3.Checked = false; radioButton4.Checked = false; label7.Text = "
23、;準備發(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.Checked = false; checkBox3.Che
24、cked = 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.Text = "準備發(fā)送" t
25、extBox2.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;
26、 checkBox4.Checked = 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 = "準備發(fā)送" textBox2.Text
27、= "7" catch (Exception er) MessageBox.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.Ch
28、ecked = false; checkBox5.Checked = false; checkBox6.Checked = false; checkBox7.Checked = false; checkBox8.Checked = true; radioButton1.Checked = false; radioButton2.Checked = false; radioButton3.Checked = false; radioButton4.Checked = false; label7.Text = "準備發(fā)送" textBox2.Text = "8&quo
29、t; catch (Exception er) MessageBox.Show("Error:" + er.Message, "Error"); return; private void button5_Click(object sender, EventArgs e) try if (button5.Text = "開啟") radioButton1.Checked = false; radioButton2.Checked = false; radioButton3.Checked = false; radioButton4.Ch
30、ecked = false; checkBox1.Checked = false; checkBox2.Checked = false; checkBox3.Checked = false; checkBox4.Checked = false; checkBox5.Checked = false; checkBox6.Checked = false; checkBox7.Checked = false; checkBox8.Checked = false; groupBox7.Enabled = true; button6.Enabled = true; textBox2.Text = Str
31、ing.Empty; button5.Text = "關(guān)閉" else groupBox7.Enabled = false; button6.Enabled = false; button5.Text = "開啟" textBox2.Text = String.Empty; catch (Exception er) MessageBox.Show("Error:" + er.Message, "Error"); return; private void button6_Click(object sender, Ev
32、entArgs e) label7.Text = "已發(fā)送" if (textBox2.Text = "") MessageBox.Show("發(fā)送失敗。請選擇發(fā)送的數(shù)據(jù)!"); else sp.WriteLine(textBox2.Text);/往串口寫數(shù)據(jù) private void radioButton1_CheckedChanged(object sender, EventArgs e) try if (radioButton1.Checked) label7.Text = "準備發(fā)送" textBox2.
33、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 = false; catch (Exception er) MessageBox.Show("Error:" + er.Me
34、ssage, "Error"); return; private void radioButton2_CheckedChanged(object sender, EventArgs e) try if (radioButton2.Checked) label7.Text = "準備發(fā)送" textBox2.Text = "a" checkBox1.Checked = false; checkBox2.Checked = false; checkBox3.Checked = false; checkBox4.Checked = false; checkBox5.Checked = false; checkBox6.Checked = false; checkBox7.Checked = false; checkBox8.Checked = false; catch (Ex
溫馨提示
- 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)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 網(wǎng)購?fù)丝顓f(xié)議書范本
- 物資置換協(xié)議書范本
- 廣告宣傳授權(quán)委托協(xié)議書范本詳細解讀
- 智能制造園區(qū)場地使用權(quán)出讓合同范本
- 車輛運輸合同模板:長途貨運車輛運輸與保險
- 餐飲店員工培訓(xùn)與發(fā)展協(xié)議
- 主題餐廳投資合作及品牌輸出協(xié)議
- 高原橋梁混凝土抗凍配比研究與應(yīng)用
- 2024年高考語文二輪復(fù)習專題3散文閱讀突破練12詞句理解與表達技巧賞析
- 上消化道碘水造影護理
- 配電室巡檢培訓(xùn)
- 項目部組織安排
- 妊娠期非產(chǎn)科手術(shù)麻醉管理
- 武警防暴隊形課件
- DB21T 3163-2019 遼寧省綠色建筑施工圖設(shè)計評價規(guī)程
- 采購合規(guī)培訓(xùn)
- 黨建標準化建設(shè)培訓(xùn)
- 臨時停車場設(shè)施建設(shè)方案
- 小學家校共育合作實施方案
- 警用執(zhí)法記錄儀培訓(xùn)
- 財務(wù)崗位招聘筆試題及解答(某大型國企)2025年
評論
0/150
提交評論