




已閱讀5頁(yè),還剩13頁(yè)未讀, 繼續(xù)免費(fèi)閱讀
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
基于單片機(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ù)。 串口通信的概念非常簡(jiǎn)單,串口按位(bit)發(fā)送和接收字節(jié)。盡管比按字節(jié)(byte)的并行通信慢,但是串口可以在使用一根線發(fā)送數(shù)據(jù)的同時(shí)用另一根線接收數(shù)據(jù)。它很簡(jiǎn)單并且能夠?qū)崿F(xiàn)遠(yuǎn)距離通信。比如IEEE488定義并行通行狀態(tài)時(shí),規(guī)定設(shè)備線總常不得超過20米,并且任意兩個(gè)設(shè)備間的長(zhǎng)度不得超過2米;而對(duì)于串口而言,長(zhǎng)度可達(dá)1200米。首先亮出C#的源程序吧。主要界面:只是作為簡(jiǎn)單的運(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 partial class Form1 : Form public Form1() InitializeComponent(); SerialPort sp = new SerialPort(); private void button1_Click(object sender, EventArgs e) String str1 = comboBox1.Text;/串口號(hào) String str2 = comboBox2.Text;/波特率 String str3 = comboBox3.Text;/校驗(yàn)位 String str4 = comboBox5.Text;/停止位 String str5 = comboBox4.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(請(qǐng)先選擇串口!, Error); return; sp.Close(); sp = new SerialPort(); sp.PortName = comboBox1.Text;/串口編號(hào) 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 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(); button1.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 = 歡迎使用簡(jiǎn)易的串口助手!; groupBox3.Enabled = false;/LED控制界面變灰色 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;/選擇第一個(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 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; checkBox4.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 = String.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ā)送失敗,請(qǐng)選擇發(fā)送的數(shù)據(jù)!); else sp.WriteLine(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 = false; checkBox8.Checked = false; label7.Text = 準(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; 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.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; checkBox7.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 checkBox4_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; radioButton1.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 (checkBox5.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; radioButton3.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.Checked = 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.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 = 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) 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.Checked = 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 = 準(zhǔn)備發(fā)送; textBox2.Text = 8; 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.Checked = 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 = String.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, EventArgs e) label7.Text = 已發(fā)送; if (textBox2.Text = ) MessageBox.Show(發(fā)送失敗。請(qǐng)選擇發(fā)送的數(shù)據(jù)!); else sp.WriteLine(textBox2.Text);/往串口寫數(shù)據(jù) private void radioButton1_CheckedChanged(object 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 = 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; che
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝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ù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 抵押合同六8篇
- 伸縮門采購(gòu)合同合同
- 新零售模式下智慧物流配送優(yōu)化策略
- 灑水車合同5篇
- 商業(yè)保密協(xié)議書十
- 公司員工保底協(xié)議
- 2025年貴港貨運(yùn)資格證培訓(xùn)考試題
- 2025年寧夏貨車從業(yè)資格證答題軟件
- 陶瓷插芯市場(chǎng)分析及競(jìng)爭(zhēng)策略分析報(bào)告
- 珠光材料市場(chǎng)分析及競(jìng)爭(zhēng)策略分析報(bào)告
- 05臨水臨電臨時(shí)設(shè)施安全監(jiān)理細(xì)則
- 國(guó)家煙草行業(yè)物流管理
- “小學(xué)品德與生活教學(xué)關(guān)鍵問題實(shí)踐研究”課題研究中期報(bào)告
- 畢業(yè)設(shè)計(jì)外文文獻(xiàn)-Spring Boot
- 六年級(jí)下冊(cè)《生命.生態(tài).安全》全冊(cè)教案(表格式)
- 采購(gòu)入庫(kù)單模板
- GB/T 15566.6-2007公共信息導(dǎo)向系統(tǒng)設(shè)置原則與要求第6部分:醫(yī)療場(chǎng)所
- 中國(guó)電信教育基地市級(jí)“三通兩平臺(tái)”建設(shè)方案(教育機(jī)構(gòu))
- 火力發(fā)電廠節(jié)能技術(shù)經(jīng)濟(jì)指標(biāo)釋義
- 智能制造知識(shí)課件
- 雙方責(zé)任及工程分工界面
評(píng)論
0/150
提交評(píng)論