Example #1
0
        /// <summary>
        /// 自动扫描可连接串口属性
        /// </summary>
        /// <returns></returns>
        private void AutoOpenRead()
        {
            #region 当前选择的配置有效匹配
            FirstItem(ref BaudRateList, int.Parse(cb_baudRate.Text.ToString()));
            FirstItem(ref DataBitList, int.Parse(txt_dataBit.Text.ToString()));
            FirstItem(ref StopBitsList, (StopBits)int.Parse(txt_stopBit.Text.ToString()));
            var firstParity = cb_parity.SelectedIndex == 0 ? Parity.None : (cb_parity.SelectedIndex == 1 ? Parity.Odd : Parity.Even);
            FirstItem(ref ParityList, firstParity);
            #endregion
            byte.TryParse(txt_stationNumber.Text?.Trim(), out byte stationNumber);

            if (!client.ReadInt16("0", stationNumber).IsSucceed)
            {
                foreach (var baudRate in BaudRateList)
                {
                    foreach (var dataBit in DataBitList)
                    {
                        foreach (var stopBits in StopBitsList)
                        {
                            foreach (var parity in ParityList)
                            {
                                for (byte i = 0; i < 255; i++)
                                {
                                    stationNumber = (byte)(i + 1);
                                    if (but_open.Enabled)
                                    {
                                        return;
                                    }
                                    client?.Close();
                                    client = new ModBusRtuClient(cb_portNameSend.Text.ToString(), baudRate, dataBit, stopBits, parity);
                                    var result = client.Open();
                                    if (result.IsSucceed)
                                    {
                                        if (client.ReadInt16("0", stationNumber).IsSucceed)
                                        {
                                            AppendText($@"连接【成功】 端口:{cb_portNameSend.Text.ToString()} 波特率:{baudRate} 数据位:{dataBit} 停止位:{stopBits} 奇偶:{parity} 站号:{stationNumber}");
                                            return;
                                        }
                                        else
                                        {
                                            AppendText($@"连接失败 端口:{cb_portNameSend.Text.ToString()} 波特率:{baudRate} 数据位:{dataBit} 停止位:{stopBits} 奇偶:{parity} 站号:{stationNumber}");
                                        }
                                    }
                                    else
                                    {
                                        AppendText($"连接异常 端口:{cb_portNameSend.Text.ToString()} 波特率:{baudRate} 数据位:{dataBit} 停止位:{stopBits} 奇偶:{parity} 站号:{stationNumber} Err:{result.Err}");
                                    }
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                AppendText($@"连接【成功】 端口:{cb_portNameSend.Text} 波特率:{cb_baudRate.Text} 数据位:{txt_dataBit.Text} 停止位:{txt_stopBit.Text} 奇偶:{cb_parity.Text} 站号:{stationNumber}");
            }
        }
        public async Task 短连接自动开关()
        {
            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);
        }
Example #3
0
        /// <summary>
        /// 读数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void but_read_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);
            }
        }