這篇文章將為大家詳細(xì)講解有關(guān)利用C# 項目實現(xiàn)一個串口監(jiān)視上位機(jī)功能,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
實現(xiàn)上位機(jī)和下位機(jī)之間的通信,通常使用的是串口通信,接下來實現(xiàn)一個通過上位機(jī)和串口調(diào)試助手來完成串口通信測試。
首先創(chuàng)建一個WInfrom窗體應(yīng)用工程文件,在創(chuàng)建好的工程下面,通過工具箱中已有的控件完成界面的搭建,如下圖所示,為了方便初學(xué)者容易看懂程序,下圖將控件的命名一并標(biāo)注出來:
直接進(jìn)入正題,將完整的工程代碼黏貼出來:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO.Ports; using System.Diagnostics; namespace Tem_Hum_Monitorring { public partial class Form1 : Form { //實例化串口 SerialPort s = new SerialPort(); public Form1() { InitializeComponent(); Control.CheckForIllegalCrossThreadCalls = false; button1.Text = "打開串口"; int[] item = { 9600,115200}; //遍歷 foreach (int a in item) { comboBox2.Items.Add(a.ToString()); } comboBox2.SelectedItem = comboBox2.Items[1]; } private void Form1_Load(object sender, EventArgs e) { portInit(); } /// <summary> /// 串口初始化 /// </summary> private void portInit() { string[] ports = SerialPort.GetPortNames(); comboBox1.Items.AddRange(ports); comboBox1.SelectedItem = comboBox1.Items[0]; } #region 開關(guān)串口 private void button1_Click(object sender, EventArgs e) { try { if (!s.IsOpen) { s.PortName = comboBox1.SelectedItem.ToString(); s.BaudRate = Convert.ToInt32(comboBox2.SelectedItem.ToString()); s.Open(); s.DataReceived += s_DataReceived; //"+="代表指定響應(yīng)事件時要調(diào)用的方法 button1.Text = "關(guān)閉串口"; } else { s.Close(); s.DataReceived -= s_DataReceived; button1.Text = "打開串口"; } } catch(Exception ee) { MessageBox.Show(ee.ToString()); } } #endregion #region 串口接收 void s_DataReceived(object sender, SerialDataReceivedEventArgs e) { int count = s.BytesToRead; string str = null; if (count == 8) { //數(shù)據(jù)解析 byte[] buff = new byte[count]; s.Read(buff, 0, count); foreach (byte item in buff) { str += item.ToString("X2") + " "; } richTextBox1.Text = "[" + System.DateTime.Now.ToString() + "] " + str + "\n" + richTextBox1.Text; if (buff[0] == 0x04) { ID.Text = buff[0].ToString(); switch (buff[2]) { case 0x01: { Tem.Text = (buff[5] * 4 + buff[4] * 0.05 - 30).ToString(); Hum.Text = (buff[6] + buff[7]).ToString(); break; } case 0x02: { Light.Text = (buff[6] + buff[7]).ToString(); break; } case 0x04: { Dust.Text = (buff[6] + buff[7]).ToString(); break; } default: break; } } } else { //當(dāng)接收數(shù)據(jù)不在設(shè)定的數(shù)據(jù)位范圍之內(nèi)時,會出現(xiàn)接受到的數(shù)據(jù)一直保存在接收緩存區(qū)之內(nèi),后續(xù)每次接手?jǐn)?shù)據(jù)都會將上一次的數(shù)據(jù)進(jìn)行疊加,造成只能通過關(guān)閉串口的方法來清除緩沖區(qū)的數(shù)據(jù) s.DiscardInBuffer(); //丟棄來自串行驅(qū)動程序的接收緩沖區(qū)的數(shù)據(jù) } } #endregion #region 串口發(fā)送 private void button3_Click(object sender, EventArgs e) { string[] sendbuff = richTextBox2.Text.Split(); Debug.WriteLine("發(fā)送字節(jié)數(shù):" + sendbuff.Length); foreach (string item in sendbuff) { int count = 1; byte[] buff = new byte[count]; buff[0] = byte.Parse(item, System.Globalization.NumberStyles.HexNumber); s.Write(buff,0,count); } } #endregion private void button2_Click(object sender, EventArgs e) { int count = 1; byte[] buff = new byte[count]; buff[0] = byte.Parse("04", System.Globalization.NumberStyles.HexNumber); s.Write(buff, 0, count); } } }
網(wǎng)頁標(biāo)題:利用C#項目實現(xiàn)一個串口監(jiān)視上位機(jī)功能-創(chuàng)新互聯(lián)
鏈接地址:http://chinadenli.net/article40/dgjiho.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營銷推廣、營銷型網(wǎng)站建設(shè)、標(biāo)簽優(yōu)化、面包屑導(dǎo)航、網(wǎng)站維護(hù)、網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)