|
用C#做的一個(gè)簡易串口助手,可以作為練手使用,再也不用別人的串口助手了HHHH
基本功能都有,代碼也有很多注釋,歡迎下載。
程序運(yùn)行界面:
QQ截圖20190127113359.png (9.92 KB, 下載次數(shù): 118)
下載附件
2019-1-27 11:34 上傳
0.png (38.62 KB, 下載次數(shù): 129)
下載附件
2019-1-27 17:01 上傳
程序源碼如下:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.IO.Ports;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace WinFormSeriport
- {
- public partial class FORM1 : Form
- {
- SerialPort sp = new SerialPort();
-
- public FORM1()
- {
- Control.CheckForIllegalCrossThreadCalls = false;
- InitializeComponent();
- sp.Close();
- lbl_TxRx.Text = "Tx:" +0+" " + "Rx:" + 0;
- }
- private void Form1_Load_1(object sender, EventArgs e)
- {
- Timer_SendDate.Interval = 1000;
- Timer_SendDate.Stop();
- }
-
- private void Form1_MouseMove(object sender, MouseEventArgs e)
- {
- lbl_Move.Text = e.X.ToString() + " " + e.Y.ToString();
- }
-
- private void serialPort1_ErrorReceived(object sender, System.IO.Ports.SerialErrorReceivedEventArgs e)
- {
- MessageBox.Show("接收數(shù)據(jù)時(shí)發(fā)生未知錯(cuò)誤!", "錯(cuò)誤", MessageBoxButtons.OK, MessageBoxIcon.Information);
- }
- private void btn_OpenSerial_Click(object sender, EventArgs e)
- {
- if (sp.IsOpen == false)
- {
- btn_OpenSerial.Text = "開啟串口";
- try
- {
- sp.PortName = comboBox_Com.Text;
- sp.BaudRate = Convert.ToInt32(comboBox_Bot.Text);
- sp.Parity = (System.IO.Ports.Parity)Enum.Parse(typeof(System.IO.Ports.Parity), comboBox_Parity.Text);
- sp.DataBits = Convert.ToInt16(comboBox_DateBit.Text);
- sp.StopBits = (System.IO.Ports.StopBits)Enum.Parse(typeof(System.IO.Ports.StopBits), comboBox_StopBit.Text);
- sp.Encoding = Encoding.Default; //設(shè)置串口編碼為default:獲取操作系統(tǒng)的當(dāng)前 ANSI 代碼頁的編碼。
- sp.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(port_DataReceived);
- sp.Open(); //開啟串口
- if (checkBox_ClearShow.Checked) //如果開啟了自動(dòng)清空功能
- Timer_SendDate.Start();
- btn_OpenSerial.Text = "關(guān)閉串口";
- }
- catch
- {
- MessageBox.Show("配置串口出錯(cuò),檢查各項(xiàng)參數(shù)是否設(shè)置!", "錯(cuò)誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- else
- {
- btn_OpenSerial.Text = "關(guān)閉串口";
- try
- {
- sp.Close();
- btn_OpenSerial.Text = "開啟串口";
- MessageBox.Show("串口已關(guān)閉!","提示",MessageBoxButtons.OK, MessageBoxIcon.Information);
- }
- catch
- {
- MessageBox.Show("出現(xiàn)未知錯(cuò)誤,串口關(guān)閉失敗!", "錯(cuò)誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- }
- string receiveData;
- UInt32 receiveBytesCount;
- string sendData;
- UInt32 sendBytesCount;
- private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
- {
- receiveData = sp.ReadExisting();
- receiveBytesCount += (UInt32)receiveData.Length;
- //字符串顯示
- if (checkBox_ClearShow.Checked == false)
- {
- if (checkBox_16Show.Checked == false) //hex 方式顯示
- {
- richtxtbox_ShowGetNumber.AppendText(receiveData);
- richtxtbox_ShowGetNumber.Focus(); //讓光標(biāo)到這來
- richtxtbox_ShowGetNumber.Select(richtxtbox_ShowGetNumber.TextLength, 0);
- this.richtxtbox_ShowGetNumber.ScrollToCaret(); //設(shè)置光標(biāo)到最后
- }
- //16進(jìn)制顯示
- else
- {
- byte[] recData = System.Text.Encoding.Default.GetBytes(receiveData);// 將接受到的字符串據(jù)轉(zhuǎn)化成數(shù)組;
- foreach (byte str in recData)
- {
- richtxtbox_ShowGetNumber.AppendText(string.Format("{0:X2} ", str));
- }
- }
- }
- lbl_TxRx.Text = "Tx:" + sendBytesCount.ToString()+" " + "Rx:" + receiveBytesCount.ToString();
- }
- private void btn_GetCom_Click(object sender, EventArgs e) //自動(dòng)獲取串口號(hào)
- {
- string[] serialPortName = System.IO.Ports.SerialPort.GetPortNames();
- foreach (string name in serialPortName)
- {
- comboBox_Com.Text = name;
- }
- }
- private void btn_Send_Click(object sender, EventArgs e)
- {
- if (sp.IsOpen == false)
- {
- btn_OpenSerial.Text = "開啟串口";
- MessageBox.Show("請打開串口!", "錯(cuò)誤",MessageBoxButtons.OK, MessageBoxIcon.Information);
- }
- else
- {
- btn_OpenSerial.Text = "關(guān)閉串口";
- if (checkBox_SendByItself.Checked)
- {
- Timer_SendDate.Interval = Convert.ToInt32(textBox_SendTime.Text);
- Timer_SendDate.Start();
- }
- sendData = richTextBox_Send.Text;
- if (checkBox_SendIn16.Checked == false)
- {
- sp.Write(sendData);
- }
- else
- {
- try
- {
- sendData.Replace("0x", ""); //去掉0x
- sendData.Replace("0X", ""); //去掉0X
- // sendData.
- string[] strArray = sendData.Split(new char[] { ',', ',', '\r', '\n', ' ', '\t' });
- int decNum = 0;
- int i = 0;
- byte[] sendBuffer = new byte[strArray.Length]; //發(fā)送數(shù)據(jù)緩沖區(qū)
- foreach (string str in strArray)
- {
- try
- {
- decNum = Convert.ToInt16(str, 16);
- sendBuffer[i] = Convert.ToByte(decNum);
- i++;
- }
- catch
- {
- }
- }
- sp.Write(sendBuffer, 0, sendBuffer.Length);
- //更新發(fā)送數(shù)據(jù)計(jì)數(shù)
- sendBytesCount += (UInt32)sendBuffer.Length;
- lbl_TxRx.Text = "Tx:" + sendBytesCount.ToString()+" " + "Rx:" + receiveBytesCount.ToString();
- }
- catch
- {
- }
- }
- }
- }
- private void Timer_SendDate_Tick(object sender, EventArgs e)
- {
- if (checkBox_ClearShow.Checked)
- richtxtbox_ShowGetNumber.Text = null;
- sendData = richTextBox_Send.Text;
-
- if (sp.IsOpen)
- {
- btn_OpenSerial.Text = "關(guān)閉串口";
- if (checkBox_SendByItself.Checked)
- {
- if (checkBox_SendIn16.Checked == false)
- {
- sp.Write(sendData);
- }
- else
- {
- try
- {
- sendData.Replace("0x", ""); //去掉0x
- sendData.Replace("0X", ""); //去掉0X
- // sendData.
- string[] strArray = sendData.Split(new char[] { ',', ',', '\r', '\n', ' ', '\t' });
- int decNum = 0;
- int i = 0;
- byte[] sendBuffer = new byte[strArray.Length]; //發(fā)送數(shù)據(jù)緩沖區(qū)
-
- foreach (string str in strArray)
- {
- try
- {
- decNum = Convert.ToInt16(str, 16);
- sendBuffer[i] = Convert.ToByte(decNum);
- i++;
- }
- catch
- {
- }
- }
- sp.Write(sendBuffer, 0, sendBuffer.Length);
- }
- catch
- {
- }
- }
- //更新發(fā)送數(shù)據(jù)計(jì)數(shù)
- sendBytesCount += (UInt32)sendData.Length;
- lbl_TxRx.Text = "Tx:" + sendBytesCount.ToString()+" " + "Rx:" + receiveBytesCount.ToString();
- }
- else
- {
- if (checkBox_ClearShow.Checked)
- checkBox_SendByItself.CheckState=CheckState .Unchecked ;
- else
- Timer_SendDate.Stop();
- }
- }
- else
- {
- btn_OpenSerial.Text = "開啟串口";
- Timer_SendDate.Stop();
- MessageBox.Show("串口已經(jīng)關(guān)閉!", "錯(cuò)誤", MessageBoxButtons.OK, MessageBoxIcon.Information);
- }
-
- }
- private void btn_ClearAll_Click(object sender, EventArgs e)
- {
- richtxtbox_ShowGetNumber.Text = null;
- richTextBox_Send.Text = null;
- }
- }
- }
復(fù)制代碼
所有資料51hei提供下載:
WinFormSeriport.zip
(88.05 KB, 下載次數(shù): 311)
2019-1-27 11:35 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
|
評(píng)分
-
查看全部評(píng)分
|