using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO.Ports; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace daq_testing { public partial class UserControlInstrument : UserControl { public event EventHandler InstrumentConnectButtonClicked; public string VisaAddress { get => tbVisaAddress.Text; set => VisaAddress = value; } private protected void OnInsturmentConnectButtonClicked ( EventArgs e) { InstrumentConnectButtonClicked?.Invoke(this, e); } public UserControlInstrument() { InitializeComponent(); #if DEBUG btnConnectInstrument.Enabled = true; #else btnConnectInstrument.Enabled = false; #endif } // Load available COM ports into ComboBox private void LoadComPorts() { string[] ports = SerialPort.GetPortNames(); cmbComPorts.Items.Clear(); foreach (string port in ports) { cmbComPorts.Items.Add(port); } if (cmbComPorts.Items.Count > 0) { cmbComPorts.SelectedIndex = 0; } } private void UserControlInstrument_Load(object sender, EventArgs e) { } private void btnConnectInstrument_Click(object sender, EventArgs e) { OnInsturmentConnectButtonClicked(EventArgs.Empty); } public void SetConnectButtonActive() { btnConnectInstrument.BackColor = Color.Green; } public void SetConnectButtonNonActive() { btnConnectInstrument.BackColor = Color.Red; } private void btnSearch_Click(object sender, EventArgs e) { LoadComPorts(); } private void cmbComPorts_SelectedIndexChanged(object sender, EventArgs e) { if (cmbComPorts.SelectedItem != null) { string selectedPort = cmbComPorts.SelectedItem.ToString(); string address = $"ASRL{selectedPort.Substring(3)}::INSTR"; Console.WriteLine(address); tbVisaAddress.Text = address; btnConnectInstrument.Enabled = true; } else { MessageBox.Show("Please select a COM port.", "No COM Port Selected", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } } }