版權(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() /自動(dòng)獲取串行口名稱 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)容里面會(huì)有圖紙預(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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2024年學(xué)生寢室衛(wèi)生的管理制度細(xì)則
- 速寫的課程設(shè)計(jì)
- 二零二五版電商平臺電商平臺數(shù)據(jù)分析與產(chǎn)品優(yōu)化合同3篇
- 年度特種變壓器戰(zhàn)略市場規(guī)劃報(bào)告
- 2025年度社區(qū)停車位產(chǎn)權(quán)轉(zhuǎn)讓協(xié)議范本4篇
- 2025年度鋅錠國際貿(mào)易結(jié)算服務(wù)合同3篇
- 2025年度智能大樓能源管理系統(tǒng)施工合同4篇
- 老虎畫畫賀卡課程設(shè)計(jì)
- 二零二五版共享單車運(yùn)營管理服務(wù)合同4篇
- 2025年度個(gè)人別墅買賣合同范本8篇
- 《酸堿罐區(qū)設(shè)計(jì)規(guī)范》編制說明
- 橋梁監(jiān)測監(jiān)控實(shí)施方案
- 書籍小兵張嘎課件
- 藝術(shù)哲學(xué):美是如何誕生的學(xué)習(xí)通超星期末考試答案章節(jié)答案2024年
- 北京海淀區(qū)2025屆高三下第一次模擬語文試題含解析
- 量子醫(yī)學(xué)治療學(xué)行業(yè)投資機(jī)會(huì)分析與策略研究報(bào)告
- 多重耐藥菌病人的管理-(1)課件
- (高清版)TDT 1056-2019 縣級國土資源調(diào)查生產(chǎn)成本定額
- 環(huán)境監(jiān)測對環(huán)境保護(hù)的意義
- 2023年數(shù)學(xué)競賽AMC8試卷(含答案)
- 2023年十天突破公務(wù)員面試
評論
0/150
提交評論