tx
2025-04-09 fa7510e1ed63df0366787fa4ed1b3db6426d2b46
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TProduct.Model;
 
namespace TProduct.Link.Kedi
{
    /// <summary>
    /// 无纸数据记录仪(Dr) : 12通道无纸记录仪   1-6通道测试流量,7测试进口压,8-11测试出口压
    /// </summary>
    public class MsgItem_犇流_Dr : MsgItemBase
    {
        byte _address = 0x01;
        byte[] _query_message;
        public override byte[] GetPollSendMessage()
        {
            return _query_message;
        }
        TProduct.Model.WorkBenchMonitorPoint _monitor流量 = null;
        TProduct.Model.WorkBenchMonitorPoint _monitor进口压力 = null;
        TProduct.Model.WorkBenchMonitorPoint _monitor出口压力 = null;
 
        private byte _pressMeterRegisterAddress = 1;
        private int _pressMeterDecimalPointPosition = 1;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="instrumentParas"></param>
        /// <param name="allMonitors"></param>
        /// <returns></returns>
        public bool InitialData(
            TProduct.Model.WorkBenchBase workBench,
            TProduct.Model.WorkBenchInstrumentKedi instrumentParas,
            List<TProduct.Model.WorkBenchMonitorPoint> allMonitors)
        {
            //定死
            _address = 1;
             
            _monitor流量 = allMonitors.Find(x => x.MonitorType == eMonitorType.流量);
 
            _monitor进口压力 = allMonitors.Find(x => x.MonitorType == eMonitorType.压力 && x.Property ==
                TProduct.Model.MonitorTypeProperty.进口);
 
            //1-6通道测试流量,7测试进口压,8-11测试出口压
            _monitor出口压力 = allMonitors.Find(x => x.MonitorType == eMonitorType.压力 && x.Property ==
                TProduct.Model.MonitorTypeProperty.出口);
            //出口压力 , 根据量程 设置, 小数点位,根据现场调试,直接写死
            if (_monitor出口压力 != null)
            {
                if (_monitor出口压力.AnalogParas == null)
                {
                    //_monitor出口压力.AnalogParas = new AnalogMonitorPointParas() { RangeMin = 0, RangeMax = 1 };
                    throw new Exception("Error51 monitor出口压力.AnalogParas 为空, 可能是未选择出口压力计");
                }
                 //   
                if (_monitor出口压力.AnalogParas.RangeMax == 0.4)
                {
                    _pressMeterDecimalPointPosition =3;
                    _pressMeterRegisterAddress = 7;
                }
                if (_monitor出口压力.AnalogParas.RangeMax == 1)
                {
                    _pressMeterDecimalPointPosition = 3;
                    _pressMeterRegisterAddress = 8;
                }
                if (_monitor出口压力.AnalogParas.RangeMax == 2.5)
                {
                    _pressMeterDecimalPointPosition = 3;
                    _pressMeterRegisterAddress = 9;
                }
                if (_monitor出口压力.AnalogParas.RangeMax == 4)
                {
                    _pressMeterDecimalPointPosition = 3;
                    _pressMeterRegisterAddress = 10;
                }
            }
 
            int registerNumber = 12;
            // _query_message = new byte[] { 0x01, 0x04, 0x00, 0x00, 0x00, 0x08, 0xF1, 0xCC };
            _query_message = BuildQueryMessage(_address, Convert.ToUInt16(registerNumber + 2));
 
            return true;
        }
       
        /// <summary>
        /// 构建查询指令
        /// </summary>
        /// <param name="address"></param>
        /// <param name="registerNumber"></param>
        /// <returns></returns>
        private static byte[] BuildQueryMessage(byte address, ushort registerNumber)
        {
            //new byte[] { 0x01, 0x04, 0x00, 0x00, 0x00, 0x08, 0xF1, 0xCC };
            // 读取小数点位数指令  01 03 00 90 00 0F 05 E3 ;
            //byte[] byteMessage = new byte[] {0x01,0x04,0x10,
            //     0x00,0x00,0xFF,0x9c,
            //     0x01,0xF6,0x00,0x00,
            //     0xF8,0x30,0xF8,0X30,
            //     0x00,0x00,0x00,0x00,};
 
            ushort start = 0;
            //ushort registers = 8;
            //构建消息 message数据长度要求包含最后2位的校验码
            var message = new byte[8];
            message[0] = address;//都是0x01
            message[1] = 0x04;//03是读取小数点位数,04是读取整形数据
            message[2] = (byte)(start >> 8);
            message[3] = (byte)start;
            message[4] = (byte)(registerNumber >> 8);
            message[5] = (byte)registerNumber;
 
            //计算16位的CRC校验码 
            byte[] CRC = new byte[2];
            TProduct.RS485.ModBusRtuHelper.GetCRC16(message, ref CRC);
 
            //赋值校验码
            message[message.Length - 2] = CRC[0];
            message[message.Length - 1] = CRC[1];
 
 
 
 
 
            return message;
        }
 
        /// <summary>
        /// 接受消息
        /// </summary>
        public override int AnaPollReceiveValue(
            byte[] byteMessage,
            ref Dictionary<string, string> additionInfos,
            out List<MonitorPointValue> results,
            out string error_info)
        {
            results = new List<MonitorPointValue>();
 
            //备注  这里小数位 是通过配置设置, 也可以通过通讯获取,获取小数位的指令是 01 03 00 90 00 0F 05 E3
 
            // var value流量 = BitConverter.ToSingle(byte流量.Reverse().ToArray(), 0);//采用了IEEE-754二进制浮点数算术标准
 
            if (_monitor流量 != null)
            {
                byte[] byteValue = new byte[2];
                Array.Copy(byteMessage, 3 + 2 * _monitor流量.DigitalParas.RegisterAddress,
                    byteValue, 0, 2);
                float rValue = BitConverter.ToInt16(byteValue.Reverse().ToArray(), 0);
 
                results.Add(new MonitorPointValue(_monitor流量,
                    rValue * Math.Pow(10.0, -_monitor流量.DigitalParas.DecimalPointPosition)));
            }
 
            if (_monitor进口压力 != null)
            {
                byte[] byteValue = new byte[2];
                Array.Copy(byteMessage, 3 + 2 * _monitor进口压力.DigitalParas.RegisterAddress,
                    byteValue, 0, 2);
                float rValue = BitConverter.ToInt16(byteValue.Reverse().ToArray(), 0);
 
                results.Add(new MonitorPointValue(_monitor进口压力,
                    rValue * Math.Pow(10.0, -_monitor进口压力.DigitalParas.DecimalPointPosition)));
            }
 
            if (_monitor出口压力 != null)
            {
                byte[] byteValue = new byte[2];
                Array.Copy(byteMessage, 3 + 2 * _pressMeterRegisterAddress,//_monitor出口压力.DigitalParas.RegisterAddress,
                    byteValue, 0, 2);
                float rValue = BitConverter.ToInt16(byteValue.Reverse().ToArray(), 0);
 
                results.Add(new MonitorPointValue(_monitor出口压力, 
                    rValue * Math.Pow(10.0, -_pressMeterDecimalPointPosition)));
            }
 
            error_info = null;
            return 1;
        }
 
        /// <summary>
        /// 测试指令
        /// </summary>
        public static byte[] BuildQueryMessage( )
        {
            ushort registerNumber = 12;
            return BuildQueryMessage(0x01, registerNumber);
        }
 
        /// <summary>
        /// 用于信号调试
        /// </summary>
        /// <param name="byteMessage"></param>
        /// <returns></returns>
        public static string GetReceiveMessageValues(byte[] byteMessage )
        {
            if (byteMessage == null || byteMessage.Length < 7)
            {
                return "长度不够";
            }
 
            StringBuilder sb = new StringBuilder();
            byte[] byteValue = new byte[2];
 
 
            //1-6通道测试流量,7测试进口压,8-11测试出口压
            Array.Copy(byteMessage, 3 + 2 * 0, byteValue, 0, 2);
            float rValue0 = BitConverter.ToInt16(byteValue.Reverse().ToArray(), 0);
            sb.AppendFormat("寄存器{0}(流量),数据:{1};  ", 0, rValue0);
 
            Array.Copy(byteMessage, 3 + 2 * 1, byteValue, 0, 2);
            float rValue1 = BitConverter.ToInt16(byteValue.Reverse().ToArray(), 0);
            sb.AppendFormat("寄存器{0}(流量),数据:{1};  ", 1, rValue1);
 
            Array.Copy(byteMessage, 3 + 2 * 2,
                byteValue, 0, 2);
            float rValue2 = BitConverter.ToInt16(byteValue.Reverse().ToArray(), 0);
            sb.AppendFormat("寄存器{0}(流量),数据:{1};  ", 2, rValue2);
 
            Array.Copy(byteMessage, 3 + 2 * 3,  byteValue, 0, 2);
            float rValue3 = BitConverter.ToInt16(byteValue.Reverse().ToArray(), 0);
            sb.AppendFormat("寄存器{0}(流量),数据:{1};  ", 3, rValue3);
 
            Array.Copy(byteMessage, 3 + 2 * 4,  byteValue, 0, 2);
            float rValue4 = BitConverter.ToInt16(byteValue.Reverse().ToArray(), 0);
            sb.AppendFormat("寄存器{0}(流量),数据:{1};  ", 4, rValue3);
 
            Array.Copy(byteMessage, 3 + 2 * 5,  byteValue, 0, 2);
            float rValue5 = BitConverter.ToInt16(byteValue.Reverse().ToArray(), 0);
            sb.AppendFormat("寄存器{0}(流量),数据:{1};  ", 5, rValue5);
 
 
 
            Array.Copy(byteMessage, 3 + 2 * 6,  byteValue, 0, 2);
            float rValue6 = BitConverter.ToInt16(byteValue.Reverse().ToArray(), 0);
            sb.AppendFormat("寄存器{0}(进口压),数据:{1};  ", 6, rValue6);
 
 
 
 
 
            Array.Copy(byteMessage, 3 + 2 * 7, byteValue, 0, 2);
            float rValue7 = BitConverter.ToInt16(byteValue.Reverse().ToArray(), 0);
            sb.AppendFormat("寄存器{0}(出口压),数据:{1};  ", 7, rValue7);
 
            Array.Copy(byteMessage, 3 + 2 * 8, byteValue, 0, 2);
            float rValue8 = BitConverter.ToInt16(byteValue.Reverse().ToArray(), 0);
            sb.AppendFormat("寄存器{0}(出口压),数据:{1};  ", 8, rValue8);
 
            Array.Copy(byteMessage, 3 + 2 * 9, byteValue, 0, 2);
            float rValue9 = BitConverter.ToInt16(byteValue.Reverse().ToArray(), 0);
            sb.AppendFormat("寄存器{0}(出口压),数据:{1};  ", 9, rValue9);
 
            Array.Copy(byteMessage, 3 + 2 * 10, byteValue, 0, 2);
            float rValue10 = BitConverter.ToInt16(byteValue.Reverse().ToArray(), 0);
            sb.AppendFormat("寄存器{0}(出口压),数据:{1};  ", 10, rValue10);
 
            Array.Copy(byteMessage, 3 + 2 * 11, byteValue, 0, 2);
            float rValue11 = BitConverter.ToInt16(byteValue.Reverse().ToArray(), 0);
            sb.AppendFormat("寄存器{0}(出口压),数据:{1};  ", 11, rValue11);
 
 
 
 
 
            return sb.ToString(); 
        }
 
 
 
    }
}