2024-07-11
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
Effect: The panel next to the serial port and network port will flash when the client or server sends a message. The textbox below will increase the number when receiving or sending data. The heartbeat mechanism radio button can be turned on and off, and the heartbeat interval and content can be set. The reset button resets the serial port data, and the save button saves the serial port data (groupBox: prompt box, panel, comboBox drop-down box, radioButton, single-choice box, checkBox, multiple-choice box)
ini file:
Create a file in the Debug path, and create Setting.ini in the file
Read the configuration file:
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;
-