Using Yats port settings configuration in a GUI application

By | November 9, 2015

Here are some code snippets on how to reuse the Yats communication port configuration control in your own application. This is a bare minimum required to configure and open any port type.

Port configuration GUI

using yats.TestCase.Parameter;
using yats.Gui.Winforms.Components.ParameterEditor;
using yats.Utilities;
using yats.Ports.Utilities;

class MyForm : Form 
{
        SerialPortSettingsParameter portSettings;
        AbstractPort port;
        public MyForm()
        {
             InitializeComponent();
        }

        private void btConnSettings_Click(object sender, EventArgs e)
        {
            SerialPortSettingsParameter settings = portSettings ?? new SerialPortSettingsParameter();
            AbstractParameterValue res;
            Dictionary<AbstractParameterValue, AbstractParameterValue> dict = new Dictionary<AbstractParameterValue,AbstractParameterValue>();
            dict.Add(settings, settings);
            if (ParameterTabEditorForm.Edit(dict, out res))
            {
                portSettings = settings as SerialPortSettingsParameter;
            }
        }

        private void btConnect_Click(object sender, EventArgs e)
        {
            if (portSettings == null)
            {
                return;
            }
            if (port != null)
            {
                port.Close();
            }
            try
            {
                port = PortOpenHelper.Open(portSettings, null);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        } 
}

 

 

 

Leave a Reply

Your email address will not be published.