public bool Connection()
        {
            busAsciiClient?.Close();
            busAsciiClient = new ModbusAscii(Station)
            {
                AddressStartWithZero = true,

                IsStringReverse = false
            };
            try
            {
                busAsciiClient.SerialPortInni(sp =>
                {
                    sp.PortName = serialPort.PortName;
                    sp.BaudRate = serialPort.BaudRate;
                    sp.DataBits = serialPort.DataBits;
                    sp.StopBits = serialPort.StopBits;
                    sp.Parity   = serialPort.Parity;
                });
                busAsciiClient.Open();
                IsConnected = true;
                return(IsConnected);
            }
            catch (Exception ex)
            {
                EventscadaException?.Invoke(GetType().Name, ex.Message);
                return(IsConnected);
            }
        }
Esempio n. 2
0
        private void button1_Click(object sender, EventArgs e)
        {
            if (!int.TryParse(textBox2.Text, out int baudRate))
            {
                MessageBox.Show(DemoUtils.BaudRateInputWrong);
                return;
            }

            if (!int.TryParse(textBox16.Text, out int dataBits))
            {
                MessageBox.Show(DemoUtils.DataBitsInputWrong);
                return;
            }

            if (!int.TryParse(textBox17.Text, out int stopBits))
            {
                MessageBox.Show(DemoUtils.StopBitInputWrong);
                return;
            }


            if (!byte.TryParse(textBox15.Text, out byte station))
            {
                MessageBox.Show("station input wrong!");
                return;
            }

            busAsciiClient?.Close( );
            busAsciiClient = new ModbusAscii(station);
            busAsciiClient.AddressStartWithZero = checkBox1.Checked;

            ComboBox2_SelectedIndexChanged(null, new EventArgs( ));
            busAsciiClient.IsStringReverse = checkBox3.Checked;


            try
            {
                busAsciiClient.SerialPortInni(sp =>
                {
                    sp.PortName = comboBox3.Text;
                    sp.BaudRate = baudRate;
                    sp.DataBits = dataBits;
                    sp.StopBits = stopBits == 0 ? System.IO.Ports.StopBits.None : (stopBits == 1 ? System.IO.Ports.StopBits.One : System.IO.Ports.StopBits.Two);
                    sp.Parity   = comboBox1.SelectedIndex == 0 ? System.IO.Ports.Parity.None : (comboBox1.SelectedIndex == 1 ? System.IO.Ports.Parity.Odd : System.IO.Ports.Parity.Even);
                });
                busAsciiClient.Open( );

                button2.Enabled = true;
                button1.Enabled = false;
                panel2.Enabled  = true;

                userControlReadWriteOp1.SetReadWriteNet(busAsciiClient, "100", false);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }