一.上位机模板界面展示
二.这个程序所的控件
1.button
2.comboBox
3.label
4.checkBox
5.textBox
6.groupBox
7.timer
8.serialPort
9.statusStrip
三.代码展示
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;
using System.Runtime.InteropServices;
using System.Threading;
using Microsoft.Win32;
namespace 串口助手
{
public partial class MainForm : Form
{
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath); // 系统dll导入ini写函数
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath); // 系统dll导入ini读函数
string FileName = System.AppDomain.CurrentDomain.BaseDirectory + "Backup.ini"; // ini 文件名
StringBuilder BackupBuf = new StringBuilder(50);//存储读出ini内容变量
private bool isRxShow = true;
private string strRead;
public MainForm()
{
InitializeComponent();
serialPort1.Encoding = Encoding.GetEncoding("GB2312"); //支持汉字
//System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
}
private void RefreshSerialPortList(SerialPort serialPort, ComboBox comboBox)
{
string[] computerPortName = SerialPort.GetPortNames();
comboBox.Items.Clear();
for (int i = 0; i < computerPortName.Length; i++)
{
try
{
serialPort.PortName = computerPortName[i];
serialPort.Open();
comboBox.Items.Add(serialPort.PortName);
serialPort.Close();
}
catch
{
}
}
if (comboBox.Items.Count > 0)
{
comboBox.SelectedIndex = 0;
}
else
{
AppendTextToInfoBox("[" + DateTime.Now.ToString("HH:mm:ss") + "]" + "->", txtRecvInfo);
AppendTextToInfoBox("没检测到可用串口! \r\n", txtRecvInfo);
}
}
private void SerialPortInit()
{
RefreshSerialportList(cmbSerialPort);
string[] baudRates = { "4800", "9600", "115200" };
cmbBaudRate.Items.AddRange(baudRates);
cmbBaudRate.SelectedIndex = 1;
string[] dataBits = { "5", "6", "7", "8" };
cmbDataBits.Items.AddRange(dataBits);
cmbDataBits.SelectedIndex = 3;
string[] stopBits = { "One", "OnePointFive", "Two" };
cmbStopBits.Items.AddRange(stopBits);
cmbStopBits.SelectedIndex = 0;
string[] parity = { "None", "Even", "Odd", "Mark", "Space" };
cmbParity.Items.AddRange(parity);
cmbParity.SelectedIndex = 0;
}
private void AppendTextToInfoBox(string text,TextBox textBox)
{
textBox . AppendText(text);
}
private void MainForm_Load(object sender, EventArgs e)
{
SerialPortInit();
chbCheckCRC.Enabled = false;
// 恢复发送栏
GetPrivateProfileString("串口1", "发送栏", "", BackupBuf, 50, FileName);
txtSendInfo.Text = BackupBuf.ToString();
}
private void btnRefreshSerialPort_Click(object sender, EventArgs e)
{
if (serialPort1.IsOpen == true)
{
serialPort1.Close();
btnOpenOrClose.Text = "打开串口";
AppendTextToInfoBox("[" + DateTime.Now.ToString("HH:mm:ss") + "]" + "->", txtRecvInfo);
AppendTextToInfoBox("扫描和添加串口时,关闭串口。\r\n", txtRecvInfo);
}
RefreshSerialPortList(serialPort1,cmbSerialPort);
}
private void btnOpenOrClose_Click(object sender, EventArgs e)
{
if (serialPort1 != null && serialPort1.IsOpen)
{
// 关闭串口
serialPort1.Close();
btnOpenOrClose.Text = "打开串口";
AppendTextToInfoBox("[" + DateTime.Now.ToString("HH:mm;ss") + "]" + "-> ", txtRecvInfo);
AppendTextToInfoBox("手动关闭串口。\r\n", txtRecvInfo);
timer1.Stop(); // 关闭串口工具异常检测
}
else
{
// 打开串口
try
{
serialPort1.PortName = cmbSerialPort.Text; // 端口号
serialPort1.Open();