一个串口多种形式 [英] One serial port in multiple forms

查看:49
本文介绍了一个串口多种形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问你们关于在一个项目中将一个串行端口传递给多个表单的问题.我完成了将它从主"表单传递到Form1"但我不知道如何通过按钮、组合框等处理它.我想以单独的形式更改串行端口参数.我是 C# 新手,无法弄清楚.

I would like to ask you folks about passing one serial port to multiple forms in one project. I acomplished to passed it from "main" form to "Form1" but I don't know how to handle it by buttons, combobox, etc. I want to change serial port parameters in seperate form. I'm quite new in c# and i can't figure it out.

表单主"

namespace RS232
{
public partial class mainform : Form
{
    Form1 frm1;
    Form2 frm2;

    public mainform()
    {
        InitializeComponent();
        this.Text = "Mitasubaszi controller";
        frm1 = new Form1(sport);
        frm2 = new Form2();            
    }      

    public void toolStripMenuItem9_Click(object sender, EventArgs e)
    {
        DialogResult resultquit;
        resultquit = MessageBox.Show("Are you sure?","Warning", MessageBoxButtons.YesNo);
        if (resultquit == System.Windows.Forms.DialogResult.Yes)
        {
            if (sport.IsOpen) sport.Close();
            Close();
        }           

    }

    private void toolStripMenuItem6_Click(object sender, EventArgs e)
    {
        frm1.ShowDialog();   
    }


    private void toolStripMenuItem5_Click(object sender, EventArgs e)
    {
        frm2.ShowDialog();
    }
}
}

表单Form1"

namespace RS232
{

public partial class Form1 : Form
{

    int brate;


    public Form1(System.IO.Ports.SerialPort main)
    {
        InitializeComponent();
        initvalue(main);                     
    }      


    public void initvalue(System.IO.Ports.SerialPort initport)
    {   
        boxbaud.SelectedIndex = 5;
        boxdata.SelectedIndex = 3;
        boxpar.SelectedIndex = 0;
        boxstop.SelectedIndex = 2;
        checkDTR.Checked = true;
        checkRTS.Checked = true;
        initport.BaudRate = 9600;
        initport.DataBits = 8;
        initport.PortName = "COM1";
        initport.DtrEnable = true;
        initport.RtsEnable = true;
        initport.Parity = System.IO.Ports.Parity.Even;
        initport.StopBits = System.IO.Ports.StopBits.Two;           
    }    

    public void button1_Click(object sender, EventArgs e)
    {
        this.Close();            
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void DTR_CheckedChanged(object sender, EventArgs e)
    {
        if (checkDTR.Checked) .DtrEnable = true;
        else .DtrEnable = false;            
    }

    public void numericUpDown1_ValueChanged(object sender, EventArgs e)
    {
        try
        {
            if (.IsOpen) .Close();
            int comvalue = Convert.ToInt32(numericUpDown1.Value);
            string comport = String.Format("COM{0}", comvalue);

        }
        catch
        {
            MessageBox.Show("Port does not exist");
        }
    }

    public void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (boxbaud.SelectedIndex == -1) MessageBox.Show("Baud Rate is not selected", "Warning");
        else
        {
            brate = Convert.ToInt32(boxbaud.SelectedItem);

        }

    }

    private void button7_Click(object sender, EventArgs e)
    {
        string stat;
        if (.IsOpen) stat = "Open";
        else stat = "Close";
        string br = Convert.ToString(.BaudRate);
        string dts = Convert.ToString(.DtrEnable);
        string com = Convert.ToString(.PortName);
        string par = Convert.ToString(.Parity);
        string dat = Convert.ToString(.DataBits);
        string rts = Convert.ToString(.RtsEnable);
        string stop = Convert.ToString(.StopBits);
        string text = string.Format("Status: {0}\nPort: {1}\nBaud rate: {2}\nData bits: {3}\nParity: {4}\nStop bits: {5}\nDTS: {6}\nRTS {7}", stat, com, br, dat, par, stop, dts, rts);
        MessageBox.Show(text);
    }

    private void checkRTS_CheckedChanged(object sender, EventArgs e)
    {
        if (checkRTS.Checked) .RtsEnable = true;
        else .RtsEnable = false;
    }

    private void boxpar_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (boxpar.SelectedItem == "Even") .Parity = System.IO.Ports.Parity.Even;
        if (boxpar.SelectedItem == "Mark") .Parity = System.IO.Ports.Parity.Mark;
        if (boxpar.SelectedItem == "None") .Parity = System.IO.Ports.Parity.None;
        if (boxpar.SelectedItem == "Odd") .Parity = System.IO.Ports.Parity.Odd;
        if (boxpar.SelectedItem == "Space") .Parity = System.IO.Ports.Parity.Space;
    }

    private void boxdata_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (boxdata.SelectedIndex == -1) MessageBox.Show("Data bits are not selected", "Warning");
        else
        {
            int bdata = Convert.ToInt32(boxdata.SelectedItem);
            .DataBits = bdata;
        }
    }

    private void boxstop_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (boxstop.SelectedItem == "1") .StopBits = System.IO.Ports.StopBits.One;
        if (boxstop.SelectedItem == "1.5") .StopBits = System.IO.Ports.StopBits.OnePointFive;
        if (boxstop.SelectedItem == "2") .StopBits = System.IO.Ports.StopBits.Two;
    }

    private void button2_Click(object sender, EventArgs e)
    {
        try
        {
            if (!.IsOpen) .Open();
        }
        catch
        {
            MessageBox.Show("Invalid port parameters");
        }
    }

    private void button3_Click(object sender, EventArgs e)
    {
        if (.IsOpen) .Close(); 
    }  


}
}

推荐答案

如果你想在你的整个应用程序中只有一个 SerialPort,你可以创建一个 static 类具有 SerialPort 类型的静态属性.

If you want to have a single SerialPort in your whole application, You can create an static class with a static Property of type SerialPort.

public static class SerialPortCommunicator
{
   private static SerialPort _serialPort=new SerialPort();

   public static SerialPort SerialPort
    {
        get { return _serialPort; }
        set { _serialPort = value; }
    }
}

现在您可以从项目内的任何地方更改其属性的值.

Now you can change the value of its properties from everywhere inside your project.

 public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_InForm1_Click(object sender, EventArgs e)
        {
            SerialPortCommunicator.SerialPort.DtrEnable = true;
            // Manipulate every other properties here (inside this from)
        }
    }



public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }

    private void button1_InFrom2_Click(object sender, EventArgs e)
    {
        SerialPortCommunicator.SerialPort.BaudRate = 4900;
        // Manipulate every other properties here(inside this form as well)
    }
}

这篇关于一个串口多种形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆