C#串口通信學習筆記


因為參加一個小項目,需要對繼電器進行串口控制,所以這兩天學習了基本的串口編程。同事那邊有JAVA的串口通信包,不過是從網上下載的,比較零亂,難以准確掌握串口通信的流程和內含。因此,個人通過學習網上大牛的方法,利用C#實現了基本的串口通信編程。下面對學習成果進行總結歸納,希望對大家有所幫助。

一、串口通信簡介

串行接口(串口)是一種可以將接受來自CPU的並行數據字符轉換為連續的串行數據流發送出去,同時可將接受的串行數據流轉換為並行的數據字符供給CPU的器件。一般完成這種功能的電路,我們稱為串行接口電路。

串口通信(Serial Communications)的概念非常簡單,串口按位(bit)發送和接收字節。盡管比按字節(byte)的並行通信慢,但是串口可以在使用一根線發送數據的同時用另一根線接收數據。串口通信最重要的參數是波特率、數據位、停止位和奇偶校驗。對於兩個進行通信的端口,這些參數必須匹配。

  1. 波特率:這是一個衡量符號傳輸速率的參數。指的是信號被調制以后在單位時間內的變化,即單位時間內載波參數變化的次數,如每秒鍾傳送960個字符,而每個字符格式包含10位(1個起始位,1個停止位,8個數據位),這時的波特率為960Bd,比特率為10位*960個/秒=9600bps。

  2. 數據位:這是衡量通信中實際數據位的參數。當計算機發送一個信息包,實際的數據往往不會是8位的,標准的值是6、7和8位。標准的ASCII碼是0~127(7位),擴展的ASCII碼是0~255(8位)。

  3. 停止位:用於表示單個包的最后幾位。典型的值為1,1.5和2位。由於數據是在傳輸線上定時的,並且每一個設備有其自己的時鍾,很可能在通信中兩台設備間出現了小小的不同步。因此停止位不僅僅是表示傳輸的結束,並且提供計算機校正時鍾同步的機會。

  4. 校驗位:在串口通信中一種簡單的檢錯方式。有四種檢錯方式:偶、奇、高和低。當然沒有校驗位也是可以的。

二、C#串口編程類

從.NET Framework 2.0開始,C#提供了SerialPort類用於實現串口控制。命名空間:System.IO.Ports。其中詳細成員介紹參看MSDN文檔。下面介紹其常用的字段、方法和事件。

  1. 常用字段:

名稱 說明
PortName 獲取或設置通信端口
BaudRate 獲取或設置串行波特率
DataBits 獲取或設置每個字節的標准數據位長度
Parity 獲取或設置奇偶校驗檢查協議
StopBits 獲取或設置每個字節的標准停止位數

 

 

 

 

 

 

2. 常用方法:

名稱 說明
Close 關閉端口連接,將 IsOpen 屬性設置為 false,並釋放內部 Stream 對象
GetPortNames 獲取當前計算機的串行端口名稱數組
Open 打開一個新的串行端口連接
Read 從 SerialPort 輸入緩沖區中讀取
Write  將數據寫入串行端口輸出緩沖區

 

 

 

 

 

 

3. 常用事件:

名稱 說明
DataReceived 表示將處理 SerialPort 對象的數據接收事件的方法

 

 

 

三、基本用法

下面結合已有的一款繼電器給出串口通信的基本用法,以供參考。

  1 using System;
  2 using System.Windows.Forms;
  3 using System.IO.Ports;
  4 using System.Text;
  5 
  6 namespace Traveller_SerialPortControl
  7 {
  8     public partial class Form1 : Form
  9     {
 10         //定義端口類
 11         private SerialPort ComDevice = new SerialPort();
 12         public Form1()
 13         {
 14             InitializeComponent();
 15             InitralConfig();
 16         }
 17         /// <summary>
 18         /// 配置初始化
 19         /// </summary>
 20         private void InitralConfig()
 21         {
 22             //查詢主機上存在的串口
 23             comboBox_Port.Items.AddRange(SerialPort.GetPortNames());
 24 
 25             if (comboBox_Port.Items.Count > 0)
 26             {
 27                 comboBox_Port.SelectedIndex = 0;
 28             }
 29             else
 30             {
 31                 comboBox_Port.Text = "未檢測到串口";
 32             }
 33             comboBox_BaudRate.SelectedIndex = 5;
 34             comboBox_DataBits.SelectedIndex = 0;
 35             comboBox_StopBits.SelectedIndex = 0;
 36             comboBox_CheckBits.SelectedIndex = 0;
 37             pictureBox_Status.BackgroundImage = Properties.Resources.red;
 38 
 39             //向ComDevice.DataReceived(是一個事件)注冊一個方法Com_DataReceived,當端口類接收到信息時時會自動調用Com_DataReceived方法
 40             ComDevice.DataReceived += new SerialDataReceivedEventHandler(Com_DataReceived);
 41         }
 42 
 43         /// <summary>
 44         /// 一旦ComDevice.DataReceived事件發生,就將從串口接收到的數據顯示到接收端對話框
 45         /// </summary>
 46         /// <param name="sender"></param>
 47         /// <param name="e"></param>
 48         private void Com_DataReceived(object sender, SerialDataReceivedEventArgs e)
 49         {
 50             //開辟接收緩沖區
 51             byte[] ReDatas = new byte[ComDevice.BytesToRead];
 52             //從串口讀取數據
 53             ComDevice.Read(ReDatas, 0, ReDatas.Length);
 54             //實現數據的解碼與顯示
 55             AddData(ReDatas);
 56         }
 57 
 58         /// <summary>
 59         /// 解碼過程
 60         /// </summary>
 61         /// <param name="data">串口通信的數據編碼方式因串口而異,需要查詢串口相關信息以獲取</param>
 62         public void AddData(byte[] data)
 63         {
 64             if (radioButton_Hex.Checked)
 65             {
 66                 StringBuilder sb = new StringBuilder();
 67                 for (int i = 0; i < data.Length; i++)
 68                 {
 69                     sb.AppendFormat("{0:x2}" + " ", data[i]);
 70                 }
 71                 AddContent(sb.ToString().ToUpper());
 72             }
 73             else if (radioButton_ASCII.Checked)
 74             {
 75                 AddContent(new ASCIIEncoding().GetString(data));
 76             }
 77             else if (radioButton_UTF8.Checked)
 78             {
 79                 AddContent(new UTF8Encoding().GetString(data));
 80             }
 81             else if (radioButton_Unicode.Checked)
 82             {
 83                 AddContent(new UnicodeEncoding().GetString(data));
 84             }
 85             else
 86             {
 87                 StringBuilder sb = new StringBuilder();
 88                 for (int i = 0; i < data.Length; i++)
 89                 {
 90                     sb.AppendFormat("{0:x2}" + " ", data[i]);
 91                 }
 92                 AddContent(sb.ToString().ToUpper());
 93             }
 94         }
 95 
 96         /// <summary>
 97         /// 接收端對話框顯示消息
 98         /// </summary>
 99         /// <param name="content"></param>
100         private void AddContent(string content)
101         {
102             BeginInvoke(new MethodInvoker(delegate
103             {              
104                     textBox_Receive.AppendText(content);              
105             }));
106         }
107 
108         /// <summary>
109         /// 串口開關
110         /// </summary>
111         /// <param name="sender"></param>
112         /// <param name="e"></param>
113         private void button_Switch_Click(object sender, EventArgs e)
114         {
115             if (comboBox_Port.Items.Count <= 0)
116             {
117                 MessageBox.Show("未發現可用串口,請檢查硬件設備");
118                 return;
119             }
120 
121             if (ComDevice.IsOpen == false)
122             {
123                 //設置串口相關屬性
124                 ComDevice.PortName = comboBox_Port.SelectedItem.ToString();
125                 ComDevice.BaudRate = Convert.ToInt32(comboBox_BaudRate.SelectedItem.ToString());
126                 ComDevice.Parity = (Parity)Convert.ToInt32(comboBox_CheckBits.SelectedIndex.ToString());
127                 ComDevice.DataBits = Convert.ToInt32(comboBox_DataBits.SelectedItem.ToString());
128                 ComDevice.StopBits = (StopBits)Convert.ToInt32(comboBox_StopBits.SelectedItem.ToString());
129                 try
130                 {
131                     //開啟串口
132                     ComDevice.Open();
133                     button_Send.Enabled = true;
134                 }
135                 catch (Exception ex)
136                 {
137                     MessageBox.Show(ex.Message, "未能成功開啟串口", MessageBoxButtons.OK, MessageBoxIcon.Error);
138                     return;
139                 }
140                 button_Switch.Text = "關閉";
141                 pictureBox_Status.BackgroundImage = Properties.Resources.green;
142             }
143             else
144             {
145                 try
146                 {
147                     //關閉串口
148                     ComDevice.Close();
149                     button_Send.Enabled = false;
150                 }
151                 catch (Exception ex)
152                 {
153                     MessageBox.Show(ex.Message, "串口關閉錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
154                 }
155                 button_Switch.Text = "開啟";
156                 pictureBox_Status.BackgroundImage = Properties.Resources.red;
157             }
158 
159             comboBox_Port.Enabled = !ComDevice.IsOpen;
160             comboBox_BaudRate.Enabled = !ComDevice.IsOpen;
161             comboBox_DataBits.Enabled = !ComDevice.IsOpen;
162             comboBox_StopBits.Enabled = !ComDevice.IsOpen;
163             comboBox_CheckBits.Enabled = !ComDevice.IsOpen;
164         }
165 
166       
167         /// <summary>
168         /// 將消息編碼並發送
169         /// </summary>
170         /// <param name="sender"></param>
171         /// <param name="e"></param>
172         private void button_Send_Click(object sender, EventArgs e)
173         {
174             if (textBox_Receive.Text.Length > 0)
175             {
176                 textBox_Receive.AppendText("\n");
177             }
178 
179             byte[] sendData = null;
180 
181             if (radioButton_Hex.Checked)
182             {
183                 sendData = strToHexByte(textBox_Send.Text.Trim());
184             }
185             else if (radioButton_ASCII.Checked)
186             {
187                 sendData = Encoding.ASCII.GetBytes(textBox_Send.Text.Trim());
188             }
189             else if (radioButton_UTF8.Checked)
190             {
191                 sendData = Encoding.UTF8.GetBytes(textBox_Send.Text.Trim());
192             }
193             else if (radioButton_Unicode.Checked)
194             {
195                 sendData = Encoding.Unicode.GetBytes(textBox_Send.Text.Trim());
196             }
197             else
198             {
199                 sendData = strToHexByte(textBox_Send.Text.Trim());
200             }
201 
202             SendData(sendData);
203         }
204 
205         /// <summary>
206         /// 此函數將編碼后的消息傳遞給串口
207         /// </summary>
208         /// <param name="data"></param>
209         /// <returns></returns>
210         public bool SendData(byte[] data)
211         {
212             if (ComDevice.IsOpen)
213             {
214                 try
215                 {
216                     //將消息傳遞給串口
217                     ComDevice.Write(data, 0, data.Length);
218                     return true;
219                 }
220                 catch (Exception ex)
221                 {
222                     MessageBox.Show(ex.Message, "發送失敗", MessageBoxButtons.OK, MessageBoxIcon.Error);
223                 }
224             }
225             else
226             {
227                 MessageBox.Show("串口未開啟", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
228             }
229             return false;
230         }
231 
232         /// <summary>
233         /// 16進制編碼
234         /// </summary>
235         /// <param name="hexString"></param>
236         /// <returns></returns>
237         private byte[] strToHexByte(string hexString)
238         {
239             hexString = hexString.Replace(" ", "");
240             if ((hexString.Length % 2) != 0) hexString += " ";
241             byte[] returnBytes = new byte[hexString.Length / 2];
242             for (int i = 0; i < returnBytes.Length; i++)
243                 returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2).Replace(" ", ""), 16);
244             return returnBytes;
245         }
246 
247         //以下兩個指令是結合一款繼電器而設計的
248         private void button_On_Click(object sender, EventArgs e)
249         {
250             textBox_Send.Text = "005A540001010000B0";
251         }
252 
253         private void button_Off_Click(object sender, EventArgs e)
254         {
255             textBox_Send.Text = "005A540002010000B1";
256         }
257     }
258 }

軟件實現基本界面

 

希望以上內容對入門串口編程有所幫助,在此感謝百度百科的參考和kasama1953文章的幫助。

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM