版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、串口 COM3正常情況下依次發(fā)送過來“G1 G2 G3”內(nèi)容,串口助手判斷接收的數(shù)據(jù)內(nèi)容,然后發(fā)送特定長度的隨機(jī)內(nèi)容給串口,當(dāng)串口接收到的非“ G1G2G3 ”內(nèi)容,提示system root 錯(cuò)誤整個(gè)軟件功能包含如下:搜索電腦上的串口,并打開指定的串口設(shè)置串口的波特率設(shè)置發(fā)送和接受的模式,是數(shù)值還是字符打開或者關(guān)閉端口,打開端口后,發(fā)送和接收模式、端口和波特率不能更改發(fā)送,當(dāng)文本內(nèi)容為空的時(shí)候,“發(fā)送”按鈕顯示灰色,不能使用當(dāng)接收到G1的時(shí)候,記數(shù)一次,并在lobel上顯示當(dāng)接收異常的是時(shí)候,記數(shù)一次,并在lobel上顯示主要代碼如下:using System;using System.Co
2、llections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.IO.Ports;namespace SerialCommunicatepublic partial class Form1 : Formint count = 0; /記數(shù)G1收到的次數(shù)int root = 0;/ 記數(shù) 收到錯(cuò)誤的次數(shù)public Form1()Initia
3、lizeComponent();System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;private void button1_Click(object sender, EventArgs e)tryserialPort1.PortName = comboBox1.Text;serialPortl.BaudRate = Convert.ToInt32(comboBox2.Text,10); /十進(jìn)制數(shù)據(jù)轉(zhuǎn) 換serialPort1.Open();button1.Enabled = false ;/打開串 口按鈕
4、不可用butt on2.Enabled = t rue;/關(guān)閉串口 comboBox1.Enabled = false; comboBox2.Enabled = false; panel1.Enabled = false; panel2.Enabled = false;catch MessageBox.Show(端口錯(cuò)誤,請檢查串口 ,”錯(cuò)誤);private void Form1_Load (object sender, EventArgs e)this.timer1.Start();this. timer1.Interval = 1000; /定時(shí)間隔 100msst ring str =
5、 Sys te m.IO.Por ts.SerialPor t.Ge tPor tNames();/獲取串口數(shù)組 comboBox1.Items.AddRange(str);string bote = new string 9600, 115200, 2400 ; /comboBox1.Text = COM 1 ; /串口號多額默認(rèn)值 comboBox1.Text = str0;comboBox2.Items.AddRange(bote);comboBox2.Text = bot e0;/波特率默認(rèn)值/btnOK.Enabled = txMsg.Text.Trim() != String.Em
6、pty;/button3.Enabled = textBox2.Text.Trim() != string.Empty;textBox2.Text = ;if (textBox2.Text.Length != 0) /判斷文本2是否為空,為空不能發(fā)送 button3.Enabled = true; else button3.Enabled = false;label4.Text = count.ToString() + 次發(fā)送 G1; label5.Text = root.ToString() + 次出現(xiàn) system root;/Tj 匚 ril-l-l-l/*非常重要*/serialPor
7、t1.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived) ; /必須手動(dòng)添加事件處理程序private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)/ 串口數(shù)據(jù)接收事件if (!radioButton3. Checked) /如果接收模式為字符模式 string str = serialPort1. ReadExisting () ; /字符串方式讀 t ex tBox1.AppendTex t(s tr); /添加
8、內(nèi)容else /如果接收模式為數(shù)值接收byte data;da ta = (by te)serialPor t1.ReadBy te();/此處需要強(qiáng)制類型轉(zhuǎn)換,將(int)類型 數(shù)據(jù)轉(zhuǎn)換為(byte類型數(shù)據(jù),不必考慮是否會(huì)丟失數(shù)據(jù)string str = Convert.ToString(data, 16). ToUpper() /轉(zhuǎn)換為大寫十六進(jìn)制字 符串textBox1.AppendText(0 x + (str.Length = 1 ?0 + str : str) + );/ 空位補(bǔ)“0”/上一句等同為:if(str.Length = 1)/str = 0 + str;/else/st
9、r = str;/textBox1.AppendText(0 x + str);private void radioButton2_CheckedChanged(object sender, EventArgs e) private void button2_Click(object sender, EventArgs e)tryserialPor t1.Close();/關(guān)閉串口butt onl.Enabled = t rue;/打開串口按鈕可用button2.Enabled = false ;/關(guān)閉串 口按鈕不可用 comboBox1.Enabled = true;comboBox2.En
10、abled = true;panel1.Enabled = true;panel2.Enabled = true;catch (Excep tion err) /一般情況下關(guān)閉串口不會(huì)出錯(cuò),所以不需要加處理程序 private void button3_Click(object sender, EventArgs e)byt e Da ta = new byt e1;/作用同上集if (serialPor t1.Is0pen)/判斷串口是否打開,如果打開執(zhí)行下一步操作 if (textBox2.Text != )/button3.Enabled = true;if (!radioBu tt o
11、n1.Checked)/如果發(fā)送模式是字符模式try /textBox2.Text = !000 100 0000 000.0 +50.0 00.0 00.0 00.0;serialPort1.WriteLine(textBox2.Text); /寫數(shù)據(jù)catch (Exception err)MessageBox.Show(串口數(shù)據(jù)寫入錯(cuò)誤,”錯(cuò)誤);/出錯(cuò)提示 serialPort1.Close();butto n1.Enabled = t rue;/打開串口按鈕可用 button2.Enabled = false ;/關(guān)閉串 口按鈕不可用else for(inti =0; i (text
12、Box2.Text.Length - textBox2. Text. Length %2) / 2; i+)/取余3運(yùn)算作用是防止用戶輸入的字符為奇數(shù)個(gè)Data0 = Convert.ToByte(textBox2.Text.Substring(i * 2, 2), 16);serialPortl.Write(Data, 0, 1);/循環(huán)發(fā)送(如果輸入字符為 0A0BB, 則只發(fā)送0A,0B)if (textBox2.Text.Length % 2 != 0) /剩下一位單獨(dú)處理Data0 =Convert.ToByte(textBox2.Text.Substring(textBox2.Te
13、xt.LengthT, 1), 16);/單獨(dú)發(fā)送 B (OB) serialPort1.Write(Data, 0, 1);/發(fā)送private void textBox2_TextChanged (object sender, EventArgs e)if (textBox2.Text.Length != 0) /判斷文本2是否為空,為空不能發(fā)送button3.Enabled = true;elsebutton3.Enabled = false;private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)pr
14、ivate void comboBox1_SelectedIndexChanged(object sender, EventArgs e) private void button4_Click(object sender, EventArgs e) private void button4_Click_1(object sender, EventArgs e) textBox1.Text = ;/清除按鈕按下 清空private void textBox1_TextChanged(object sender, EventArgs e) private void timer1_Tick(obje
15、ct sender, EventArgs e)this.timer1.Stop();/ 首先關(guān)閉定時(shí)器,因有可能后面執(zhí)行的代碼時(shí)間大于設(shè)置的定 時(shí)間隔時(shí)間if (textBox1.Text = 0 x47 0 x33 0 x0D )Random r2 = new Random();/產(chǎn)生隨機(jī)數(shù)int z = r2.Next(999);textBox2.Text = !225.9/255.8/255.6 255.9/255.8/226.6 219.1/001.0/001.0001.7/001.0/001.0n;/textBox2.Text = !+z.ToString()+/+ z.ToStri
16、ng() + /+ z.ToString() + / + +/ 255.9 / 255.8 / 226.6 219.1 / 001.0 / 001.0 001.7 / 001.0 / 001.0n ;/serialPor tl.Wri teLine( text Box2.Tex t);/寫數(shù)據(jù) button4_Click(sender, e);textBox1.Text = null; /textBox2.Text = null;else if (textBox1.Text = 0 x47 0 x32 0 x0D )/G2Random r1 = new Random();/產(chǎn)生隨機(jī)數(shù)int v
17、 = r1.Next(1);textBox2.Text = !0000 + v.ToString() + 011 00000111 00000000n;/textBox2.Text = !00000011 00000111 00000000n;/serialPor tl.Wri teLine( text Box2.Tex t);/寫數(shù)據(jù) button4_Click(sender, e);textBox1.Text = null; /textBox2.Text = null;else if (textBox1.Text = 0 x47 0 x31 0 x0D ) /G1Random r1 = n
18、ew Random();/產(chǎn)生隨機(jī)數(shù)int v = r1.Next(99);/隨機(jī)數(shù)范圍 099Random r2 = new Random();/產(chǎn)生隨機(jī)數(shù) int z = r2.Next(9999);/隨機(jī)數(shù)范圍 09999/textBox2.Text = !000 100 0000 000.0 + v.ToString()+.0 00.0 00.0 00.0n;textBox2.Text =!000 + v.ToString() + + z.ToString() + 000.0 + + + v.ToString() + .0 00.0 00.0 00.0n;serialPor tl.Wri teLine( tex tBox2.Tex t); /寫數(shù)據(jù) bu tt on4_Click(sender, e);/ 引用 butt on 事件 textBox1.Text = null;/textBox2.Text = null;label4.Text = count.ToString() + 次發(fā)送 G1 ;/label 上顯示發(fā)送的次數(shù) count+;else if(textBox1.Text !=)label5.Text = root.ToString() + 次出現(xiàn) syst
溫馨提示
- 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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 中國雙螺桿泵行業(yè)運(yùn)行態(tài)勢及未來發(fā)展趨勢預(yù)測報(bào)告
- 修路溝渠工程合同范本
- 出租單間小屋合同范本
- 加盟餐飲連鎖合同范例
- 中國人體安檢設(shè)備行業(yè)市場深度研究及投資規(guī)劃建議報(bào)告
- 公司個(gè)人借款合同范例
- 分期購車合同范本6
- 2025年度摩托車行業(yè)技術(shù)交流合作合同模板
- 公司采購勞保合同范本
- 農(nóng)村地?fù)Q地合同范本
- 小學(xué)英語-What a dream教學(xué)設(shè)計(jì)學(xué)情分析教材分析課后反思
- 數(shù)據(jù)分析系統(tǒng)Hive培訓(xùn)課件
- 小學(xué)五年級英語20篇英文閱讀理解(答案附在最后)
- 學(xué)校安全隱患排查治理工作臺賬
- GB/T 8151.13-2012鋅精礦化學(xué)分析方法第13部分:鍺量的測定氫化物發(fā)生-原子熒光光譜法和苯芴酮分光光度法
- 2023年遼寧鐵道職業(yè)技術(shù)學(xué)院高職單招(英語)試題庫含答案解析
- GB/T 39274-2020公共安全視頻監(jiān)控?cái)?shù)字視音頻編解碼技術(shù)測試規(guī)范
- GB/T 23800-2009有機(jī)熱載體熱穩(wěn)定性測定法
- T-SFSF 000012-2021 食品生產(chǎn)企業(yè)有害生物風(fēng)險(xiǎn)管理指南
- 2023年上海市閔行區(qū)精神衛(wèi)生中心醫(yī)護(hù)人員招聘筆試題庫及答案解析
- 水庫工程施工組織設(shè)計(jì)
評論
0/150
提交評論