1、(完整版)自己用C写的一个定时接收的串口调试软件,所有代码以下为所有的原代码:using System;using System。Collections.Generic;using System。ComponentModel;using System.Data;using System.Drawing;using System。Linq;using System。Text;using System。Threading; /添线程引用using System.IO。Ports; /添加串口端口引用using System。Windows.Forms;namespace WindowsFormsA
2、pplication1 public partial class Form1 : Form public Form1() InitializeComponent(); private void Form1_Load(object sender, EventArgs e) Control。CheckForIllegalCrossThreadCalls = false; /处理线程错误问题 / string PortName = System.IO。Ports.SerialPort。GetPortNames(); / 获取串口名字 string PortName = SerialPort.GetP
3、ortNames(); / 获取串口名字 using System。IO.Ports; 添加串口端口引用 if (PortName.Length = 0) MessageBox.Show(请检查串口连接是否存在问题”); else int i; for (i = 0; i PortName.Length; i+) comboBox1。Items。Add(PortNamei); foreach (SerialPort1Baudset rate in Enum.GetValues(typeof(SerialPort1Baudset)) comboBox2。Items.Add((int)rate).
4、ToString(); foreach (SerialPortDatabitsset bitset in Enum。GetValues(typeof(SerialPortDatabitsset))) 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))) comboBox5。Items
5、。Add(Sbset); / 串口端口设置 comboBox1。SelectedIndex = 0; /boBox1.SelectedIndex = PortName。Length-1; this。comboBox2.SelectedIndex = 8; this。comboBox3。SelectedIndex = 3; boBox4.SelectedIndex = 0; this。comboBox5.SelectedIndex = 1; / 端口初始化 ResetPort(); private void ResetPort() / this.serialPort1.BreakState =
6、true; /中断状态 serialPort1.Encoding = Encoding.GetEncoding(GB2312); serialPort1。PortName = comboBox1。Text; serialPort1.BaudRate = int。Parse(comboBox2。Text); serialPort1。DataBits = int.Parse(comboBox3。Text); serialPort1.Parity = (Parity)Enum.Parse(typeof(Parityset), boBox4。Text); / serialPort1.StopBits
7、= (StopBits)Enum.Parse(typeof(Soptbitset), this。comboBox5.Text); serialPort1.ReadTimeout = 500; serialPort1。WriteTimeout = 1000; try serialPort1。Open(); linkLabel1.Text = ”串口已连接; timer1。Enabled = true; catch MessageBox。Show(”串口使用中,请选其它端口 n , ”操作提示); linkLabel1。Text = 串口未连接”; private void comboBox2_S
8、electedIndexChanged(object sender, EventArgs e) /选择波特率 serialPort1。BaudRate = int.Parse(comboBox2。Text); public enum SerialPort1Baudset : int /波特率参数 BaudRate_75 = 75, BaudRate_110 = 110, BaudRate_150 = 150, BaudRate_300 = 300, BaudRate_600 = 600, BaudRate_1200 = 1200, BaudRate_2400 = 2400, BaudRate_
9、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 private void butto
10、n3_Click(object sender, EventArgs e) /退出程序 Application。Exit(); private void comboBox3_SelectedIndexChanged(object sender, EventArgs e) /选择数据位 serialPort1。DataBits = int.Parse(comboBox3.Text); /用于将数字字符串转换为指定的数字类型 public enum SerialPortDatabitsset : int FiveBits = 5, SixBits = 6, SeventBits = 7, Eight
11、Bits = 8 private void comboBox4_SelectedIndexChanged(object sender, EventArgs e) /校验位 serialPort1。Parity = (Parity)Enum.Parse(typeof(Parityset), this。comboBox4。Text); /serialPort1。Parity = (Parity)Enum.Parse(typeof(Parityset), comboBox4.SelectedItem.ToString(); public enum Parityset None, odd, Even,
12、 Mark, Space private void comboBox5_SelectedIndexChanged(object sender, EventArgs e) /停止位 serialPort1。StopBits = (StopBits)Enum.Parse(typeof(StopBits), this。comboBox5.Text); /*public enum Soptbitset None, One, Two, OnePointFive / public string stopbits(int i) string strstop; switch (i) case 0: strst
13、op = ”One; break; case 1: strstop = ”Two”; break; case 2: strstop = OnePointFive”; break; default: strstop = ”One; break; return strstop; private void button2_Click(object sender, EventArgs e) /关闭 serialPort1。Close(); linkLabel1。Text = ”串口未连接; timer1。Enabled=false; private void button1_Click(object
14、sender, EventArgs e) if (serialPort1。IsOpen) MessageBox。Show(串口已经连接”); return; else ResetPort(); private void button5_Click(object sender, EventArgs e) /串口数据发送 if(textBox2.Text =) MessageBox。Show(发送区内数据不能为空); return; try SendStringData(serialPort1); catch (Exception el) MessageBox。Show(el。Message,发送
15、数据异常”); / ReceiveData(serialPort1); /开启接收数据线程 private void SendStringData(SerialPort serialPort1) /传输发送区数据 serialPort1.Write(textBox2。Text); label11.Text = (int。Parse(label11.Text.Trim() + 1)。ToString(); /统计发送区数据 组数 private void ReceiveData(SerialPort serialPort1) /开启接收线程 Thread threadReceiveSub = n
16、ew Thread(new ParameterizedThreadStart(AsyReceiveData)); threadReceiveSub.Start(serialPort1); private void AsyReceiveData(object serialPortobj) /数据接收 SerialPort serialPort = (SerialPort)serialPortobj; System.Threading。Thread。Sleep(500); try textBox3.Text = serialPort.ReadExisting(); label10。Text = (
17、int。Parse(label10。Text。Trim()) + 1)。ToString(); /统计接收区数据 组数 catch (Exception e) MessageBox.Show(e.Message, 接收数据异常); /处理错误 private void button4_Click(object sender, EventArgs e) /清空接收数据 label10.Text = ”0”; label11.Text =”0; private void button6_Click(object sender, EventArgs e) textBox3。Text = ; private void checkBox1_CheckedChanged(object sender, EventArgs e) private void timer1_Tick(object sender, EventArgs e) ReceiveData(serialPort1); /开启接收数据线程