C#上位机——串口助手高级
第一步:设置界面
右击RiceTextBox,选择置于顶层
选择StatusLabel
第二步:重新对控件进行命名
第三步:设置一些属性
波特率就是一秒钟发送的位数!
using Microsoft.Win32;
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;namespace 串口助手
{public partial class Form1 : Form{private bool isOpen = false;//定义一个变量,表示串口的开或者关闭状态public Form1(){InitializeComponent();}private void Form1_Load(object sender, EventArgs e){//给下拉列表添加端口//this.port_cbb.Items.Add("COM5");//this代表窗口//this.port_cbb.Items.Add("COM6");//初始化各个下拉列表//this.port_cbb.SelectedIndex = 2;//选择port_cbb的第2个是COM3//this.baud_cbb.SelectedIndex = 1;//默认选择9600//this.check_cbb.SelectedIndex = 0;//默认是none//this.databit_cbb.SelectedIndex = 3;//默认是8位//this.stopbit_cbb.SelectedIndex = 0;//默认是1位//给发送框添加预设文本 也就是初始化//this.send_rtb.Text = "我是发送框";//给接收框添加预设文本//this.recive_rtb.Text = "我是接收框";serialLoad();}//自动加载串口的端口号的函数private void serialLoad(){RegistryKey keyCom = Registry.LocalMachine.OpenSubKey(@"Hardware\DeviceMap\SerialComm");string[] sSubKeys = keyCom.GetValueNames();//定义一个数组储存获取注册表中的值port_cbb.Items.Clear();//清空列表中的值foreach (var sValue in sSubKeys){string porName = (string)keyCom.GetValue(sValue);port_cbb.Items.Add(porName);}//初始化各个下拉列表this.port_cbb.SelectedIndex = 0;//选择port_cbb的第2个是COM3this.baud_cbb.SelectedIndex = 1;//默认选择9600this.check_cbb.SelectedIndex = 0;//默认是nonethis.databit_cbb.SelectedIndex = 3;//默认是8位this.stopbit_cbb.SelectedIndex = 0;//默认是1位 }private void button1_Click(object sender, EventArgs e){如果发送的数据不为空,则接收//if (this.send_rtb.Text != "")//{// this.recive_rtb.AppendText(this.send_rtb.Text);//}//else//{// MessageBox.Show("请先输入发送数据");//}try{if (serialPort1.IsOpen == false)//如果没有打开串口{//对串口的属性进行初始化serialPort1.PortName = port_cbb.Text;serialPort1.BaudRate = Convert.ToInt32(baud_cbb.Text);serialPort1.DataBits = Convert.ToInt32(databit_cbb.Text);switch (check_cbb.SelectedIndex){//none odd evencase 0:serialPort1.Parity = System.IO.Ports.Parity.None;break;case 1:serialPort1.Parity = System.IO.Ports.Parity.Odd;break;case 2:serialPort1.Parity = System.IO.Ports.Parity.Even;break;default:serialPort1.Parity = System.IO.Ports.Parity.None;break;}switch (stopbit_cbb.SelectedIndex){case 0:serialPort1.StopBits = System.IO.Ports.StopBits.One;break;case 1:serialPort1.StopBits = System.IO.Ports.StopBits.OnePointFive;break;case 2:serialPort1.StopBits = System.IO.Ports.StopBits.Two;break;default:serialPort1.StopBits = System.IO.Ports.StopBits.One;break;}//串口属性设置完毕,打开串口serialPort1.Open();isOpen = true;//修改按钮的文字open_btn.Text = "关闭串口";}else//如果串口已经打开{serialPort1.Close();isOpen = false;open_btn.Text = "打开串口";}//if (port_cbb.Text != "")//{// serialPort1.PortName = port_cbb.Text;//}//serialPort1.Open();//if (serialPort1.IsOpen)//{// MessageBox.Show("serialport is open");//}}catch (Exception ex){MessageBox.Show(ex.ToString() + serialPort1.PortName.ToString());}}private void port_cbb_SelectedIndexChanged(object sender, EventArgs e){}}
}
串口的接收
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Text;
using System.Windows.Forms;namespace 串口助手
{public partial class Form1 : Form{private bool isOpen = false;//SerialPortpublic Form1(){InitializeComponent();Control.CheckForIllegalCrossThreadCalls = false;}private void Form1_Load(object sender, EventArgs e){//在设计页面已经预先添加了COM1 COM2 COM3 //this.port_cbb.Items.Add("COM4");//this.port_cbb.Items.Add("COM5");//this.port_cbb.SelectedIndex = 2;//this.baud_cbb.SelectedIndex = 1;//this.check_cbb.SelectedIndex = 0;//this.databit_cbb.SelectedIndex = 3;//this.stopbit_cbb.SelectedIndex = 0;// 给发送框添加预设文本// this.send_rtb.Text = "我是发送框";// 给接收框添加预设文本// this.recive_rtb.Text = "我是接收框";serialLoad();}//自动加载串口端口号private void serialLoad(){RegistryKey keyCom = Registry.LocalMachine.OpenSubKey(@"Hardware\DeviceMap\SerialComm");string[] sSubKeys = keyCom.GetValueNames();port_cbb.Items.Clear();foreach (var sValue in sSubKeys){string portName = (string)keyCom.GetValue(sValue);port_cbb.Items.Add(portName);}this.port_cbb.SelectedIndex = 0;this.baud_cbb.SelectedIndex = 1;this.check_cbb.SelectedIndex = 0;this.databit_cbb.SelectedIndex = 3;this.stopbit_cbb.SelectedIndex = 0;}private void send_btn_Click(object sender, EventArgs e){// 如果发送的数据不为空,则接收if (this.send_rtb.Text != "" && serialPort1.IsOpen){//this.recive_rtb.AppendText(this.send_rtb.Text);serialPort1.Write(send_rtb.Text);//发送数据}else{MessageBox.Show("请先输入发送数据!");}}private void open_btn_Click(object sender, EventArgs e){try{if (serialPort1.IsOpen == false){serialPort1.PortName = port_cbb.Text;serialPort1.BaudRate = Convert.ToInt32(baud_cbb.Text);serialPort1.DataBits = Convert.ToInt32(databit_cbb.Text);switch (check_cbb.SelectedIndex){// none odd even case 0:serialPort1.Parity = Parity.None;break;case 1:serialPort1.Parity = Parity.Odd;break;case 2:serialPort1.Parity = Parity.Even;break;default:serialPort1.Parity = Parity.None;break;}switch (stopbit_cbb.SelectedIndex){// 1 1.5 2case 0:serialPort1.StopBits = StopBits.One;break;case 1:serialPort1.StopBits = StopBits.OnePointFive;break;case 2:serialPort1.StopBits = StopBits.Two;break;default:serialPort1.StopBits = StopBits.One;break;}serialPort1.Open();isOpen = true;open_btn.Text = "关闭串口";}else{serialPort1.Close();isOpen = false;open_btn.Text = "打开串口";}}catch (Exception ex){MessageBox.Show(ex.ToString()+serialPort1.PortName.ToString());}}//数据接收private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e){string dataRecive = serialPort1.ReadExisting();recive_rtb.AppendText(dataRecive);}}
}