Beispiel #1
0
        private ModBusTcpClient busTcpClient = new ModBusTcpClient("127.0.0.1", 51234, 0xFF);   // 站号255



        #endregion

        private void userButton8_Click(object sender, EventArgs e)
        {
            HslCommunication.OperateResult <byte[]> read = busTcpClient.ReadDiscrete(0, 10);
            if (read.IsSuccess)
            {
                // 共返回2个字节,以下展示手动处理位,分别获取10和线圈的通断情况
                bool coil_0 = (read.Content[0] & 0x01) == 0x01;
                bool coil_1 = (read.Content[0] & 0x02) == 0x02;
                bool coil_2 = (read.Content[0] & 0x04) == 0x04;
                bool coil_3 = (read.Content[0] & 0x08) == 0x08;
                bool coil_4 = (read.Content[0] & 0x10) == 0x10;
                bool coil_5 = (read.Content[0] & 0x20) == 0x20;
                bool coil_6 = (read.Content[0] & 0x40) == 0x40;
                bool coil_7 = (read.Content[0] & 0x80) == 0x80;
                bool coil_8 = (read.Content[1] & 0x01) == 0x01;
                bool coil_9 = (read.Content[1] & 0x02) == 0x02;
            }
            else
            {
                MessageBox.Show(read.ToMessageShowString());
            }
        }
        private void button3_Click(object sender, EventArgs e)
        {
            byte.TryParse(txt_stationNumber.Text?.Trim(), out byte stationNumber);
            if (string.IsNullOrWhiteSpace(txt_address.Text))
            {
                MessageBox.Show("请输入地址");
                return;
            }
            try
            {
                dynamic result = null;
                if (rd_bit.Checked)
                {
                    result = client.ReadCoil(txt_address.Text, stationNumber);
                }
                else if (rd_short.Checked)
                {
                    result = client.ReadInt16(txt_address.Text, stationNumber);
                }
                else if (rd_ushort.Checked)
                {
                    result = client.ReadUInt16(txt_address.Text, stationNumber);
                }
                else if (rd_int.Checked)
                {
                    result = client.ReadInt32(txt_address.Text, stationNumber);
                }
                else if (rd_uint.Checked)
                {
                    result = client.ReadUInt32(txt_address.Text, stationNumber);
                }
                else if (rd_long.Checked)
                {
                    result = client.ReadInt64(txt_address.Text, stationNumber);
                }
                else if (rd_ulong.Checked)
                {
                    result = client.ReadUInt64(txt_address.Text, stationNumber);
                }
                else if (rd_float.Checked)
                {
                    result = client.ReadFloat(txt_address.Text, stationNumber);
                }
                else if (rd_double.Checked)
                {
                    result = client.ReadDouble(txt_address.Text, stationNumber);
                }
                else if (rd_discrete.Checked)
                {
                    result = client.ReadDiscrete(txt_address.Text, stationNumber);
                }

                if (result.IsSucceed)
                {
                    AppendText($"[读取 {txt_address.Text?.Trim()} 成功]:{result.Value}");
                }
                else
                {
                    AppendText($"[读取 {txt_address.Text?.Trim()} 失败]:{result.Err}");
                }
                if (chb_show_package.Checked)
                {
                    AppendText($"[请求报文]{result.Requst}");
                    AppendText($"[响应报文]{result.Response}\r\n");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }