版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
1、叮叮小文庫串口調(diào)試助手預源代碼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(”本機沒有串口!", "error");return;/添加串口項目foreach (stri ng port in Syste m.IO.P orts.SerialPort.GetPortNames() cbSerial.Properties .I tems.Add(port);/串口設置默認選擇項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)串口是關閉的,設置參數(shù),打開串口try/獲取串口號string serialName = cbSerial.
6、SelectedItem.ToString();sp1.PortName = serialName;/設置各參數(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ù)不正確!/無":波特率數(shù)據(jù)位停止位", "Error");校驗位labSerial.Te
8、xt =" labState.Text ="串口號:"+ sp1. PortName; 狀 態(tài):打開”;/private void CloseSerial_Click(object sen der, Even tArgs e)/ if (sp1.lsOpe n)/ /sp1.Close();/labSerial.Text ="串口號:” + sp1.PortName;/labState.Text ="狀 態(tài):關閉"/ 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判斷是否定時發(fā)送數(shù)據(jù)&& !(stri ng.lsNullOrEmpty(textTime.Text)/string Time = textTime.Text;tmSe nd.l nterval = int.P arse(Time) * 1000;/時間單位是秒,化成毫秒單位tmSe nd.Start();/開啟定時器elsesp1.Write(StrSe nd);/寫入數(shù)據(jù)else/十六進制發(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(' ');/用空格符隔開字符串數(shù)組var Str Se nd = new List<stri ng>();/ int byteBufferLe ngth = StrArray.Le ngth;/數(shù)組StrArray的長度for (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); /符串轉成16進制數(shù)byteBufferii = Co nvert.ToByte
13、(decNum);trydecNum = Co nvert.Tol nt32(Str_Se ndi, 16); /字符串轉成16進制數(shù)byteBufferii = Co nvert.ToByte(decNum);catch (System.Exceptio n ex)MessageBox.Show(”輸入錯誤!","提示");return;ii+;sp1.Write(byteBuffer,入byteBuffer ,長度 bytebuffer 的長度。0, byteBuffer.Length);/從第 0個開始寫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('');/用空格符隔開字符串數(shù)組if (StrArrayi.Le ngth < 3)叮叮小文庫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); /把字符串轉成16進制數(shù)byteBufferii = Co nvert.ToByte(decNum);catch (System.Exceptio n ex)MessageBox.Show(”輸入錯誤!","提示");return;ii+;sp1.Write(byteBuffer, 0, byteBuffer.Le ngth);/從第 0 個開始寫入byteBuffer ,長度 bytebuffer 的長度。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叮叮小文庫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) /限制定時發(fā)送時間只能輸入數(shù)字if (!(Char.lsNumber(e.KeyChar) && e.KeyChar != (char)8)MessageBox.Show(”請輸入數(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)/關閉串口if (sp1.lsOpe n)sp1.Close();labSerial.Text ="串口號:” + sp1.PortName;labState.Text ="狀 態(tài):關閉"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;去掉前后的空格用空格符隔開stri ng StrSe nd_1 = StrSe nd.Trim();/stri ng StrArray = StrSe nd_1.Split(' ');/字符串數(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. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025至2030年中國輕型懸臂貨架數(shù)據(jù)監(jiān)測研究報告
- 2025至2030年中國移動式空芯砌塊成型機數(shù)據(jù)監(jiān)測研究報告
- 2025至2030年中國汽車儀表數(shù)據(jù)監(jiān)測研究報告
- 2025至2030年中國化瘀散結灌腸液數(shù)據(jù)監(jiān)測研究報告
- 2025年中國真絲裙市場調(diào)查研究報告
- 2025至2031年中國紅椰子行業(yè)投資前景及策略咨詢研究報告
- 二零二五年度寧波交通企業(yè)勞動合同與車輛安全責任協(xié)議4篇
- 上市種業(yè)公司金融支持效率測度及影響因素研究
- 二零二五年度水產(chǎn)養(yǎng)殖害蟲防治與養(yǎng)殖環(huán)境合同3篇
- 2025年度出國打工人員勞動合同模板及注意事項3篇
- 吉林省吉林市普通中學2024-2025學年高三上學期二模試題 生物 含答案
- 《電影之創(chuàng)戰(zhàn)紀》課件
- 社區(qū)醫(yī)療抗菌藥物分級管理方案
- 開題報告-鑄牢中華民族共同體意識的學校教育研究
- 《醫(yī)院標識牌規(guī)劃設計方案》
- 公司2025年會暨員工團隊頒獎盛典攜手同行共創(chuàng)未來模板
- 新滬科版八年級物理第三章光的世界各個章節(jié)測試試題(含答案)
- 夜市運營投標方案(技術方案)
- 電接點 水位計工作原理及故障處理
- 國家職業(yè)大典
- 2024版房產(chǎn)代持協(xié)議書樣本
評論
0/150
提交評論