Exemplo n.º 1
0
        private void but_sendData_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(txt_dataPackage.Text))
                {
                    MessageBox.Show("请输入要发送的报文");
                    return;
                }
                var dataPackageString = txt_dataPackage.Text.Replace(" ", "");
                if (dataPackageString.Length % 2 != 0)
                {
                    MessageBox.Show("请输入正确的的报文");
                    return;
                }

                var dataPackage = DataConvert.StringToByteArray(txt_dataPackage.Text?.Trim(), false);
                var msg         = client.SendPackage(dataPackage);
                AppendText($"[请求报文]{string.Join(" ", dataPackage.Select(t => t.ToString("X2")))}\r");
                AppendText($"[响应报文]{string.Join(" ", msg.Select(t => t.ToString("X2")))}\r\n");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                client.Close();
                client.Open();
            }
        }
Exemplo n.º 2
0
 private void but_open_Click(object sender, EventArgs e)
 {
     try
     {
         var PortName = cb_portNameSend.Text.ToString();
         var BaudRate = int.Parse(cb_baudRate.Text.ToString());
         var DataBits = int.Parse(txt_dataBit.Text.ToString());
         var StopBits = (StopBits)int.Parse(txt_stopBit.Text.ToString());
         var parity   = cb_parity.SelectedIndex == 0 ? Parity.None : (cb_parity.SelectedIndex == 1 ? Parity.Odd : Parity.Even);
         client?.Close();
         client = new ModbusAsciiClient(PortName, BaudRate, DataBits, StopBits, parity);
         var result = client.Open();
         if (result.IsSucceed)
         {
             but_open.Enabled        = false;
             cb_portNameSend.Enabled = false;
             but_read.Enabled        = true;
             but_write.Enabled       = true;
             but_open.Enabled        = false;
             but_close.Enabled       = true;
             but_sendData.Enabled    = true;
             AppendText("连接成功");
         }
         else
         {
             AppendText($"连接失败:{result.Err}");
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
     }
 }
Exemplo n.º 3
0
        private void but_open_Click(object sender, EventArgs e)
        {
            try
            {
                var PortName = cb_portNameSend.Text.ToString();
                var BaudRate = int.Parse(cb_baudRate.Text.ToString());
                var DataBits = int.Parse(txt_dataBit.Text.ToString());
                var StopBits = (StopBits)int.Parse(txt_stopBit.Text.ToString());
                var parity   = cb_parity.SelectedIndex == 0 ? Parity.None : (cb_parity.SelectedIndex == 1 ? Parity.Odd : Parity.Even);
                client?.Close();

                EndianFormat format = EndianFormat.ABCD;
                switch (comboBox1.SelectedIndex)
                {
                case 0:
                    format = EndianFormat.ABCD;
                    break;

                case 1:
                    format = EndianFormat.BADC;
                    break;

                case 2:
                    format = EndianFormat.CDAB;
                    break;

                case 3:
                    format = EndianFormat.DCBA;
                    break;
                }
                client = new ModbusAsciiClient(PortName, BaudRate, DataBits, StopBits, parity, format: format);
                var result = client.Open();
                if (result.IsSucceed)
                {
                    but_open.Enabled        = false;
                    cb_portNameSend.Enabled = false;
                    but_read.Enabled        = true;
                    but_write.Enabled       = true;
                    but_open.Enabled        = false;
                    but_close.Enabled       = true;
                    but_sendData.Enabled    = true;
                    AppendText("连接成功");
                    ControlEnabledFalse();
                }
                else
                {
                    AppendText($"连接失败:{result.Err}");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        public async Task 长连接主动开关()
        {
            client.Open();

            short Number = 33;

            client.Write("4", Number, stationNumber);
            Assert.True(client.ReadInt16("4", stationNumber).Value == Number);

            Number = 34;
            client.Write("4", Number, stationNumber);
            Assert.True(client.ReadInt16("4", stationNumber).Value == Number);

            Number = 1;
            client.Write("12", Number, stationNumber);
            Assert.True(client.ReadInt16("12", stationNumber).Value == 1);

            Number = 0;
            client.Write("12", Number, stationNumber);
            Assert.True(client.ReadInt16("12", stationNumber).Value == 0);

            int numberInt32 = -12;

            client.Write("4", numberInt32, stationNumber);
            Assert.True(client.ReadInt32("4", stationNumber).Value == numberInt32);

            float numberFloat = 112;

            client.Write("4", numberFloat, stationNumber);
            Assert.True(client.ReadFloat("4", stationNumber).Value == numberFloat);

            double numberDouble = 32;

            client.Write("4", numberDouble, stationNumber);
            Assert.True(client.ReadDouble("4", stationNumber).Value == numberDouble);

            client.Close();
        }
Exemplo n.º 5
0
        private void but_open_Click(object sender, EventArgs e)
        {
            try
            {
                var PortName = cb_portNameSend.Text.ToString();
                var BaudRate = int.Parse(cb_baudRate.Text.ToString());
                var DataBits = int.Parse(txt_dataBit.Text.ToString());
                var StopBits = (StopBits)int.Parse(txt_stopBit.Text.ToString());
                var parity   = cb_parity.SelectedIndex == 0 ? Parity.None : (cb_parity.SelectedIndex == 1 ? Parity.Odd : Parity.Even);
                client?.Close();

                EndianFormat format = EndianFormat.ABCD;
                switch (cmb_EndianFormat.SelectedIndex)
                {
                case 0:
                    format = EndianFormat.ABCD;
                    break;

                case 1:
                    format = EndianFormat.BADC;
                    break;

                case 2:
                    format = EndianFormat.CDAB;
                    break;

                case 3:
                    format = EndianFormat.DCBA;
                    break;
                }
                client = new ModbusAsciiClient(PortName, BaudRate, DataBits, StopBits, parity, format: format);
                var result = client.Open();
                if (result.IsSucceed)
                {
                    but_open.Enabled        = false;
                    cb_portNameSend.Enabled = false;
                    but_read.Enabled        = true;
                    but_brokenline.Enabled  = true;
                    but_write.Enabled       = true;
                    but_open.Enabled        = false;
                    but_close.Enabled       = true;
                    but_sendData.Enabled    = true;
                    AppendText($"连接成功\t\t\t\t耗时:{result.TimeConsuming}ms");
                    ControlEnabledFalse();
                }
                else
                {
                    AppendText($"连接失败:{result.Err}");
                }

                var config = ConnectionConfig.GetConfig();
                config.ModBusAscii_PortName     = PortName;
                config.ModBusAscii_BaudRate     = BaudRate.ToString();
                config.ModBusAscii_DataBits     = DataBits.ToString();
                config.ModBusAscii_StopBits     = StopBits;
                config.ModBusAscii_Parity       = parity;
                config.ModBusAscii_Value        = txt_value.Text;
                config.ModBusAscii_Address      = txt_address.Text;
                config.ModBusAscii_ShowPackage  = chb_show_package.Checked;
                config.ModBusAscii_EndianFormat = format;
                config.SaveConfig();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }