tx
2025-04-22 e0b138b3e057de6f57021e6c8963868f5c5acc5a
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
using System;
using System.Collections.Generic;
using System.Linq;
using TProduct.Model;
 
namespace TProduct.Link.Kedi
{
    /// <summary>
    /// 无纸数据记录仪(Dr) : 9路
    /// </summary>
    public class MsgItem_力夫_Dr : SiMsg无纸记录仪
    {
        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 byte _pressMeterRegisterAddress出口 = 1;
        private byte _flowMeterRegisterAddress = 0;
        private int _pressMeterDecimalPointPosition = 3;
        private int _flowMeterDecimalPointPosition = 1;
 
        public bool InitialData(
            TProduct.Model.WorkBenchBase workBench,
            Model.WorkBenchInstrumentKedi instrumentParas,
            List<TProduct.Model.WorkBenchMonitorPoint> allMonitors)
        {
            //定死
            _address = 1;
 
            // send_message = new byte[] { 0x01, 0x04, 0x00, 0x00, 0x00, 0x08, 0xF1, 0xCC };
 
 
            _monitor流量 = allMonitors.Find(x => x.MonitorType == eMonitorType.流量);
            _monitor进口压力 = allMonitors.Find(x => x.MonitorType == eMonitorType.压力 && x.Property ==
                TProduct.Model.MonitorTypeProperty.进口);
            _monitor出口压力 = allMonitors.Find(x => x.MonitorType == eMonitorType.压力 && x.Property ==
                TProduct.Model.MonitorTypeProperty.出口);
            if (this._allFlowMeter != null)
            {
                var flowMeter = this._allFlowMeter.Find(x => x.IsHavePipeLine( workBench.PipelineID));
                if (flowMeter != null)
                { 
                    _flowMeterRegisterAddress = Convert.ToByte(flowMeter.RegisterAddress);
                    _flowMeterDecimalPointPosition = flowMeter.DecimalPlaces;
                }
            }
            int registerNumber = 2;
            //if (_monitor流量 != null)
            //{
            //    registerNumber = Math.Max(registerNumber, _monitor流量.DigitalParas.RegisterAddress);
            //}
            //if (_monitor进口压力 != null)
            //{
            //    registerNumber = Math.Max(registerNumber, _monitor进口压力.DigitalParas.RegisterAddress);
            //}
            //if (_monitor出口压力 != null)
            //{
            //    registerNumber = Math.Max(registerNumber, _monitor出口压力.DigitalParas.RegisterAddress);
            //}
 
            _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 };
            //new byte[] { 0x01, 0x04, 0x00, 0x00, 0x00, 0x08, 0xF1, 0xCC };
 
            ushort start = 0; 
            //构建消息 message数据长度要求包含最后2位的校验码
            var message = new byte[8];//03是读取小数点位数,04是读取整形数据
            message[0] = address;//都是0x01
            message[1] = 0x04;//
            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>();
 
            if (_monitor流量 != null)
            {
                byte[] byteValue = new byte[2];
                Array.Copy(byteMessage, 3 + 2 * _flowMeterRegisterAddress,//_monitor流量.DigitalParas.RegisterAddress,
                    byteValue, 0, 2);
                float rValue = BitConverter.ToInt16(byteValue.Reverse().ToArray(), 0);
 
                results.Add(new MonitorPointValue(_monitor流量, rValue * Math.Pow(10.0, -_flowMeterDecimalPointPosition )));//_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 )));//_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)));// _monitor出口压力.DigitalParas.DecimalPointPosition
            }
 
            error_info = null;
            return 1;
        }
 
        /// <summary>
        /// 测试指令
        /// </summary>
        public static byte[] DefaultQueryMessage(ushort registerNumber)
        {
            return BuildQueryMessage(0x01, registerNumber);
            //byte[] byteMessage = new byte[] {0x01,0x04,0x10,
            //     0x00,0x00,0xFF,0x9c,
            //     0x01,0xF6,0x00,0x00,
            //     0xF8,0x30,0xF8,0X30,
            //     0x00,0x00,0x00,0x00,};
 
            //// byte[] byteMessage = new byte[] {0x01,0x04,0x10,
            ////     0x00,0x00,0xFF,0x9c,
            ////     0x01,0xF6,0x00,0x00,
            ////     0xF8,0x30,0xF8,0X30,
            ////     0x00,0x00,0x00,0x00,};
            //byte posi流量1 = 0;
            //byte[] byte流量1 = new byte[2];
            //Array.Copy(byteMessage, 3 + 2 * posi流量1, byte流量1, 0, 2);
            //var value流量1 = BitConverter.ToInt16(byte流量1.Reverse().ToArray(), 0);
 
 
            //byte posi进口压力 = 1;
            //byte[] byte进口压力 = new byte[2];
            //Array.Copy(byteMessage, 3 + 2 * posi进口压力, byte进口压力, 0, 2);
            //var value进口压力 = BitConverter.ToInt16(byte进口压力.Reverse().ToArray(), 0);
 
            //byte posi出口压力 = 2;
            //byte[] byte出口压力 = new byte[2];
            //Array.Copy(byteMessage, 3 + 2 * posi出口压力, byte出口压力, 0, 2);
            //var value出口压力 = BitConverter.ToInt16(byte出口压力.Reverse().ToArray(), 0);
            //var value流量12 = value出口压力 * Math.Pow(10, -3);
 
            //byte posi流量2 = 0;
            //byte[] byte流量2 = new byte[2];
            //Array.Copy(byteMessage, 3 + 2 * posi流量2, byte流量2, 0, 2);
            //var value流量2 = BitConverter.ToInt16(byte流量2.Reverse().ToArray(), 0);
 
            // var value流量 = BitConverter.ToSingle(byte流量.Reverse().ToArray(), 0);//采用了IEEE-754二进制浮点数算术标准
 
        }
 
 
 
 
    }
}