版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、叮叮小文庫(kù)串口調(diào)試助手預(yù)源代碼9using System;using System.Collect ion s.Ge neric; using System.Comp onen tModel;using System.Data;using System.Draw ing;using System.Linq;using System.Text;using System.Thread in g.Tasks; using System.Wi ndows.Forms;using Syste m.IO;using Syste m.IO .Ports;using System.Thread ing;usi
2、ng DevExpress.XtraEditors;using System.Text.RegularExpressi ons;amespace Win dowsFormsApplicati on3public partial class Form1 : XtraForm SerialPort sp1 = new SerialPort(); int Flag = 0;int StateCha nged = 0;int StateCha nged_Ascii = 1; int StateCha nged_16 = 0;string Temp = null;stri ng Temp memoSe
3、nd = n ull;private void memoSe nd_EditValueCha nged(object sen der, Even tArgs e) -public Form1()In itializeComp onen t();sp1.DataReceived += sp1_DataReceived; private void Form1_Load(object sen der, Even tArgs e) -this.MaximizeBox = false;/檢查是否有串口stri ng str = SerialPort.GetPortNames();if (str = n
4、ull)MessageBox.Show(”本機(jī)沒(méi)有串口!", "error");return;/添加串口項(xiàng)目foreach (stri ng port in Syste m.IO.P orts.SerialPort.GetPortNames() cbSerial.Properties .I tems.Add(port);/串口設(shè)置默認(rèn)選擇項(xiàng)cbSerial.Selectedl ndex = 0;Con trol.CheckForlllegalCrossThreadCalls = false;spl.DataReceived += new訂閱委托SerialData
5、ReceivedEve ntHa ndler(sp1 DataReceived); /private void comboBoxEdit1 Selectedl ndexCha nged(object sen der, Even tArgs e)private void Open Serial_Click(object sen der. Even tArgs e)sp1.Close();/SerialPort sp1 = new SerialPort();if (!sp1.lsOpe n)串口是關(guān)閉的,設(shè)置參數(shù),打開(kāi)串口try/獲取串口號(hào)string serialName = cbSerial.
6、SelectedItem.ToString();sp1.PortName = serialName;/設(shè)置各參數(shù)stri ng strBaudRate = cbBaudRate.Text; string strDateBits = cbDataBits.Text;string strStopBits = cbStop.Text;sp1.BaudRate = Con vert.ToI nt32(strBaudRate); sp1.DataBits = Con vert.ToI nt32(strDateBi ts); switch (cbStop.Text)/case "1":
7、sp1.StopBits =break;case "1.5":sp1.StopBits =break;case "2":sp1.StopBits =break;default:MessageBox.Show("Error:break;switch (cbParity.Text)case "sp1.Parity =break;case "sp1.Parity = break;case "參數(shù)不正確!/無(wú)":波特率數(shù)據(jù)位停止位", "Error");校驗(yàn)位labSerial.Te
8、xt =" labState.Text ="串口號(hào):"+ sp1. PortName; 狀 態(tài):打開(kāi)”;/private void CloseSerial_Click(object sen der, Even tArgs e)/ if (sp1.lsOpe n)/ /sp1.Close();/labSerial.Text ="串口號(hào):” + sp1.PortName;/labState.Text ="狀 態(tài):關(guān)閉"/ private void Sen d_Click(object sen der, Even tArgs e) stri
9、 ng StrSe nd = memoSe nd.Text;if (!sp1.lsOpe n)if (ckTimeSe nd.Checked判斷是否定時(shí)發(fā)送數(shù)據(jù)&& !(stri ng.lsNullOrEmpty(textTime.Text)/string Time = textTime.Text;tmSe nd.l nterval = int.P arse(Time) * 1000;/時(shí)間單位是秒,化成毫秒單位tmSe nd.Start();/開(kāi)啟定時(shí)器elsesp1.Write(StrSe nd);/寫(xiě)入數(shù)據(jù)else/十六進(jìn)制發(fā)送if (ckTimeSe nd.Checke
10、d&& !(stri ng.IsNullOrEmpty(textTime.Text)string Time = textTime.Text;tmSe nd.l nterval = int.P arse(Time) * 1000;tmSe nd.Start();elsestri ng StrSe nd_1 = StrSe nd.Trim();/去掉前后的空格-/stri ng StrSe nd_2 = StrSe nd_1.Replace(” ", "");/stri ng StrSe nd_3 = stri ng.J oi n(” ",R
11、egex.Matches(StrSend_2, ".|.").Cast<Match>().ToList();stri ng StrArray = StrSe nd_1.Split(' ');/用空格符隔開(kāi)字符串?dāng)?shù)組var Str Se nd = new List<stri ng>();/ int byteBufferLe ngth = StrArray.Le ngth;/數(shù)組StrArray的長(zhǎng)度f(wàn)or (int i = 0; i < StrArray.Le ngth; i+)if (StrArrayi.Le ngth <
12、 3)Str_Se nd.Add(StrArrayi);-elseStr_Se nd.AddRa nge(Devide(StrArrayi); -int byteBufferLe ngth = Str_Se nd.Cou nt;byte byteBuffer = new bytebyteBufferLe ngth; in t ii = 0;for (int i = 0; i < Str_Send.Count; i+)把字/decNum = Co nvert.Tol nt32(Str_Se ndi, 16); /符串轉(zhuǎn)成16進(jìn)制數(shù)byteBufferii = Co nvert.ToByte
13、(decNum);trydecNum = Co nvert.Tol nt32(Str_Se ndi, 16); /字符串轉(zhuǎn)成16進(jìn)制數(shù)byteBufferii = Co nvert.ToByte(decNum);catch (System.Exceptio n ex)MessageBox.Show(”輸入錯(cuò)誤!","提示");return;ii+;sp1.Write(byteBuffer,入byteBuffer ,長(zhǎng)度 bytebuffer 的長(zhǎng)度。0, byteBuffer.Length);/從第 0個(gè)開(kāi)始寫(xiě)private List<string>
14、Devide(string str) var ret = Regex.Matches(str, ".|.").Cast<Match>().ToList().Co nvertAll(s => s.Value.ToStri ng();return ret;private void tmSe nd_Tick(object sen der, Even tArgs e) stri ng StrSe nd = memoSe nd.Text;if (rbtnSen dStr.Checked)sp1.Write(StrSe nd);else/去掉前后的空格stri ng
15、StrSe nd_1 = StrSe nd.Trim();stri ng StrArray = StrSe nd_1.Split('');/用空格符隔開(kāi)字符串?dāng)?shù)組if (StrArrayi.Le ngth < 3)叮叮小文庫(kù)int byteBufferLe ngth = Str_Se nd.Cou nt;byte byteBuffer = new bytebyteBufferLe ngth;int ii = 0;for (int i = 0; i < Str_Send.Count; i+)int decNum = 0;trydecNum = Co nvert.Tol
16、 nt32(Str_Se ndi, 16); /把字符串轉(zhuǎn)成16進(jìn)制數(shù)byteBufferii = Co nvert.ToByte(decNum);catch (System.Exceptio n ex)MessageBox.Show(”輸入錯(cuò)誤!","提示");return;ii+;sp1.Write(byteBuffer, 0, byteBuffer.Le ngth);/從第 0 個(gè)開(kāi)始寫(xiě)入byteBuffer ,長(zhǎng)度 bytebuffer 的長(zhǎng)度。void sp1_DataReceived(object sen der, SerialDataReceived
17、Eve ntArgs e)byte byteRead = new bytesp1.BytesToRead;if (rbt nRceStr.Checked) if (rbt nRce16.Checked)memoRcv.Text += sp1.ReadExist in g(); memoRcv.Selectio nStart = memoRcv.Text .Len gth; memoRcv.ScrollToCaret();elseByte receivedData = new Bytesp1.BytesToRead; /創(chuàng)建接收字節(jié)數(shù)組sp1.Read(receivedData, 0, rece
18、ivedData.Le ngth);11叮叮小文庫(kù)stri ng strRcv =null;for (int i = 0; i< receivedData.Le ngth; i+)strRcv += receivedDatai.ToStri ng("X2");memoRcv.Text += strRcv;memoRcv.Selectio nStart = memoRcv.Text .Len gth; memoRcv.ScrollToCaret();private void Stop_Click(object sen der, Even tArgs e)/退出接收數(shù)據(jù)-
19、tmSe nd.Stop();private void Clear_Click(object sen der, Even tArgs e)/清空接收數(shù)據(jù)-memoRcv.Text =""private void TextTime_KeyPress(object sender, KeyPressEventArgs e) /限制定時(shí)發(fā)送時(shí)間只能輸入數(shù)字if (!(Char.lsNumber(e.KeyChar) && e.KeyChar != (char)8)MessageBox.Show(”請(qǐng)輸入數(shù)字"," 提示");e.Ha n
20、dled = true;private void memoSe nd_KeyPress(object sen der, KeyPressEve ntArgs e) -private void btn Close_Click(object sen der, Even tArgs e)/關(guān)閉串口if (sp1.lsOpe n)sp1.Close();labSerial.Text ="串口號(hào):” + sp1.PortName;labState.Text ="狀 態(tài):關(guān)閉"private void Sen d16_KeyPress(object sen der, KeyP
21、ressEve ntArgs e) /stri ng Sen d16 = memoRcv.Text;byte buff = new byteSend16.Length / 2;int in dex = 0;/for (int i = 0; i < Send16.Length; i += 2)/ buffi ndex = Co nvert.ToByte(Se nd16.Substri ng(i, 2), 16);/ +i ndex;/string result = Encoding.Default.GetString(buff);/ MessageBox.Show(result);/mem
22、oRcv.Text = result;private void Sen dStr_KeyPress(object sen der, KeyPressEve ntArgs e) -/stri ng Sen dStr = memoRcv.Text;/byte ba = System.Text.ASCIIE ncodi ng.Default.GetBytes(Se ndStr);/Stri ngBuilder sb = new Strin gBuilder();/foreach (byte b in ba)/ sb.Appe nd(b.ToStri ng("x");/ Messa
23、geBox.Show(sb.ToStri ng();/memoRcv.Text = sb.ToStri ng();private void rbt nSen d16_CheckedCha nged(object sen der, Even tArgs e) -StateCha nged_16 = 1;StateCha nged_Ascii = 0;stri ng StrSe nd = memoSe nd.Text;if (Temp_memoSe nd=StrSe nd.Trim ()&& Flag = 1)16memoSe nd.Text =""foreac
24、h (char S in StrSe nd)memoSe nd.Text += (byte)S).ToStri ng("X2");private void rbt nSen dStr_CheckedCha nged(object sen der, Even tArgs e) if (StateCha nged_16 = 1) -Flag = 1;StateCha nged_16 = 0;StateCha nged_Ascii = 1;stri ng StrSe nd = memoSe nd.Text;Temp = memoSe nd.Text;去掉前后的空格用空格符隔開(kāi)stri ng StrSe nd_1 = StrSe nd.Trim();/stri ng StrArray = StrSe nd_1.Split(' ');/字符串?dāng)?shù)組var Str_Se nd = new List<stri ng>();for (int i = 0;i < StrArray.Le ngth; i+)if (StrArray.Le ngth = 0)con ti nue; elseif (
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝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ù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 期中模擬檢測(cè)卷03(解析版)
- 2025年昌吉職業(yè)技術(shù)學(xué)院高職單招職業(yè)技能測(cè)試近5年??及鎱⒖碱}庫(kù)含答案解析
- 2025年新疆科信職業(yè)技術(shù)學(xué)院高職單招職業(yè)技能測(cè)試近5年??及鎱⒖碱}庫(kù)含答案解析
- 2025科學(xué)儀器行業(yè)技術(shù)發(fā)展與市場(chǎng)前景分析
- 外架工勞務(wù)分包合同范本
- 股東轉(zhuǎn)讓出資合同書(shū)
- 2024年旅游項(xiàng)目規(guī)劃設(shè)計(jì)合同
- 醫(yī)療儀器行業(yè)發(fā)展趨勢(shì)
- 環(huán)境保護(hù)與綠色航空發(fā)展
- 營(yíng)銷推廣服務(wù)合同模板
- 婦科急腹癥的急救與護(hù)理
- 尿道狹窄護(hù)理PPT課2
- 教育家精神六個(gè)方面專題PPT
- 2023年河北廊坊市三河市金創(chuàng)產(chǎn)業(yè)投資有限公司招聘筆試題庫(kù)含答案解析
- 醫(yī)學(xué)專題-脛骨高位截骨術(shù)
- 中國(guó)減肥行業(yè)市場(chǎng)分析與發(fā)展趨勢(shì)講義
- 海通食品集團(tuán)楊梅汁產(chǎn)品市場(chǎng)營(yíng)銷
- 圍術(shù)期下肢深靜脈血栓預(yù)防的術(shù)中護(hù)理
- DBJ51-T 151-2020 四川省海綿城市建設(shè)工程評(píng)價(jià)標(biāo)準(zhǔn)
- GB/T 12996-2012電動(dòng)輪椅車(chē)
- 小象學(xué)院深度學(xué)習(xí)-第7講遞歸神經(jīng)網(wǎng)絡(luò)
評(píng)論
0/150
提交評(píng)論