Serial port 多形式的一个串行端口

Serial port 多形式的一个串行端口,serial-port,Serial Port,我想问你们关于在一个项目中将一个串行端口传递给多个表单的问题。我成功地将它从主窗体传递到窗体1,但我不知道如何通过按钮、组合框等来处理它。我想以单独的形式更改串行端口参数。我是c语言的新手,我搞不懂 主窗体 namespace RS232 { public partial class mainform : Form { Form1 frm1; Form2 frm2; public mainform() { InitializeComponent(

我想问你们关于在一个项目中将一个串行端口传递给多个表单的问题。我成功地将它从主窗体传递到窗体1,但我不知道如何通过按钮、组合框等来处理它。我想以单独的形式更改串行端口参数。我是c语言的新手,我搞不懂

主窗体

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();
    }
}
}
表格1

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,可以创建一个静态类,其静态属性类型为SerialPort

现在,您可以在项目中的任何位置更改其属性的值

 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)
    }
}

现在,当程序启动时,我得到了NullReferenceException,或者当我像这样更改类时,得到了TypeInitializationException:公共静态类portclass{public static Form1 frm1=new Form1;公共静态Form2 frm2=new Form2;公共静态串行端口sport{get;set;}}}}Mate。有三个不同的班级。仔细看我的清单。这根本不像你写的东西。您只需要定义一个静态类并定义SerialPort的属性。然后你可以打开你的表格,双击你在表格上的按钮。并在its click方法中写入例如SerialPortCommunicator.SerialPort.BaudRate=4900。让我知道你是否能解决它。对不起,当我写最后一条评论的时候,它看起来不那么矫揉造作。我有问题,因为我甚至不能用你的方式改变价值。当我为例如SerialPortCommunicator.SerialPort.BaudRate=4900编写时,我得到的是NullReferenceException。我无法更改串行端口的任何参数,因为出现了此异常。好的,我找到它了。我忘了在form1 portclass.sport=new SerialPort;中添加。感谢您所做的一切,并再次对第一条评论表示抱歉:
 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)
    }
}