串口助手程序
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; using System.Text.RegularExpressions; namespace USART003 { public partial class Form1 : Form { private const int V = 0x00; string msgShowStr = ""; int timerCount = 0; int beefCount = 0; //Boolean enterBeeflag = false; public Form1() { InitializeComponent(); System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;//设置该属性 为false } private void button1_Click(object sender, EventArgs e) { Boolean isCanSend = true;//判断是否需要发送串口数据 //2. 准备要发送的数据,将其转成byte数组数据 byte[] buffer = new byte[8]; //2.1 判断要发送的数据类型是数值还是字符 if (serialPort1.IsOpen)//判断串口是否打开 { if ("".Equals(input.Text) == false) { if (numModeOfSend.Checked)//数值模式 { //MessageBox.Show("数值"); //将用户输入的字符串转成byte数组数据 int temp = 0; string userInput = input.Text.Trim();//"00 00 00 00 00 00 00 00" if (userInput.Length % 2 != 0) { MessageBox.Show("发送失败,请检查数据格式,发送数据个数必须是偶数"); isCanSend = false; } int j = 0; if (userInput.Length == 1)//假设用户输入1,则改成01,否则下面的for循环进不去 { userInput = "0" + userInput; } /*else if (userInput.Length == 0) { MessageBox.Show("请输入您要发送的数据"); isCanSend = false; }*/ for (int i = 0; i < userInput.Length / 2; i++, j += 2) { string data = userInput.Substring(0 + j, 2); int tempInt = Convert.ToInt32(data); byte tempBtye = Convert.ToByte(tempInt); buffer[i] = Convert.ToByte(data, 10);//将字符串转成16进制的byte数据 } //buffer = BitConverter.GetBytes(temp);// //Convert.ToByte(buffer[0], 16); //buffer = System.Text.Encoding.Default.GetBytes(userInput); } else if (charModeOfSend.Checked)// { MessageBox.Show("字符"); } if (isCanSend) { sendMsg(buffer);//发送串口数据 } } else { MessageBox.Show("请输入您要发送的数据"); } } else { MessageBox.Show("请先打开串口再发送"); } } private void radioButton1_CheckedChanged(object sender, EventArgs e) { } /** @brief 程序启动加载程序 */ private void Form1_Load(object sender, EventArgs e) { /* 1. 初始化端口选择下拉框 * 2. 初始化波特率选择下拉框 */ int i; //1. 端口初始化 for (i = 0; i < 20; i++) { portSelect.Items.Add("COM" + i); } portSelect.Text = "COM9"; //2. 波特率初始化 baudSelect.Text = "115200"; serialPort1.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(port_DataReceive); // msgShowBox.Text += "你好"; } private void port_DataReceive(object sender, SerialDataReceivedEventArgs e) { if (serialPort1.IsOpen) { if (charModeOfReceive.Checked)//字符模式 { string str = serialPort1.ReadExisting(); msgShowBox.AppendText(str+" "); if ("LED1 ON".Equals(str) || "KEY1 ".Equals(str)) { LED1_STATE.BackColor = Color.Red; }else if ("LED1 OFF".Equals(str) || "KEY2 ".Equals(str)) { LED1_STATE.BackColor = Color.White; } if ("LED2 ON".Equals(str)) { LED2.BackColor = Color.Red; } else if ("LED2 OFF".Equals(str)) { LED2.BackColor = Color.White; } else if ("BUZZER".Equals(str))//BUZZER { beef.BackColor = Color.Red; beef.BackColor = Color.Red; beef.BackColor = Color.Red; beef.BackColor = Color.Red; beef.BackColor = Color.Red; beef.BackColor = Color.Red; beefCount = 0; }else if (str.Contains("VCC"))//收到了报文1 { //MessageBox.Show(str); textBox1.Text = str; //将D0~D7解析出来 //如:D0:238 //string[] strArr = str.Split("MSG1");//MSG1:1:2:3:4:5:6:7:8 string[] sarr1 = Regex.Split(str, "}", RegexOptions.IgnoreCase); string[] sarr2 = Regex.Split(str, ":", RegexOptions.IgnoreCase);//MSG1:1:2:3:4:5:6:7:8 //MessageBox.Show(sarr1[0]); #if false for (int i=1;i<strArr.Length;i++) { try { //arr[i] = Convert.ToInt32(strArr[i]); textBox1.Text += strArr[i] + " "; } catch { MessageBox.Show("报文解析错误"); } VCC.Text += strArr[i]+" "; } #endif } //msgShowBox.Text += ""; msgShowStr += str; } else if (numModeOfReceive.Checked)//数值模式接收 { byte data; data = (byte)serialPort1.ReadByte(); string str = Convert.ToString(data, 16).ToUpper(); //msgShowStr += str; msgShowBox.AppendText("0x" + (str.Length == 0 ? "0" + str : str)); //msgShowBox.Text += "0x"; //msgShowBox.Text += str.Length == 1 ? "0" + str : str; } } //add(msgShowStr); //throw new NotImplementedException(); } private void add(string str) { msgShowBox.Text += str; } private void baudSelect_SelectedIndexChanged(object sender, EventArgs e) { } private void input_TextChanged(object sender, EventArgs e) { } private void sendMsg(byte[] buffer) { //2. 写入数据 try { serialPort1.Write(buffer, 0, 1); } catch { MessageBox.Show("串口写入数据出错"); serialPort1.Close(); openPortButton.Enabled = true; closePortButton.Enabled = false; } } private void openPortButton_Click(object sender, EventArgs e) { try { //1. 打开串口 serialPort1.PortName = portSelect.Text; serialPort1.BaudRate = Convert.ToInt32(baudSelect.Text); serialPort1.Open(); openPortButton.Enabled = false; closePortButton.Enabled = true; } catch { if (serialPort1.IsOpen)//判断端口是否已经打开 { serialPort1.Close();//已经打开了,除了问题,一定要先将端口关闭 } MessageBox.Show("端口错误"); } } private void closePortButton_Click(object sender, EventArgs e) { try { serialPort1.Close(); openPortButton.Enabled = true; closePortButton.Enabled = false; } catch { MessageBox.Show("关闭失败"); } } private void portSelect_SelectedIndexChanged(object sender, EventArgs e) { //MessageBox.Show("你好"); } private void label6_Click(object sender, EventArgs e) { } private void panel4_Paint(object sender, PaintEventArgs e) { } int btemp = 0; private void button3_Click(object sender, EventArgs e) { byte[] buffer = new byte[1]; buffer[0] = btemp == 0 ? (byte)0x00 : (byte)0x01; btemp = 1 - btemp; sendMsg(buffer); } int btemp2 = 0; private void button2_Click(object sender, EventArgs e) { byte[] buffer = new byte[1]; buffer[0] = btemp2 == 0 ? (byte)0x03 : (byte)0x04; btemp2 = 1 - btemp2; sendMsg(buffer); } private void button4_Click(object sender, EventArgs e) { byte[] buffer = new byte[1]; buffer[0] = 0x02; sendMsg(buffer); } private void beef_Click(object sender, EventArgs e) { } int countemp = 0; Boolean flag1 = true; private void timer1_Tick(object sender, EventArgs e) { timerCount++; beefCount++; if (beefCount >= 3) { beef.BackColor = Color.White; } } private void beefCnt_Click(object sender, EventArgs e) { } private void LED2_Click(object sender, EventArgs e) { } private void startAllPowerBtn_Click(object sender, EventArgs e) { byte[] buffer = new byte[1]; buffer[0] = 0x05; sendMsg(buffer); } private void button1_Click_1(object sender, EventArgs e) { byte[] buffer = new byte[1]; buffer[0] = 0x06; sendMsg(buffer); } private void button5_Click(object sender, EventArgs e) { msgShowBox.Text = ""; } } }
Fist day