기술나눔

직렬포트ToTcp①

2024-07-11

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

형태

효과: 클라이언트 또는 서버가 메시지를 보낼 때 직렬 포트 및 네트워크 포트 옆에 있는 패널이 깜박입니다. 데이터를 받거나 보낼 때 아래 텍스트 상자의 수가 증가합니다. 하트비트 메커니즘 라디오 버튼을 켜거나 끌 수 있습니다. 하트비트 간격과 내용을 설정할 수 있으며 재설정 버튼은 직렬 포트 데이터를 재설정하고 저장 버튼은 직렬 포트 데이터를 저장합니다(groupBox: 프롬프트 상자, 패널, 패널, 콤보 상자 드롭다운 상자, radioButton, 라디오 버튼, checkBox, 다중 선택 상자)

ini 파일:

Debug 경로에 파일을 생성하고 파일에 Setting.ini를 생성합니다.

구성 파일 읽기:

문자열 dirPath = Path.Combine(Application.StartupPath, "파일");

문자열 파일 경로 = Path.Combine(dirPath, "Setting.ini");

Ini = new IniHelper(파일 경로);

  1. namespace SerialportToTCP
  2. {
  3. public partial class Form1 : Form
  4. {
  5. IniHelper Ini;
  6. string[] botelvs = new string[] { "1200", "4800", "9600", "13200" };
  7. public Form1()
  8. {
  9. InitializeComponent();
  10. //1 读取配置文件
  11. string dirPath = Path.Combine(Application.StartupPath, "File");// debug/file
  12. string filePath = Path.Combine(dirPath, "Setting.ini");// debug / file/setting.ini
  13. Ini = new IniHelper(filePath); //创建读取对象
  14. // 添加串口
  15. comboBox1.Items.AddRange(SerialPort.GetPortNames());// 获取所有串口 拼接在下拉框的items中
  16. comboBox2.Items.AddRange(botelvs);// 添加波特率数组
  17. comboBox2.Items.Add("自定义");//添加一个
  18. comboBox3.Items.AddRange(new string[] { "5", "6", "7", "8" });
  19. comboBox4.Items.AddRange(new string[] { "无", "奇校检", "偶校检" });
  20. comboBox5.Items.AddRange(new string[] { "无", "1", "2", "1.5" });
  21. //2开始处理串口接受数据事件
  22. //处理串口的数据
  23. this.serialPort1.DataReceived = SerialPort1_DataReceived;
  24. //3 处理界面显示默认值 也就是从ini文件读取数据
  25. readSetting();
  26. //4 开始串口通信
  27. startChuanKou();
  28. //5 开始网口通信
  29. startTCP();
  30. }
  31. //开始搭建TCP服务器
  32. TcpListener listen;