C#串口调试助手
//serialPort1.Parity = (Parity)Enum.Parse(typeof(Parityset), comboBox4.SelectedItem.ToString());
} public enum Parityset {
None, odd, Even, Mark, Space } private void comboBox5_SelectedIndexChanged(object sender, EventArgs e) //停止位 { serialPort1.StopBits = (StopBits)Enum.Parse(typeof(StopBits), boBox5.Text); }
serialPort1.BaudRate = int.Parse(comboBox2.Text); serialPort1.DataBits = int.Parse(comboBox3.Text); serialPort1.Parity = (Parity)Enum.Parse(typeof(Parityset),
namespace WindowsFormsApplication1 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent(); }
private void Form1_Load(object sender, EventArgs e) {
textBox3.Text = serialPort.ReadExisting();
label10.Text = (int.Parse(label10.Text.Trim()) + 1).ToString(); //统计接收区 数据 组数
} catch (Exception e) {
BaudRate_75 = 75, BaudRate_110 = 110, BaudRate_150 = 150, BaudRate_300 = 300, BaudRate_600 = 600, BaudRate_1200 = 1200, BaudRate_2400 = 2400, BaudRate_4800 = 4800, BaudRate_9600 = 9600, BaudRate_14400 = 14400, BaudRate_19200 = 19200, BaudRate_28800 = 28800, BaudRate_38400 = 38400, BaudRate_56000 = 56000, BaudRate_57600 = 57600, BaudRate_115200 = 115200, BaudRate_128000 = 128000, BaudRate_230400 = 230400, BaudRate_256000 = 256000 }
Control.CheckForIllegalCrossThreadCalls = false; //处理线程错误问题
// string[] PortName = System.IO.Ports.SerialPort.GetPortNames(); // 获取串口名字 string[] PortName = SerialPort.GetPortNames(); // 获取串口名字 using
} private void SendStringData(SerialPort serialPort1) //传输发送区数据 {
serialPort1.Write(textBox2.Text);
label11.Text = (int.Parse(label11.Text.Trim()) + 1).ToString(); //统计发送区数据 组数
/*public enum Soptbitset {
None, One, Two, OnePointFive }*/
public string stopbits(int i) {
string strstop; switch (i) {
case 0: strstop = "One"; break;
case 1: strstop = "Two"; break;
comboBox3.Items.Add(((int)bitset).ToString()); } foreach(string str in Enum.GetNames(typeof(Parityset))) {
comboBox4.Items.Add(str); } foreach (string Sbset in Enum.GetNames(typeof(StopBits))) {
foreach (SerialPort1Baudset rate in Enum.GetValues(typeof(SerialPort1Baudset)))
{ comboBox2.Items.Add(((int)rate).ToString());
} foreach (SerialPortDatabitsset bitset in Enum.GetValues(typeof(SerialPortDatabitsset))) {
//关闭
}
private void button1_Click(object sender, EventArgs e) {
if (serialPort1.IsOpen) {
MessageBox.Show("串口已经连接"); return; } else {
ResetPort(); } }
private void button5_Click(object sender, EventArgs e) //串口数据发送 {
serialPort1.Open(); linkLabel1.Text = "串口已连接"; timer1.Enabled = true; } catch { MessageBox.Show("串口使用中,请选其它端口 \n ", "操作提示"); linkLabel1.Text = "串口未连接"; } }
}
private void AsyReceiveData(object serialPortobj) //数据接收 {
SerialPort serialPort = (SerialPort)serialPortobj; System.Threading.Thread.Sleep(500); try {
System.IO.Ports; 添加串口端口引用 if (PortName.Length == 0) { MessageBox.Show("请检查串口连接是否存在问题"); } else { int i; for (i = 0; i < PortName.Length; i++) { comboBox1.Items.Add(PortName[i]); }
case 2: strstop = "OnePointFive"; break;
default: strstop = "One"; break;
}
return strstton2_Click(object sender, EventArgs e) {
serialPort1.Close(); linkLabel1.Text = "串口未连接"; timer1.Enabled=false;
comboBox5.Items.Add(Sbset); } // 串口端口设置
comboBox1.SelectedIndex = 0; //boBox1.SelectedIndex = PortName.Length-1;
boBox2.SelectedIndex = 8; boBox3.SelectedIndex = 3; boBox4.SelectedIndex = 0; boBox5.SelectedIndex = 1;
} private void ReceiveData(SerialPort serialPort1) //开启接收线程 {
Thread threadReceiveSub = new Thread(new ParameterizedThreadStart(AsyReceiveData));
threadReceiveSub.Start(serialPort1);
以下为所有的原代码:
using System; using System.Collections.Generic; using ponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading; //添线程引用 using System.IO.Ports; //添加串口端口引用 using System.Windows.Forms;
boBox4.Text); // serialPort1.StopBits = (StopBits)Enum.Parse(typeof(Soptbitset),
boBox5.Text);
serialPort1.ReadTimeout = 500; serialPort1.WriteTimeout = 1000; try {
if(textBox2.Text =="") {
MessageBox.Show("发送区内数据不能为空"); return; } try {
SendStringData(serialPort1); } catch (Exception el) {
MessageBox.Show(el.Message,"发送数据异常"); } // ReceiveData(serialPort1); //开启接收数据线程