這其實就是一個用C#寫一個串列埠除錯的工具,下面看怎麼實現
this.Serial_Rate.SelectedIndexChanged += new System.EventHandler(this.Serial_Param_Changed);// 下拉列表繫結事件
this.Serial_OpenPort.Click += new System.EventHandler(this.Button_OpenPort_Click);//開啟串列埠繫結事件
this.Serial_Send_Data.Click += new System.EventHandler(this.Serial_Send_Data_Click);//傳送資料繫結事件
this.Serial_SendText.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.Serial_SendText_PressKey);//傳送訊息框繫結事件 過濾按鍵
this.Serial_RecvText.TextChanged += new System.EventHandler(this.Serial_RecvText_TextChanged);//接收資料框繫結事件 自動下拉到最下面
this.Serial_Display_Hex.Click += new System.EventHandler(this.Serial_Display_Hex_CheckedChanged);//已hex 顯示 選擇框繫結事件
private void Button_OpenPort_Click(object sender, EventArgs e)
{
if (Serial1.IsOpen)
Serial1.Close();//關閉串列埠
}
else
Serial_Open();//讀取串列埠設定,並開啟串列埠
private void Serial_Send_Data_Click(object sender, EventArgs e)
if (Serial_SendText.Text.Length > 0)
if (Serial1.IsOpen == false)
Serial_Open();
Serial_Status();
if (Serial_SendWithHex.Checked == false)
string TxBuff = Serial_SendText.Text;
byte[] Tx = Encoding.GetEncoding("gb2312").GetBytes(TxBuff);
Comm_Send_Bytes += Tx.Length;
Serial_SendNum.Text = "傳送:" + Comm_Send_Bytes.ToString();
Serial1.Write(Tx, 0, Tx.Length);
string TxTemp = Serial_SendText.Text;
byte[] Serial_SendBuff = Str2Hex(TxTemp);
Comm_Send_Bytes += Serial_SendBuff.Length;
Serial1.Write(Serial_SendBuff, 0, Serial_SendBuff.Length);
MessageBox.Show("傳送框不能為空!");
private void Com_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
try
//開闢接收緩衝區
byte[] ReDatas = new byte[Serial1.BytesToRead];
//從串列埠讀取資料
Comm_Received_Bytes += Serial1.Read(ReDatas, 0, Serial1.BytesToRead);
//實現資料的解碼與顯示
this.Invoke(updateText, ReDatas);
catch(Exception ex)
MessageBox.Show(ex.Message);
//清空輸入輸出TextBox 傳送接收資料長度及label
private void Serial_Empty_Click(object sender, EventArgs e)
Comm_Received_Bytes = 0;
Comm_Send_Bytes = 0;
Serial_SendNum.Text = "傳送:0";
Serial_RecvNum.Text = "接收:0";
Serial_RecvText.Text = "";
Serial_SendText.Text = "";
Serial_Rx_Buff_Hex = "";
Serial_Rx_Buff_Ascii = "";
//接收區
string ReDatas = Serial1.ReadExisting();
Comm_Received_Bytes += ReDatas.Length;
byte[] RecvBuff = System.Text.Encoding.GetEncoding("gb2312").GetBytes(ReDatas);
ReDatas = System.Text.Encoding.GetEncoding("gb2312").GetString(RecvBuff );
//傳送區
byte[] Tx = GB2312.GetBytes(TxBuff);
上面列出了主要的程式碼,其實就是一個串列埠助手。
這其實就是一個用C#寫一個串列埠除錯的工具,下面看怎麼實現
1、在工具裡面找到串列埠控制元件,拖到視窗上即可。然後新增需要的各種按鍵和顯示框控制元件等2.繫結事件 其實也沒啥,系統都會幫我們全部建好,不用我們再去一行一行的去新增程式碼,so easy!!this.Serial_Rate.SelectedIndexChanged += new System.EventHandler(this.Serial_Param_Changed);// 下拉列表繫結事件
this.Serial_OpenPort.Click += new System.EventHandler(this.Button_OpenPort_Click);//開啟串列埠繫結事件
this.Serial_Send_Data.Click += new System.EventHandler(this.Serial_Send_Data_Click);//傳送資料繫結事件
this.Serial_SendText.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.Serial_SendText_PressKey);//傳送訊息框繫結事件 過濾按鍵
this.Serial_RecvText.TextChanged += new System.EventHandler(this.Serial_RecvText_TextChanged);//接收資料框繫結事件 自動下拉到最下面
this.Serial_Display_Hex.Click += new System.EventHandler(this.Serial_Display_Hex_CheckedChanged);//已hex 顯示 選擇框繫結事件
3.串列埠的開啟與關閉 這個也挺簡單的,主要是判斷當前串列埠是否開啟,如果沒有的話,就開啟,private void Button_OpenPort_Click(object sender, EventArgs e)
{
if (Serial1.IsOpen)
{
Serial1.Close();//關閉串列埠
}
else
{
Serial_Open();//讀取串列埠設定,並開啟串列埠
}
}
4.資料的傳送(需要注意的地方就是 在傳送之間,先將傳送的內容轉換編碼形式,傳送的時候用位元組形式傳送)private void Serial_Send_Data_Click(object sender, EventArgs e)
{
if (Serial_SendText.Text.Length > 0)
{
if (Serial1.IsOpen == false)
{
Serial_Open();
Serial_Status();
}
if (Serial_SendWithHex.Checked == false)
{
string TxBuff = Serial_SendText.Text;
byte[] Tx = Encoding.GetEncoding("gb2312").GetBytes(TxBuff);
Comm_Send_Bytes += Tx.Length;
Serial_SendNum.Text = "傳送:" + Comm_Send_Bytes.ToString();
Serial1.Write(Tx, 0, Tx.Length);
}
else
{
string TxTemp = Serial_SendText.Text;
byte[] Serial_SendBuff = Str2Hex(TxTemp);
Comm_Send_Bytes += Serial_SendBuff.Length;
Serial_SendNum.Text = "傳送:" + Comm_Send_Bytes.ToString();
Serial1.Write(Serial_SendBuff, 0, Serial_SendBuff.Length);
}
}
else
{
MessageBox.Show("傳送框不能為空!");
}
}
5.資料接收(C# 的SerialPort 控制元件,只能在子程序裡面執行,所以需要使用委託事件來重新整理接收框中的內容,這裡我是以位元組形式接收,方便計算正確的長度private void Com_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
if (Serial1.IsOpen)
{
try
{
//開闢接收緩衝區
byte[] ReDatas = new byte[Serial1.BytesToRead];
//從串列埠讀取資料
Comm_Received_Bytes += Serial1.Read(ReDatas, 0, Serial1.BytesToRead);
//實現資料的解碼與顯示
this.Invoke(updateText, ReDatas);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
6.清空接收發送區(資料太多了,看的累,有什麼難的,點一點不就清空了—哈(-_-))//清空輸入輸出TextBox 傳送接收資料長度及label
private void Serial_Empty_Click(object sender, EventArgs e)
{
Comm_Received_Bytes = 0;
Comm_Send_Bytes = 0;
Serial_SendNum.Text = "傳送:0";
Serial_RecvNum.Text = "接收:0";
Serial_RecvText.Text = "";
Serial_SendText.Text = "";
Serial_Rx_Buff_Hex = "";
Serial_Rx_Buff_Ascii = "";
}
7.注意事項 a.關於串列埠傳送中文亂碼的解決方法(主要就是編碼的問題) 1). 使用System.Text.Encoding 類 的編碼轉換方法(練習時 可以試試)//接收區
string ReDatas = Serial1.ReadExisting();
Comm_Received_Bytes += ReDatas.Length;
byte[] RecvBuff = System.Text.Encoding.GetEncoding("gb2312").GetBytes(ReDatas);
ReDatas = System.Text.Encoding.GetEncoding("gb2312").GetString(RecvBuff );
//實現資料的解碼與顯示
this.Invoke(updateText, ReDatas);
//傳送區
string TxBuff = Serial_SendText.Text;
byte[] Tx = GB2312.GetBytes(TxBuff);
Comm_Send_Bytes += Tx.Length;
Serial_SendNum.Text = "傳送:" + Comm_Send_Bytes.ToString();
Serial1.Write(Tx, 0, Tx.Length);
上面列出了主要的程式碼,其實就是一個串列埠助手。