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二进制浮点数算术标准
|
|
}
|
|
|
|
|
}
|
}
|