lu
2 天以前 c93910363c60e95266a2f9468c1346e68124adef
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using Newtonsoft.Json.Linq;
using NModbus;
using System;
using System.Data;
using System.IO;
using System.Net.Sockets;
using System.Text;
 
namespace Gs.DevApp
{
    public partial class XtraForm1 : DevExpress.XtraEditors.XtraForm
    {
        public XtraForm1()
        {
            InitializeComponent();
 
        }
 
       
        /// <summary>
        /// 水检
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
 
        private void simpleButton2_Click(object sender, EventArgs e)
        {
            string strCsl1 = "";
            string strSw1 = "";
            string strCsl2 = "";
            string strSw2 = "";
            string strCsl3 = "";
            string strSw3 = "";
            string strCsl4 = "";
            string strSw4 = "";
            string strCsl5 = "";
            string strSw5 = "";
            string SJJG = "";
            try//水检取数,后面补充
            {
                System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
                string hostname = "192.168.60.51";
                ModbusFactory modbusFactory = new ModbusFactory();
                IModbusMaster master = modbusFactory.CreateMaster(new TcpClient(hostname, 502));
                master.Transport.ReadTimeout = 10000;
                master.Transport.Retries = 10000;
                DataTable dt = new DataTable();
                //参数(分别为从站地址,起始地址,长度)
                byte slaveAddress = byte.Parse(1.ToString());
                ushort numberOfPoints = ushort.Parse("1");
                float fsw = 0;
                DataTable dt1 = new DataTable();
                ushort[] csl1 = master.ReadHoldingRegisters(slaveAddress, ushort.Parse(32.ToString()), numberOfPoints);
                strCsl1 = csl1[0].ToString();
 
                ushort[] sw1 = master.ReadHoldingRegisters(slaveAddress, ushort.Parse(31.ToString()), numberOfPoints);
                strSw1 = (float.Parse(sw1[0].ToString()) / 10).ToString();
 
                ushort[] csl2 = master.ReadHoldingRegisters(slaveAddress, ushort.Parse(34.ToString()), numberOfPoints);
                strCsl2 = csl2[0].ToString();
 
                ushort[] sw2 = master.ReadHoldingRegisters(slaveAddress, ushort.Parse(33.ToString()), numberOfPoints);
                strSw2 = (float.Parse(sw2[0].ToString()) / 10).ToString();
 
                ushort[] csl3 = master.ReadHoldingRegisters(slaveAddress, ushort.Parse(36.ToString()), numberOfPoints);
                strCsl3 = csl3[0].ToString();
 
                ushort[] sw3 = master.ReadHoldingRegisters(slaveAddress, ushort.Parse(35.ToString()), numberOfPoints);
                strSw3 = (float.Parse(sw3[0].ToString()) / 10).ToString();
 
                ushort[] csl4 = master.ReadHoldingRegisters(slaveAddress, ushort.Parse(38.ToString()), numberOfPoints);
                strCsl4 = csl4[0].ToString();
 
                ushort[] sw4 = master.ReadHoldingRegisters(slaveAddress, ushort.Parse(37.ToString()), numberOfPoints);
                strSw4 = (float.Parse(sw4[0].ToString()) / 10).ToString();
 
                ushort[] csl5 = master.ReadHoldingRegisters(slaveAddress, ushort.Parse(40.ToString()), numberOfPoints);
                strCsl5 = csl5[0].ToString();
 
                ushort[] sw5 = master.ReadHoldingRegisters(slaveAddress, ushort.Parse(39.ToString()), numberOfPoints);
                strSw5 = (float.Parse(sw5[0].ToString()) / 10).ToString();
 
                bool[] sjjg = master.ReadCoils(slaveAddress, ushort.Parse(77.ToString()), numberOfPoints);
                SJJG = sjjg[0].ToString();
                ///////////////////
                ///
 
                // 3. 读取从站设备的寄存器
                ushort startAddress = 0;   // 寄存器起始地址
                ushort numRegisters = 125;  // 读取 10 个寄存器
                try
                {
                    // 读取保持寄存器(从站地址为 1)
                    ushort[] registers = master.ReadHoldingRegisters(1, startAddress, numRegisters);
                    // 输出结果
                    Console.WriteLine("读取到的寄存器值:");
                    foreach (var register in registers)
                    {
                        Console.WriteLine(register);
                        stringBuilder.Append(register.ToString());
                    }
                    richTextBox1.Text = stringBuilder.ToString();
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"读取错误: {ex.Message}");
                }
 
            }
            catch (Exception ex)
            {
                //VisiblePanel(txtDQBarCode.EditValue.ToString(), ex.Message, "NG", Color.Yellow);
                //txtGWM.EditValue = "";
                return;
            }
 
        }
    }
}