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();
|
}
|
|
|
|
}
|
}
|