0

我正在尝试制作一个简单的窗口,让我可以基于“默认”串行端口配置 SerialPort。我将在下面显示代码,但首先我将总结它的作用。我创建了初始化所有内容的表单。我将默认 SerialPort 设置为一个值,然后显示对话框。PortName、DataBits 和 BaudRate 都更新为正确的“默认”值。不更新的 2 个是 Parity 和 StopBits。组合框中的选项是正确的,但它显示的值不正确。当我在我的表单上点击取消时,它说停止位(并且大概奇偶校验会说同样的事情)表示枚举值超出了合法范围。但是加载表单时没有显示错误。我被困在这个问题上,有人可以帮忙吗?我' 已多次更改 DefaultSerialPort 设置方法以尝试设置值的不同方式,但它并没有改变我的结果。我试过 SelectedValue,SelectedItem。我已经尝试将属性转换为它们的枚举值类型,我已经完成了 ToString(),但都无济于事。所以现在是代码。

所以这是我的测试。这个想法是我应该在我的表单上点击取消。奇怪的非标准值只是为了表明不同的值正在正确更新。

    [Test]
    public void TestSerialPortDialog()
    {
        var expected = new SerialPort("COM3", 115200, Parity.Odd, 8, StopBits.OnePointFive);
        var actual = SerialPortDialog.GetSerialPortDialog(expected);
        Assert.AreEqual(expected.PortName, actual.PortName);
        Assert.AreEqual(expected.BaudRate, actual.BaudRate);
        Assert.AreEqual(expected.Parity, actual.Parity);
        Assert.AreEqual(expected.DataBits, actual.DataBits);
        Assert.AreEqual(expected.StopBits, actual.StopBits);

    }

串行端口对话框

public class SerialPortDialog : Form
{
    public SerialPortDialog()
    {
        InitializeComponent();
    }
    public static SerialPort GetSerialPortDialog(SerialPort sp)
    {
        SerialPort temp = null;
        using (SerialPortDialog icb = new SerialPortDialog())
        {
            icb.serialPortTablePanel.DefaultSerialPort = sp;
            icb.ShowDialog();
            temp = icb.serialPortTablePanel.InputSerialPort;
        }
        return temp;
    }
    private void InitializeComponent()
    {
        this.SuspendLayout();
        this.serialPortTablePanel = new SerialPortTablePanel();
        this.serialPortTablePanel.Dock = DockStyle.Fill;
        this.Controls.Add(serialPortTablePanel);
        this.MinimumSize = new System.Drawing.Size(280, 400);

        this.ResumeLayout(false);
    }
    private SerialPortTablePanel serialPortTablePanel;
}

串行端口表面板

internal class SerialPortTablePanel : Control
{
    internal SerialPortTablePanel()
    {
        InitializeComponent();
    }
    internal SerialPort InputSerialPort
    {
        get
        {
            var com = comportCombo.SelectedItem.ToString();
            var baud = (int)baudRatCombo.SelectedItem;
            var parity = (Parity)parityCombo.SelectedValue;
            var data = (int)databitCombo.SelectedItem;
            var stop = (StopBits)stopbitCombo.SelectedValue;
            return new SerialPort(com, baud, parity, data, stop);
        }
    }

    private void InitializeComponent()
    {
        System.Console.WriteLine("Initialize");
        var tbl = new TableLayoutPanel();
        var split1 = new SplitContainer();
        var split2 = new SplitContainer();

        this.SuspendLayout();
        tbl.SuspendLayout();
        split1.SuspendLayout();
        split1.Panel1.SuspendLayout();
        split1.Panel2.SuspendLayout();
        split2.SuspendLayout();
        split2.Panel1.SuspendLayout();
        split2.Panel2.SuspendLayout();

        split1.Dock = DockStyle.Fill;
        split1.IsSplitterFixed = true;
        split1.SplitterDistance = split1.Width / 2;
        split1.SplitterWidth = 1;

        split2.Dock = DockStyle.Fill;
        split2.SplitterDistance = 218;

        SetupTablePanel(tbl);
        tbl.Controls.Add(split1, 0, 6);
        tbl.Controls.Add(split2, 0, 5);

        comportCombo.Items.AddRange(GetComPorts());
        comportCombo.Text = "Select COM";

        baudRatCombo.Items.AddRange(GetBaudRate());
        baudRatCombo.Text = "Select BaudRate";

        parityCombo.DataSource = (System.Enum.GetValues(typeof(Parity)));
        parityCombo.Text = "Select Parity";

        databitCombo.Items.AddRange(GetDatabit());
        databitCombo.Text = "Select DataBits";

        stopbitCombo.DataSource = (System.Enum.GetValues(typeof(StopBits)));
        stopbitCombo.Text = "Select StopBits";

        okButton = GetDefaultButton("OK", split1.Panel1);
        okButton.DialogResult = DialogResult.OK;
        okButton.Enabled = false;

        cancelButton = GetDefaultButton("Cancel", split1.Panel2);
        cancelButton.DialogResult = DialogResult.Cancel;

        testButton = GetDefaultButton("Test", split2.Panel1);
        testButton.Click += testButton_Click;

        testLbl.Dock = DockStyle.Fill;
        split2.Panel2.Controls.Add(testLbl);


        split1.ResumeLayout(false);
        split1.Panel1.ResumeLayout(false);
        split1.Panel2.ResumeLayout(false);
        split2.ResumeLayout(false);
        split2.Panel1.ResumeLayout(false);
        split2.Panel2.ResumeLayout(false);
        tbl.ResumeLayout(false);
        this.ResumeLayout(false);
    }
    private void SetupTablePanel(TableLayoutPanel tbl)
    {
        tbl.ColumnCount = 1;
        tbl.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100F));
        tbl.Controls.Add(this.comportCombo, 0, 0);
        tbl.Controls.Add(this.baudRatCombo, 0, 1);
        tbl.Controls.Add(this.parityCombo, 0, 2);
        tbl.Controls.Add(this.databitCombo, 0, 3);
        tbl.Controls.Add(this.stopbitCombo, 0, 4);
        tbl.Dock = DockStyle.Fill;
        tbl.RowCount = 7;
        tbl.RowStyles.Add(new RowStyle(SizeType.Percent, 14.28571F));
        tbl.RowStyles.Add(new RowStyle(SizeType.Percent, 14.28571F));
        tbl.RowStyles.Add(new RowStyle(SizeType.Percent, 14.28571F));
        tbl.RowStyles.Add(new RowStyle(SizeType.Percent, 14.28571F));
        tbl.RowStyles.Add(new RowStyle(SizeType.Percent, 14.28571F));
        tbl.RowStyles.Add(new RowStyle(SizeType.Percent, 14.28571F));
        tbl.RowStyles.Add(new RowStyle(SizeType.Percent, 14.28571F));

        this.Controls.Add(tbl);
    }
    private Button GetDefaultButton(string text, SplitterPanel sp)
    {
        Button butt = new Button();
        butt.Dock = DockStyle.Fill;
        butt.MinimumSize = new Size(40, 40);
        butt.Text = text;

        sp.Controls.Add(butt);
        return butt;
    }
    void testButton_Click(object sender, System.EventArgs e)
    {
        using (var temp = InputSerialPort)
        {
            try
            {
                temp.Open();
                if (temp.IsOpen)
                {
                    okButton.Enabled = true;
                    testLbl.BackColor = Color.PaleGreen;
                }
                else
                {
                    okButton.Enabled = false;
                    testLbl.BackColor = Color.Pink;
                }
                temp.Close();
            }
            catch
            {
                okButton.Enabled = false;
                testLbl.BackColor = Color.Pink;
            }
        }
    }

    private string[] GetComPorts()
    {
        return SerialPort.GetPortNames();
    }
    private object[] GetBaudRate()
    {
        return new object[] { 4800, 9600, 19200, 115200 };
    }
    private object[] GetDatabit()
    {
        return new object[] { 5, 6, 7, 8 };
    }

    public Button okButton;
    public Button cancelButton;
    public Button testButton;
    public SerialPort DefaultSerialPort
    {
        set
        {
            this.comportCombo.SelectedItem = value.PortName; //string[]
            this.baudRatCombo.SelectedItem = value.BaudRate; //object[]
            this.databitCombo.SelectedItem = value.DataBits; //object[]
            this.parityCombo.SelectedItem = value.Parity; //Parity enum
            this.stopbitCombo.SelectedItem = value.StopBits; //StopBits enum
        }
    }

    private ClickComboBox comportCombo = new ClickComboBox();
    private ClickComboBox baudRatCombo = new ClickComboBox();
    private ClickComboBox parityCombo = new ClickComboBox();
    private ClickComboBox databitCombo = new ClickComboBox();
    private ClickComboBox stopbitCombo = new ClickComboBox();
    private Label testLbl = new Label();
}

感谢您的时间

编辑

呃,令人讨厌的部分是我将这段代码放在一个单独的项目中,并且它按预期工作......

    private void Form1_Load(object sender, System.EventArgs e)
    {
        comboBox1.DataSource = SerialPort.GetPortNames();
        comboBox2.DataSource = new int[] { 4800, 9600, 19200, 115200 };
        comboBox3.DataSource = new int[] { 5, 6, 7, 8 };
        comboBox4.DataSource = Enum.GetValues(typeof(Parity));
        comboBox5.DataSource = Enum.GetValues(typeof(StopBits));

        var sp = new SerialPort("COM2");
        comboBox1.SelectedItem = sp.PortName;
        comboBox2.SelectedItem = sp.BaudRate;
        comboBox3.SelectedItem = sp.DataBits;
        comboBox4.SelectedItem = sp.Parity;
        comboBox5.SelectedItem = sp.StopBits;
    }
4

1 回答 1

0

好的,事实证明我遇到这么困难的原因与时间有关..这是一个猜测,但是当我将表单更改为在完成后触发 Load 事件然后设置默认值时,问题就消失了串行端口。

public class SerialPortDialog : Form
{
    public SerialPortDialog()
    {
        InitializeComponent();
        this.Load += SerialPortDialog_Load;
    }

    void SerialPortDialog_Load(object sender, System.EventArgs e)
    {
        if (serial != null)
        {
            serialPortTablePanel.DefaultSerialPort = serial;
        }
    }
    public SerialPortDialog(SerialPort sp) : this()
    {
        this.serial = sp;
    }
    public static SerialPort GetSerialPortDialog(SerialPort sp)
    {
        SerialPort temp = sp;
        using (SerialPortDialog icb = new SerialPortDialog(sp))
        {
            if (icb.ShowDialog() == DialogResult.OK)
                temp = icb.serialPortTablePanel.InputSerialPort;
        }
        return temp;
    }
    private void InitializeComponent()
    {
        this.SuspendLayout();
        this.serialPortTablePanel = new SerialPortTablePanel();
        this.serialPortTablePanel.Dock = DockStyle.Fill;
        this.Controls.Add(serialPortTablePanel);
        this.MinimumSize = new System.Drawing.Size(280, 500);

        this.ResumeLayout(false);
    }
    private SerialPortTablePanel serialPortTablePanel;
    private SerialPort serial;
}
于 2013-05-03T20:14:54.197 回答