上位机模板程序的展示与学习心得

本文档详细介绍了上位机模板程序的界面展示、控件使用、代码实现,包括串口助手的界面设计、串口配置、数据发送与接收。通过学习,作者总结了编写上位机程序时需要关注硬件特性和应用场景,强调了架构设计、协议选择、接口实现的重要性。
摘要由CSDN通过智能技术生成

一.上位机模板界面展示

二.这个程序所的控件

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();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值