(Tips:本人纯萌新小白一枚,文章只为记录自己的编程历史,温故而知新,能帮助到大家倍感荣幸,有错误的地方劳烦指点!)
不多废话直接进入主题!
本文旨在基于Modbus协议、C#开发语言进行串口工具的开发工作:
首先是界面的设计:
初次设计,界面略显花哨,各位按需进行颜色的设置。
用到的控件有:label(文本)、textBox(文本框)、comboBox(下拉框)、button(按键)、groupBox(组合框)、timer(时钟)、serialPort(串口) 。
由于是初次设计,所以内容较为单薄,且有未实现的功能(例如10H指令并未实现)
首先是Form(主函数)的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.IO.Ports;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
using Modbus.Data;
using Modbus.Device;
using Modbus.Message;
using Modbus调试工具_demo_.Properties;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace Modbus调试工具_demo_
{
public partial class Form1 : Form
{
#region 一些定义
/// <summary>
/// 私有串口实例
/// </summary>
private SerialPort serialPort = new SerialPort();
//定义接收字节数组
byte[] Data = new byte[1024];
byte ReceiveByte;
int ReceiveByteCount = 0;
//定义设备地址
int CurrentAddr = 1;
int Word;
//定义返回报文
string ReceiveData;
#endregion
public Form1()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
}
#region 打开窗口触发事件(存在问题需要修改!!!)
private void Form1_Load(object sender, EventArgs e)
{
//(待程序调整好后启用) MessageBox.Show("欢迎使用本软件!\r\n", "MB调试工具");
this.MaximumSize = this.Size;
this.MinimumSize = this.Size; //固定程序窗口大小;
button1.Text = "打开串口";
button1.BackColor = Color.Green;
button9.BackColor = Color.Yellow;
button3.Enabled = false;
button4.Enabled = false;
button5.Enabled = false;
button6.Enabled = false;
button7.Enabled = false;
checkBox1.Enabled = false;
timer3.Start();
this.textBox3.Text = Settings.Default.One;
this.textBox4.Text = Settings.Default.Two;
this.textBox5.Text = Settings.Default.Three;
this.textBox6.Text = Settings.Default.Four;
this.textBox7.Text = Settings.Default.Five;
this.textBox8.Text = Settings.Default.Six;
this.textBox9.Text = Settings.Default.Seven;
this.textBox10.Text = Settings.Default.Eight;
this.textBox11.Text = Settings.Default.Nine;
#region 保存输入数值代码段(存在错误)
/* string Data_Save = " ";
string[] Data_Set = { " " };
string Data_Path = Application.StartupPath + "\\config_data.ini";
try
{
StreamReader streamReader = new StreamReader(Data_Path);
Data_Save = streamReader.ReadToEnd();
streamReader.Close();
Data_Set = Regex.Split(Data_Save, "||");
}
catch { }
//USART_Scan(serialPort1, comboBox1);
if (Data_Set.Length > 12)
{
try
{
if (Data_Set[0] == "1")
{
checkBox1.Checked = true;
}
else
{
checkBox1.Checked = false;
}
comboBox2.Text = Data_Set[1];
comboBox3.Text = Data_Set[2];
comboBox4.Text = Data_Set[3];
comboBox5.Text = Data_Set[4];
textBox3.Text = Data_Set[5];
textBox4.Text = Data_Set[6];
textBox5.Text = Data_Set[7];
textBox6.Text = Data_Set[8];
textBox7.Text = Data_Set[9];
textBox8.Text = Data_Set[10];
textBox9.Text = Data_Set[11];
textBox10.Text = Data_Set[12];
textBox11.Text = Data_Set[13];
//代码段暂时存在错误,无法使用;
}
catch
{
checkBox1.Checked = false;
comboBox2.Text = "9600";
comboBox3.Text = "No Parity";
comboBox4.Text = "8";
comboBox5.Text = "1";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
textBox6.Text = "";
textBox7.Text = "";
textBox8.Text = "";
textBox9.Text = "";
textBox10.Text = "";
textBox11.Text = "";
}
}
else
{
comboBox2.Text = "9600";
comboBox3.Text = "No Parity";
comboBox4.Text = "8";
comboBox5.Text = "1";
} */
#endregion
}
#endregion
#region 开关串口
private void button1_Click(object sender, EventArgs e)
{
if (button1.Text == "打开串口") //假设当前串口设备为关闭状态;
{
try
{
serialPort1.PortName = comboBox1.Text;
serialPort1.BaudRate = Convert.ToInt32(comboBox2.Text);
serialPort1.DataBits = Convert.ToInt32(comboBox4.Text);
serialPort1.Open();
button1.Text = "关闭串口";
comboBox1.Enabled = false;
comboBox2.Enabled = fal