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; using System.IO.Ports; namespace SerialPortCom { public partial class SerialPortForm : Form { //public delegate void DEL(byte[] buffer);//定义了一个委托 DEL #region 接收数据事件响应 /// <summary> /// 事件响应函数:每当有串口数据进入就响应 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void DataRecv(object sender, SerialDataReceivedEventArgs e) { int counts = sp.BytesToRead; byte[] buffer = new byte[counts]; //读入(接收)数据 sp.Read(buffer, 0, counts); //DEL changeTextDelegate = new DEL(ChangeText);//实例化委托 委托类型 DEL 把 changeText函数转为DEL类型的委托 //显示(16进制/字符串) if (radioButtonString.Checked)//字符串 { //textBoxRec.Invoke(changeTextDelegate, buffer); //匿名委托 Invoke(new MethodInvoker(delegate () { textBoxRec.Text += Encoding.Default.GetString(buffer); labelReceByte.Text = buffer.Length.ToString(); })); } else//16进制 { Invoke(new MethodInvoker(delegate() { textBoxRec.Text += HexByteToString(buffer); labelReceByte.Text = buffer.Length.ToString(); })); //textBoxRec.Invoke(changeTextDelegate, buffer); } } #endregion #region 串口实例化 SerialPort sp = new SerialPort(); public SerialPortForm() { InitializeComponent(); } #endregion #region 设置串口参数 private void SerialPortForm_Load(object sender, EventArgs e) { string[] comNames = SerialPort.GetPortNames(); for(int i=0; i < comNames.Length; i++) { comboBoxPortNum.Items.Add(comNames[i]); } if(comNames.Length>0) { comboBoxPortNum.SelectedIndex = 0; } comboBoxBaudRate.SelectedIndex = 1; comboBoxDataBit.SelectedIndex = 0; comboBoxCheckBit.SelectedIndex = 0; comboBoxStopBit.SelectedIndex = 0; } #endregion #region 打开串口 private void buttonOpenPort_Click(object sender, EventArgs e) { if(!sp.IsOpen) { sp.PortName = comboBoxPortNum.Text;//配置串口号 sp.BaudRate = Convert.ToInt32(comboBoxBaudRate.Text);//配置波特率 sp.DataBits = Convert.ToInt32(comboBoxDataBit.Text);//配置数据位 //两种string转枚举的方式 sp.Parity = (Parity)Enum.Parse(typeof(Parity),comboBoxCheckBit.Text);//校验位 sp.StopBits = (StopBits)Enum.Parse(typeof(StopBits), comboBoxStopBit.Text); //sp.StopBits = (StopBits)int.Parse(comboBoxStopBit.Text);//停止位 sp.Open();//打开串口 buttonOpenPort.Text = "关闭串口"; buttonOpenPort.BackColor = Color.Green; sp.DataReceived += new SerialDataReceivedEventHandler(DataRecv); } else { sp.Close();//关闭窗口 buttonOpenPort.Text = "打开串口"; buttonOpenPort.BackColor = Color.White; } } #endregion #region 发送 private void buttonSend_Click(object sender, EventArgs e) { if (sp.IsOpen) { byte[] buffer; if (radioButtonString.Checked)//字符串发送 { buffer = Encoding.Default.GetBytes(textBoxSend.Text);//string转ASCCII sp.Write(buffer, 0, buffer.Length); } else//16进制发送 { buffer = StringToHexByte(textBoxSend.Text); sp.Write(buffer, 0, buffer.Length); } labelSendByte.Text = Convert.ToString(buffer.Length); } else { MessageBox.Show(""); } } #endregion #region 字符串转16进制 public byte[] StringToHexByte(string str) { str.Replace(" ", ""); str.Replace("0x", ""); str.Replace("0X", ""); if (str.Length % 2 != 0) { str = "0" + str; } byte[] newbuffer = new byte[str.Length / 2]; for (int i = 0; i < str.Length / 2; i++) { newbuffer[i] = Convert.ToByte(str.Substring(i * 2, 2), 16); } return newbuffer; } #endregion #region 16进制转字符串 public string HexByteToString(byte[] buffer) { string str = ""; if(buffer==null) { return "0"; } for(int i=0;i<buffer.Length;i++) { str += buffer[i].ToString("X2"); } return str; } #endregion #region 清空发送字节 private void buttonClearSendByte_Click(object sender, EventArgs e) { labelSendByte.Text = "0"; } #endregion #region 清空接收字节 private void buttonClearRecvByte_Click(object sender, EventArgs e) { labelReceByte.Text = "0"; } #endregion #region 清空发送区 private void buttonClearSend_Click(object sender, EventArgs e) { textBoxSend.Text = ""; } #endregion #region 清空接收区 private void buttonClearRecv_Click(object sender, EventArgs e) { textBoxRec.Text = ""; } #endregion //private void ChangeText(byte[] buffer) //{ // if (radioButtonString.Checked) // { // textBoxRec.Text += Encoding.Default.GetString(buffer); // labelReceByte.Text = buffer.Length.ToString(); // } // else // { // textBoxRec.Text += HexByteToString(buffer); // labelReceByte.Text = buffer.Length.ToString(); // } //} } }
https://download.csdn.net/download/weixin_38566632/18594094