窗体
效果:串口和网口旁边的是panel当客户端或者服务器发送消息的时候会闪烁,下面的的textbox当接收到接受或者发送的数据会增加数量,心跳机制单选框可以开关,可设置心跳间隔和内容,重置按钮重置串口数据,保存按钮是保存串口数据(groupBox:提示框,panel,面板,comboBox下拉框,radioButton,单选框,checkBox,多选框)
ini文件:
在路径Debug创建File,file里创建Setting.ini
读取配置文件:
string dirPath = Path.Combine(Application.StartupPath, "File");
string filePath = Path.Combine(dirPath, "Setting.ini");
Ini = new IniHelper(filePath);
namespace SerialportToTCP
{
public partial class Form1 : Form
{
IniHelper Ini;
string[] botelvs = new string[] { "1200", "4800", "9600", "13200" };
public Form1()
{
InitializeComponent();
//1 读取配置文件
string dirPath = Path.Combine(Application.StartupPath, "File");// debug/file
string filePath = Path.Combine(dirPath, "Setting.ini");// debug / file/setting.ini
Ini = new IniHelper(filePath); //创建读取对象
// 添加串口
comboBox1.Items.AddRange(SerialPort.GetPortNames());// 获取所有串口 拼接在下拉框的items中
comboBox2.Items.AddRange(botelvs);// 添加波特率数组
comboBox2.Items.Add("自定义");//添加一个
comboBox3.Items.AddRange(new string[] { "5", "6", "7", "8" });
comboBox4.Items.AddRange(new string[] { "无", "奇校检", "偶校检" });
comboBox5.Items.AddRange(new string[] { "无", "1", "2", "1.5" });
//2开始处理串口接受数据事件
//处理串口的数据
this.serialPort1.DataReceived += SerialPort1_DataReceived;
//3 处理界面显示默认值 也就是从ini文件读取数据
readSetting();
//4 开始串口通信
startChuanKou();
//5 开始网口通信
startTCP();
}
//开始搭建TCP服务器
TcpListener listen;
List<TcpClient> lists = new List<TcpClient>();//存放所有的客户端
void startTCP()
{
if(!int.TryParse(textBox3.Text,out int port) || port < 1 || port >65563)
{
MessageBox.Show("请输入正确的端口号");
}
//开启服务器 接受客户端
try
{
listen = new TcpListener(System.Net.IPAddress.Any, port);
listen.Start(100); //开始监听
panel2.BackColor = Color.Green;
//把多个客户端接受到数组里面 异步接受
new Task(() => {
try
{
while (true)
{
//接收客户端
TcpClient c1 = listen.AcceptTcpClient();
// 把客户端添加到数组里面 群发需要
lists.Add(c1);
//接收客户端发来的消息
tcpReceive(c1);
}
}
catch
{
MessageBox.Show("TCP服务器关闭");
}
}).Start();
}
catch
{
MessageBox.Show("TCP启动失败");
//把tcp关闭等操作
foreach (var item in lists)
{
item.Close(); //关闭所有的客户端
}
listen.Stop();
panel2.BackColor = Color.Gray;
}
}
void startChuanKou()
{
// 配置串口对象
try
{
this.serialPort1.PortName = comboBox1.Text;//配置串口名称
this.serialPort1.BaudRate = int.Parse(comboBox2.Text); //波特率
this.serialPort1.DataBits = int.Parse( comboBox3.Text);
this.serialPort1.StopBits = (StopBits)comboBox5.SelectedIndex;// 正常赋值 StopBits.None 枚举值。正好对应数据0
this.serialPort1.Parity = (Parity)comboBox4.SelectedIndex; //
this.serialPort1.Open();
//亮灯
this.panel1.BackColor = Color.Green;
}
catch
{
MessageBox.Show("打开串口失败");
//停止串口
if(serialPort1.IsOpen) serialPort1.Close();
//灭灯
this.panel1.BackColor = Color.Gray;
}
void readSetting()
{
//先配置串口
comboBox1.SelectedItem = Ini.Read("Serialport", "name", "");
string botelv = Ini.Read("Serialport", "botelv", "9601");
int botelvIndex = Array.IndexOf(botelvs, botelv);// 获取botelv在数组里面的索引值
if (botelvIndex != -1) // 证明波特率在数组里面
{
comboBox2.SelectedIndex= botelvIndex;
comboBox2.DropDownStyle = ComboBoxStyle.DropDownList;
}
else
{
//波特率在数组里面 自定义波特率情况
comboBox2.DropDownStyle = ComboBoxStyle.DropDown; //可编辑的下拉框
//DropDownList 不可编辑的下拉框
comboBox2.Text = botelv;
}
//处理数据位
comboBox3.SelectedItem = Ini.Read("Serialport", "databit", "8");
//处理奇偶校检
comboBox4.SelectedIndex = Ini.Read("Serialport", "parity", 0);
comboBox5.SelectedIndex = Ini.Read("Serialport", "stopbit", 0);
comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
comboBox3.DropDownStyle = ComboBoxStyle.DropDownList;
comboBox4.DropDownStyle = ComboBoxStyle.DropDownList;
comboBox5.DropDownStyle = ComboBoxStyle.DropDownList;
//网口数据的读取
textBox3.Text = Ini.Read("NetWork", "port", "8080");
if( Ini.Read("NetWork", "heartOn", false))
{
radioButton1.Checked = true;
}
else
{
radioButton2.Checked = true;
}
textBox4.Text= Ini.Read("NetWork", "heartTime", "60000");// 心跳间隔
textBox5.Text = Ini.Read("NetWork", "heartData", ""); //心跳包数据
checkBox1.Checked = Ini.Read("NetWork", "heartHex", false);//s是否采用16进制
}
}
}
}