Пример #1
0
 public override bool Open()
 {
     try
     {
         _operator = new USBPortOperator(((USBCfgModel)Configuration).UsbAddress);
         _operator.Open();
     }
     catch (Exception exp)
     {
         throw exp;
     }
     return(true);
 }
 public override bool Open()
 {
     if (IsOpen)
     {
         return(true);
     }
     try
     {
         _operator = new GPIBPortOperator(((GPIBCfgModel)Configuration).GPIBAddress);
         _operator.Open();
     }
     catch (Exception exp)
     {
         throw exp;
     }
     return(true);
 }
Пример #3
0
        private void readData(PortOperatorBase opBase)
        {
            string    result;
            Stopwatch stopwatch = Stopwatch.StartNew();

            try
            {
                result = opBase.ReadLine();
            }
            catch (IOTimeoutException)
            {
                result = "Time Out!";
            }
            catch (Exception ex)
            {
                result = ex.Message;
            }
            listBox1.Items.Add($"[Time:{stopwatch.ElapsedMilliseconds}ms] Read:  {result}");
        }
Пример #4
0
        private void writeCMD(PortOperatorBase opBase)
        {
            isWritingError = false;

            if (string.IsNullOrEmpty(commandTB.Text))
            {
                MessageBox.Show("Command is empty!");
                return;
            }
            string content = commandTB.Text;

            Stopwatch stopwatch = Stopwatch.StartNew();

            try
            {
                opBase.WriteLine(content);
                // cboCommand.AddItem(cboCommand.Text);
            }
            catch
            {
                content = $"写入命令\"{content}\"失败!";
            }
            listBox1.Items.Add($"[Time:{stopwatch.ElapsedMilliseconds}ms] Write: {content}");
        }
Пример #5
0
        private bool NewPortInstance()
        {
            bool hasAddress   = false;
            bool hasException = false;

            DoSomethingForRadioButton(
                () =>
            {
                if (cboRS232.SelectedIndex == -1)
                {
                    return;
                }
                try
                {
                    portOperatorBase = new RS232PortOperator(((Pair <string, string>)cboRS232.SelectedItem).Value.ToString(),
                                                             (int)cboBaudRate.SelectedItem, (SerialParity)cboParity.SelectedItem,
                                                             (SerialStopBitsMode)cboStopBits.SelectedItem, (int)cboDataBits.SelectedItem);
                    hasAddress = true;
                }
                catch
                {
                    hasException = true;
                }
            },
                () =>
            {
                if (cboUSB.SelectedIndex == -1)
                {
                    return;
                }
                try
                {
                    portOperatorBase = new USBPortOperator(cboUSB.SelectedItem.ToString());
                    hasAddress       = true;
                }
                catch
                {
                    hasException = true;
                }
            },
                () =>
            {
                if (cboGPIB.SelectedIndex == -1)
                {
                    return;
                }
                try
                {
                    portOperatorBase = new GPIBPortOperator(cboGPIB.SelectedItem.ToString());
                    hasAddress       = true;
                }
                catch
                {
                    hasException = true;
                }
            },
                () =>
            {
                if (cboLAN.SelectedIndex == -1)
                {
                    return;
                }
                try
                {
                    portOperatorBase = new LANPortOperator(cboLAN.SelectedItem.ToString());
                    hasAddress       = true;
                }
                catch
                {
                    hasException = true;
                }
            });
            if (!hasException)
            {
                portOperatorBase.Timeout = (int)nudTimeout.Value;
            }
            return(hasAddress);
        }
Пример #6
0
 private void openSPbtn_Click(object sender, EventArgs e)
 {
     if (openSPbtn.Text == "Open")
     {
         bool   hasAddress   = false;
         bool   hasException = false;
         string str          = devicesCB.SelectedItem.ToString();
         Console.WriteLine("address:" + str);
         if (!str.Contains("ASRL"))
         {
             printInfo("Select port ERROR!", Color.Red);
             return;
         }
         try
         {
             serialopBase = new RS232PortOperator(str,
                                                  (int)baudRateCB.SelectedItem, 0, 0, 8);
             hasAddress = true;
         }
         catch (Exception ex)
         {
             MessageBox.Show("error1:" + ex.ToString());
             hasException = true;
         }
         if (!hasException)
         {
             hasException         = false;
             serialopBase.Timeout = 5000;
             try
             {
                 serialopBase.Open();
                 openSPbtn.Text = "Close";
                 //EnableControl(false);
             }
             catch (Exception ex)
             {
                 MessageBox.Show("error2:" + ex.ToString());
                 hasException = true;
             }
         }
         if (!hasException)
         {
             printInfo("Open Serial port successful!", Color.Green);
         }
         else
         {
             printInfo("Open Serial port fail!", Color.Red);
         }
     }
     else
     {
         bool hasException = false;
         try
         {
             serialopBase.Close();
         }
         catch { hasException = true; }
         openSPbtn.Text = "Open";
         //EnableControl(true);
         if (!hasException)
         {
             printInfo("Close Serial port successful!", Color.Green);
         }
         else
         {
             printInfo("Close Serial port fail!", Color.Red);
         }
     }
 }