上位机开发注意事项

1需要完全理解class用法

2上位机开发用的最多的是通讯控件,比如串口,在一个类中开启了串口,再另一个类中想用串口发数据,你会发现用不了。解决办法有2种:

1做全局变量,注意加 static 每个类去操作串口,有些值是需要上次的结果。

 public static SerialPort serialPort1 = new SerialPort();// 串口控件

 一般加在主程序的地方;

using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

using ui;//需要用到ui的主窗
namespace login
{
    static class Program
    {
        public static SerialPort serialPort1 = new SerialPort();// 串口控件(全局变量)
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            //Application.Run(new login());

            login login = new login();//创建类的实例对象
            DialogResult dialog = login.ShowDialog();//显示登陆窗
            if (dialog == DialogResult.OK)//登陆窗的结果
            {
                Application.Run(new 主窗());//加载主窗体
            }
            else
            {
                Application.Exit();//关闭程序
            }
        }
    }
}

对于静态的变量想调用,需要  类型.serialPort1  才能调用,Program.serialPort1   就和Console.WriteLine("------------------------------");一样

开发标准: 

开发标准:

        新增class类型oop
        在oop内,定义静态【SerialPort    串口】
        在oop内,定义串口操作:初始化,打开,发送,接收,关闭。。。。
    
        在自己的程序里,new出oop对象。然后调用操作。【任何程序位置,想用就new oop】

这种做法类似中间商,让中间商去操作,程序员只需关心调用方法。

 

2利用类的有参构造方法传递。【全局变量 传入 局部变量】

namespace ui
{
    public partial class 主窗 : Form
    {
        public 主窗()
        {
            InitializeComponent();
            this.button5.Click += new System.EventHandler(this.button5_Click);
        }
        public 主窗(SerialPort serialPort1)
        {
            InitializeComponent();
            this.button5.Click += new System.EventHandler(this.button5_Click);
        }

系统默认给的是无参构造,你需要再复制一遍,然后在参数位置,添加一个参数。作用是,当你new出实例对象时,可以把串口做为参数传递过去。

Application.Run(new 主窗(serialPort1));//加载主窗体

然后,你在类中再加个串口属性。在构造方法内,把参数再传给这个属性。下面,整个类内都可以使用这个属性了。

有了以上思路,你可以再优化一下,建个帮助类,这样其他类都可以new调用这个串口。

using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace help
{
    #region 全局
    //====1定义委托============================================
    public delegate void WT_串口接收事件(string data);
    public delegate void WT_发送命令事件(string data);
    #endregion
   
    public  class Help_SerialPort
    {
        #region 字段
        public static SerialPort serialPort1 = new SerialPort();// 串口控件
        public Help_String help_string = new Help_String();
        #endregion
        #region 属性
        //====2生成属性=========================================
        public static WT_串口接收事件 wt_串口接收事件;//静态委托
        public static WT_发送命令事件 wt_串口发送事件;

        public string read_下位机数据 { get; set; }
        public static  uint read_字节 { get;set; }

其他类中可以

Help_SerialPort.serialPort1    来访问这个串口。

3按钮的优化

习惯性的双击弹出进入按钮事件中。这样每个按钮都是进入独立的事件中,实际可以让所有的按钮都使用同一个事件方法。利用按钮的tag属性来判断是ui中哪个按钮。这样代码比较整洁,方便后期维护。

        public FrmMain()
        {
            InitializeComponent();
            InitBtn();
        }

        #region 数字键关联事件


        public void InitBtn()
        {
            this.btn0.Click += new EventHandler(btn_Click);
            this.btn1.Click += new EventHandler(btn_Click);
            this.btn2.Click += new EventHandler(btn_Click);
            this.btn3.Click += new EventHandler(btn_Click);
            this.btn4.Click += new EventHandler(btn_Click);
            this.btn5.Click += new EventHandler(btn_Click);
            this.btn6.Click += new EventHandler(btn_Click);
            this.btn7.Click += new EventHandler(btn_Click);
            this.btn8.Click += new EventHandler(btn_Click);
            this.btn9.Click += new EventHandler(btn_Click);

//=======================================================

//在屏幕上显示数字
        private void btn_Click(object sender, EventArgs e)
        {         
            this.Lable3.Text += ((Button)sender).Tag.ToString();
        }

4自定义类的显示。

自己定义的类,只要有属性,就可以显示出数据,我们用   DataGridView

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace list_Demo
{
    internal class Class_学生
    {
        public string 名字 { get; set; }
        public int 数学 { get; set; } 
        public int 语文 { get; set; }
        public int 英语 { get; set; } 
         
        public Class_学生(string c1_名字, int c2_数学, int c3_语文,int c4_英语)
            //默认有参构造方法
        {
            名字 = c1_名字;
            数学 = c2_数学;
            语文 = c3_语文;
            英语 = c4_英语;
        }
        public void z_自定义方法()
        {
 
        }
    }
}

用 dataGridView1.DataSource =

把new出来的类,给这个控件就完成了。

5 combobox 下拉框

读取的是一个类的对象,想输出某一属性的所有元素

//动态填充课程分类下拉框
this.combobox2.DataSource = get集合();//(combobox课程分类)加课,数据源》数据库
this.combobox2.DisplayMember = "列3";//我们在UI中看到的该列所有成员
this.comobox2.ValueMember = "CategoryId";//保存到数据库使用的外键值

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值