1068 lines
34 KiB
C#
1068 lines
34 KiB
C#
using Aurora.Api;
|
|
using Aurora.Api.Devices;
|
|
using Hermes;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Markup;
|
|
|
|
namespace AuroraChiller
|
|
{
|
|
public class SmcChiller_HRLMonitoringItems : IMonitoringItems, ICloneable
|
|
{
|
|
public double TargetTempCH1 { get; set; }
|
|
public double CurrentTempCH1 { get; set; }
|
|
public double TargetTempCH2 { get; set; }
|
|
public double CurrentTempCH2 { get; set; }
|
|
public double WarningRange { get; set; }
|
|
public double ErrorRange { get; set; }
|
|
public eTemperatureStatus TempStatus { get; set; }
|
|
public int AlamSt1 { get; set; }
|
|
public int AlamSt2 { get; set; }
|
|
public int AlamSt3 { get; set; }
|
|
public int AlamSt4 { get; set; }
|
|
|
|
public int Status { get; set; }
|
|
|
|
public object Clone()
|
|
{
|
|
return new SmcChiller_HRLMonitoringItems()
|
|
{
|
|
TargetTempCH1 = TargetTempCH1,
|
|
CurrentTempCH1 = CurrentTempCH1,
|
|
TargetTempCH2 = TargetTempCH2,
|
|
CurrentTempCH2 = CurrentTempCH2,
|
|
WarningRange = WarningRange,
|
|
ErrorRange = ErrorRange,
|
|
TempStatus = TempStatus,
|
|
AlamSt1 = AlamSt1,
|
|
AlamSt2 = AlamSt2,
|
|
AlamSt3 = AlamSt3,
|
|
Status = Status
|
|
};
|
|
}
|
|
}
|
|
|
|
public class SmcChiller_HRL : SerialPortDevice, ISmcChiller
|
|
{
|
|
public const byte BYTE_STX = 0x02; // 02
|
|
public const byte BYTE_ETX = 0x03; // 03
|
|
public const byte BYTE_ACK = 0x06; // 06
|
|
public const byte BYTE_NAK = 0x15; // 21
|
|
public const byte BYTE_0 = 0x30; // 48
|
|
public const byte BYTE_1 = 0x31; // 49
|
|
public const byte BYTE_5 = 0x35; // 53
|
|
|
|
public const byte BYTE_CR = 0x0D;
|
|
public const byte BYTE_LF = 0x0A;
|
|
|
|
public const string CMD_PV1 = "PV1";
|
|
public const string CMD_SV1 = "SV1";
|
|
public const string CMD_LOC = "LOC";
|
|
public const string CMD_STR = "STR";
|
|
|
|
public const byte BYTE_REQ_READ = 0x52; // 82
|
|
public const byte BYTE_REQ_WRITE = 0x57; // 87
|
|
|
|
public const byte LENGTH_READ_REQUEST_MSG = 8;
|
|
public const byte LENGTH_WRITE_REQUEST_MSG = 13;
|
|
public const byte LENGTH_SAVE_REQUEST_MSG = 8;
|
|
public const byte LENGTH_RESPONSE_MSG = 14;
|
|
public const byte LENGTH_SLAVE_ADDRESS = 2;
|
|
public const byte LENGTH_WRITE_ADDRESS = 4;
|
|
public const byte LENGTH_CMD = 3;
|
|
public const byte LENGTH_DATA_INTEGER = 4;
|
|
public const byte LENGTH_DATA_DECIMAL_FRACTION = 1;
|
|
public const byte LENGTH_DATA = 4;
|
|
|
|
public const byte LENGTH_READ_REGISTER_MSG = 17;
|
|
public const byte LENGTH_RESPONSE_REGISTER_MSG = 255;
|
|
|
|
private SmcChiller_HRLMonitoringItems MonitoringItems;
|
|
|
|
private bool bIsSimpleCommMode = false;
|
|
|
|
public SmcChillerConfig _Config { get; set; }
|
|
|
|
#region Simulation
|
|
private double SimulTargetTemp;
|
|
#endregion
|
|
|
|
public SmcChiller_HRL(string deviceId) : base(deviceId)
|
|
{
|
|
MonitoringItems = new SmcChiller_HRLMonitoringItems();
|
|
}
|
|
|
|
protected override int DoActivate(IDeviceConfig config)
|
|
{
|
|
_Config = config as SmcChillerConfig;
|
|
|
|
//임시
|
|
if (IsSimul)
|
|
{
|
|
return E1.OK;
|
|
}
|
|
|
|
var result = Open(_Config.ConnConfig as SerialConnConfig);
|
|
if (result != E1.OK) {
|
|
return result;
|
|
}
|
|
|
|
return Init();
|
|
|
|
}
|
|
|
|
public int Open(SmcChillerConfig config)
|
|
{
|
|
var result = Open(config.ConnConfig as SerialConnConfig);
|
|
_Config = config as SmcChillerConfig;
|
|
|
|
return result;
|
|
|
|
}
|
|
public int PortClose()
|
|
{
|
|
return Close();
|
|
}
|
|
private int Init()
|
|
{
|
|
// Set init vaues from config
|
|
if (!IsValidSlaveAddress(_Config.SlaveAddress))
|
|
{
|
|
return E1.GENERIC;
|
|
}
|
|
|
|
if (IsSimul)
|
|
{
|
|
SimulTargetTemp = 10;
|
|
}
|
|
else
|
|
{
|
|
// TargetTemperature 설정은 장치에서 직접 설정한다.
|
|
// SetTargetTemp(_Config.TargetTemperature);
|
|
}
|
|
|
|
return E1.OK;
|
|
}
|
|
|
|
public bool GetConnected()
|
|
{
|
|
if (!IsPortOpen())
|
|
{
|
|
Log.E("SMC Chiller Serial Port is NOT Open!");
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public int GetTemperatureStatus(int Channel)
|
|
{
|
|
double TargetTemperature = 0;
|
|
double CurrentTemperature = 0;
|
|
|
|
int nRetTarget = GetTargetTemp(Channel,out TargetTemperature);
|
|
int nRetCurrent = GetCurrentTemp(Channel,out CurrentTemperature);
|
|
|
|
if (nRetTarget == 0 && nRetCurrent == 0)
|
|
{
|
|
eTemperatureStatus eTemperatureStatus = ValidateTemp(
|
|
TargetTemperature,
|
|
CurrentTemperature,
|
|
MonitoringItems.WarningRange,
|
|
MonitoringItems.ErrorRange);
|
|
|
|
return (int)eTemperatureStatus;
|
|
}
|
|
else return -1;
|
|
|
|
}
|
|
|
|
public bool GetChillerRunningFlag()
|
|
{
|
|
bool bReturn = ((MonitoringItems.Status & (1 << 1)) > 0) ? true : false;
|
|
return bReturn;
|
|
|
|
}
|
|
|
|
private bool IsValidSlaveAddress(int slaveAddress)
|
|
{
|
|
return (1 <= slaveAddress && slaveAddress <= 99);
|
|
}
|
|
|
|
protected override int DoDeactivate()
|
|
{
|
|
if (IsSimul)
|
|
{
|
|
return E1.OK;
|
|
}
|
|
else
|
|
{
|
|
return Close();
|
|
}
|
|
}
|
|
|
|
public int GetTargetTemp(out double temperature)
|
|
{
|
|
temperature = 0;
|
|
return E1.OK; ;
|
|
}
|
|
|
|
public int GetTargetTemp(int Channel, out double temperature)
|
|
{
|
|
if (IsSimul) {
|
|
temperature = SimulTargetTemp;
|
|
return E1.OK;
|
|
}
|
|
if (Channel == 1)
|
|
temperature = MonitoringItems.TargetTempCH1;
|
|
else
|
|
temperature = MonitoringItems.TargetTempCH2;
|
|
return E1.OK;
|
|
}
|
|
|
|
public int GetCurrentTemp(int Channel, out double temperature)
|
|
{
|
|
if (IsSimul)
|
|
{
|
|
// ex) scale = 5, [0.0, 1.0] => [-0.5, 0.5] => [-1, 1] => [-5, 5]
|
|
double scale = 5;
|
|
double randomValue = ((new Random().NextDouble() - 0.5) * 2) * scale;
|
|
|
|
temperature = SimulTargetTemp + randomValue;
|
|
return E1.OK;
|
|
}
|
|
if (bIsSimpleCommMode == false)
|
|
{
|
|
if(Channel == 1)
|
|
temperature = MonitoringItems.CurrentTempCH1;
|
|
else
|
|
temperature = MonitoringItems.CurrentTempCH2;
|
|
return E1.OK;
|
|
}
|
|
return ReadDoubleData(CMD_PV1, out temperature);
|
|
}
|
|
|
|
public int ReadKeyLockSetting(out int settingValue)
|
|
{
|
|
if (IsSimul)
|
|
{
|
|
settingValue = 0;
|
|
return E1.OK;
|
|
}
|
|
|
|
return ReadIntData(CMD_LOC, out settingValue);
|
|
}
|
|
|
|
private int ReadDoubleData(string cmd, out double readData)
|
|
{
|
|
readData = 0;
|
|
|
|
if (!IsPortOpen())
|
|
{
|
|
Log.E("Serial Port is NOT Open!");
|
|
return E1.NETWORK;
|
|
}
|
|
|
|
lock (LockThis)
|
|
{
|
|
byte[] requestMsg = CreateReadRequestMsg(cmd);
|
|
if (Write(requestMsg))
|
|
{
|
|
byte[] responseMsg = new byte[LENGTH_RESPONSE_MSG];
|
|
if (ReadMessage(responseMsg, BYTE_STX, BYTE_ETX) <= 0)
|
|
{
|
|
Log.E("ReadError");
|
|
return E1.GENERIC;
|
|
}
|
|
|
|
var errorCode = ValidateReadResponse(responseMsg, cmd);
|
|
if (errorCode != ChillerErrorCode.NoError)
|
|
{
|
|
Log.E("ExceptionCode : {0}", errorCode.ToString());
|
|
return E1.GENERIC;
|
|
}
|
|
|
|
readData = ExtractDoubleData(responseMsg);
|
|
}
|
|
}
|
|
|
|
return E1.OK;
|
|
}
|
|
|
|
private int ReadIntData(string cmd, out int readData)
|
|
{
|
|
readData = 0;
|
|
|
|
if (!IsPortOpen())
|
|
{
|
|
Log.E("Serial Port is NOT Open!");
|
|
return E1.NETWORK;
|
|
}
|
|
|
|
lock (LockThis)
|
|
{
|
|
byte[] requestMsg = CreateReadRequestMsg(cmd);
|
|
if (Write(requestMsg))
|
|
{
|
|
byte[] responseMsg = new byte[LENGTH_RESPONSE_MSG];
|
|
if (ReadMessage(responseMsg, BYTE_STX, BYTE_ETX) <= 0)
|
|
{
|
|
Log.E("ReadError");
|
|
return E1.GENERIC;
|
|
}
|
|
|
|
var errorCode = ValidateReadResponse(responseMsg, cmd);
|
|
if (errorCode != ChillerErrorCode.NoError)
|
|
{
|
|
Log.E("ExceptionCode : {0}", errorCode.ToString());
|
|
return E1.GENERIC;
|
|
}
|
|
|
|
readData = ExtractIntData(responseMsg);
|
|
}
|
|
}
|
|
|
|
return E1.OK;
|
|
}
|
|
|
|
private byte[] CreateReadRequestMsg(string cmd)
|
|
{
|
|
byte[] requestMsg = new byte[LENGTH_READ_REQUEST_MSG];
|
|
byte[] cmdByte = Encoding.Default.GetBytes(cmd);
|
|
byte[] slaveAddress = GetSlaveAddress();
|
|
|
|
requestMsg[0] = BYTE_STX;
|
|
Array.Copy(slaveAddress, 0, requestMsg, 1, LENGTH_SLAVE_ADDRESS);
|
|
requestMsg[3] = BYTE_REQ_READ;
|
|
Array.Copy(cmdByte, 0, requestMsg, 4, LENGTH_CMD);
|
|
requestMsg[requestMsg.Length - 1] = BYTE_ETX;
|
|
requestMsg = AppendBcc(requestMsg);
|
|
|
|
return requestMsg;
|
|
}
|
|
|
|
public int SetTargetTemp(double temperature)
|
|
{
|
|
temperature = 0;
|
|
return E1.OK;
|
|
}
|
|
|
|
private byte[] CreateWriteRequestMsg(string cmd, string writeData)
|
|
{
|
|
byte[] requestMsg = new byte[LENGTH_WRITE_REQUEST_MSG];
|
|
byte[] cmdByte = Encoding.Default.GetBytes(cmd);
|
|
byte[] slaveAddress = GetSlaveAddress();
|
|
byte[] writeDataByte = Encoding.Default.GetBytes(writeData);
|
|
|
|
requestMsg[0] = BYTE_STX;
|
|
Array.Copy(slaveAddress, 0, requestMsg, 1, LENGTH_SLAVE_ADDRESS);
|
|
requestMsg[3] = BYTE_REQ_WRITE;
|
|
Array.Copy(cmdByte, 0, requestMsg, 4, LENGTH_CMD);
|
|
Array.Copy(writeDataByte, 0, requestMsg, 7, LENGTH_DATA);
|
|
requestMsg[requestMsg.Length - 1] = BYTE_ETX;
|
|
requestMsg = AppendBcc(requestMsg);
|
|
|
|
return requestMsg;
|
|
}
|
|
|
|
private byte[] GetSlaveAddress()
|
|
{
|
|
return Encoding.Default.GetBytes(
|
|
ConvertSlaveAddressIntToString(_Config.SlaveAddress));
|
|
}
|
|
|
|
private byte[] AppendBcc(byte[] msg)
|
|
{
|
|
if (!_Config.BccEnable)
|
|
{
|
|
return msg;
|
|
}
|
|
|
|
byte[] bccMsg = new byte[msg.Length + 1];
|
|
Array.Copy(msg, bccMsg, msg.Length);
|
|
bccMsg[bccMsg.Length - 1] = CalculateBcc(msg);
|
|
|
|
return bccMsg;
|
|
}
|
|
|
|
private static byte CalculateBcc(byte[] msg)
|
|
{
|
|
if (msg.Length == 0)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
if (msg.Length == 1)
|
|
{
|
|
return msg[0];
|
|
}
|
|
|
|
byte bcc = (byte)(msg[0] ^ msg[1]);
|
|
for (int i = 2; i < msg.Length; i++)
|
|
{
|
|
bcc ^= msg[i];
|
|
}
|
|
|
|
return bcc;
|
|
}
|
|
|
|
private ChillerErrorCode ValidateReadResponse(byte[] msg, string cmd)
|
|
{
|
|
return Validate(msg, cmd, enableCmdCheck: true);
|
|
}
|
|
|
|
private ChillerErrorCode ValidateWriteResponse(byte[] msg, string cmd)
|
|
{
|
|
return Validate(msg, cmd, enableCmdCheck: false);
|
|
}
|
|
|
|
private ChillerErrorCode Validate(byte[] msg, string cmd, bool enableCmdCheck)
|
|
{
|
|
if (msg.Length <= 0 ||
|
|
(enableCmdCheck && ExtractCmd(msg) != cmd))
|
|
{
|
|
return ChillerErrorCode.Unknown;
|
|
}
|
|
|
|
if (!ValidateBcc(msg))
|
|
{
|
|
return ChillerErrorCode.BccError;
|
|
}
|
|
|
|
if (ExtractAck(msg) == BYTE_ACK)
|
|
{
|
|
return ChillerErrorCode.NoError;
|
|
}
|
|
else
|
|
{
|
|
return ExtractErrorCode(msg);
|
|
}
|
|
}
|
|
|
|
private bool ValidateBcc(byte[] msg)
|
|
{
|
|
if (!_Config.BccEnable)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
byte[] subMsg = new byte[msg.Length - 1];
|
|
Array.Copy(msg, 0, subMsg, 0, subMsg.Length);
|
|
|
|
if (subMsg[0] != BYTE_STX || subMsg[subMsg.Length - 1] != BYTE_ETX)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return CalculateBcc(subMsg) == ExtractBcc(msg);
|
|
}
|
|
|
|
private byte ExtractAck(byte[] msg)
|
|
{
|
|
return msg[3];
|
|
}
|
|
|
|
private ChillerErrorCode ExtractErrorCode(byte[] msg)
|
|
{
|
|
return (ChillerErrorCode)(msg[4] - BYTE_0);
|
|
}
|
|
|
|
private string ExtractCmd(byte[] msg)
|
|
{
|
|
string strMsg = Encoding.Default.GetString(msg);
|
|
return strMsg.Substring(4, LENGTH_CMD);
|
|
}
|
|
|
|
private double ExtractDoubleData(byte[] msg)
|
|
{
|
|
string msgStr = Encoding.Default.GetString(msg);
|
|
string dataStr = msgStr.Substring(7, LENGTH_DATA);
|
|
|
|
return ConvertDataStringToDouble(dataStr);
|
|
}
|
|
|
|
private int ExtractIntData(byte[] msg)
|
|
{
|
|
string msgStr = Encoding.Default.GetString(msg);
|
|
string dataStr = msgStr.Substring(7, LENGTH_DATA);
|
|
|
|
return ConvertDataStringToInt(dataStr);
|
|
}
|
|
|
|
private byte ExtractBcc(byte[] msg)
|
|
{
|
|
return msg[msg.Length - 1];
|
|
}
|
|
|
|
private static int ConvertSlaveAddressStringToInt(string addressStr)
|
|
{
|
|
return Convert.ToInt32(addressStr);
|
|
}
|
|
|
|
private static string ConvertSlaveAddressIntToString(int address)
|
|
{
|
|
return string.Format("{0:00}", address);
|
|
}
|
|
|
|
private static double ConvertDataStringToDouble(string valueStr)
|
|
{
|
|
string intStr = valueStr.Substring(0, LENGTH_DATA_INTEGER);
|
|
string dfStr = valueStr.Substring(0 + LENGTH_DATA_INTEGER, LENGTH_DATA_DECIMAL_FRACTION);
|
|
|
|
return Convert.ToDouble(intStr) + (Convert.ToDouble(dfStr) * 0.1);
|
|
}
|
|
|
|
private static string ConvertDataDoubleToString(double value)
|
|
{
|
|
return string.Format("{0:0000}", value * 10);
|
|
}
|
|
|
|
private static int ConvertDataStringToInt(string valueStr)
|
|
{
|
|
return Convert.ToInt32(valueStr);
|
|
}
|
|
|
|
private static string ConvertDataIntToString(int value)
|
|
{
|
|
return string.Format("{0:0000}", value);
|
|
}
|
|
|
|
public int UpdateMonitoringItems()
|
|
{
|
|
lock (LockThis)
|
|
{
|
|
try
|
|
{
|
|
if (bIsSimpleCommMode)
|
|
{
|
|
if (E1.OK == GetCurrentTemp(1, out double currentTemp))
|
|
{
|
|
MonitoringItems.CurrentTempCH1 = currentTemp;
|
|
}
|
|
|
|
if (IsSimul)
|
|
{
|
|
MonitoringItems.TargetTempCH1 = SimulTargetTemp;
|
|
}
|
|
else
|
|
{
|
|
if (E1.OK == GetTargetTemp(2, out double targetTemp))
|
|
{
|
|
MonitoringItems.TargetTempCH1 = targetTemp;
|
|
}
|
|
}
|
|
|
|
MonitoringItems.WarningRange = _Config.WarningRange;
|
|
MonitoringItems.ErrorRange = _Config.ErrorRange;
|
|
|
|
MonitoringItems.TempStatus = ValidateTemp(
|
|
MonitoringItems.TargetTempCH1,
|
|
MonitoringItems.CurrentTempCH1,
|
|
MonitoringItems.WarningRange,
|
|
MonitoringItems.ErrorRange);
|
|
|
|
return E1.OK;
|
|
}
|
|
else
|
|
{
|
|
if (IsSimul)
|
|
{
|
|
return E1.OK;
|
|
}
|
|
ReadRegister();
|
|
//MonitoringItems.TargetTemp = _Config.TargetTemp;
|
|
MonitoringItems.WarningRange = _Config.WarningRange;
|
|
MonitoringItems.ErrorRange = _Config.ErrorRange;
|
|
|
|
MonitoringItems.TempStatus = ValidateTemp(
|
|
MonitoringItems.TargetTempCH1,
|
|
MonitoringItems.CurrentTempCH1,
|
|
MonitoringItems.WarningRange,
|
|
MonitoringItems.ErrorRange);
|
|
return E1.OK;
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
return 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
private static eTemperatureStatus ValidateTemp(double targetTemp, double currentTemp, double warningRange, double errorRange)
|
|
{
|
|
var tempDiff = currentTemp - targetTemp;
|
|
|
|
if (Math.Abs(tempDiff) <= warningRange)
|
|
{
|
|
return eTemperatureStatus.OK;
|
|
}
|
|
else if (Math.Abs(tempDiff) <= errorRange)
|
|
{
|
|
return (tempDiff < 0) ? eTemperatureStatus.WARNING_LOW : eTemperatureStatus.WARNING_HIGH;
|
|
}
|
|
else
|
|
{
|
|
return (tempDiff < 0) ? eTemperatureStatus.ERROR_LOW : eTemperatureStatus.ERROR_HIGH;
|
|
}
|
|
}
|
|
|
|
public IMonitoringItems GetMonitoringItems()
|
|
{
|
|
lock (LockThis)
|
|
{
|
|
return MonitoringItems.Clone() as IMonitoringItems;
|
|
}
|
|
}
|
|
private byte[] CreateReadRegisterMsg()
|
|
{
|
|
string strReadMessage = ConvertSlaveAddressIntToString(_Config.SlaveAddress) + "04" + "0030" + "0014";
|
|
byte[] requestMsg = new byte[LENGTH_READ_REGISTER_MSG];
|
|
|
|
byte[] bccMessage = HexStringToByteArray(strReadMessage);
|
|
byte[] byteReadMessage = Encoding.Default.GetBytes(strReadMessage);
|
|
byte[] bcc = CalculateLRC(bccMessage);
|
|
|
|
requestMsg[0] = (byte)(':');
|
|
|
|
Array.Copy(byteReadMessage, 0, requestMsg, 1, byteReadMessage.Length);
|
|
|
|
Array.Copy(bcc, 0, requestMsg, 13, bcc.Length);
|
|
|
|
requestMsg[15] = BYTE_CR;
|
|
requestMsg[16] = BYTE_LF;
|
|
string check = Encoding.Default.GetString(requestMsg);
|
|
|
|
return requestMsg;
|
|
}
|
|
private byte[] CreateReadRegisterMsg2()
|
|
{
|
|
byte[] requestMsg = new byte[LENGTH_READ_REGISTER_MSG];
|
|
byte[] slaveAddress = GetSlaveAddress();
|
|
|
|
requestMsg[0] = (byte)(':');
|
|
Array.Copy(slaveAddress, 0, requestMsg, 1, LENGTH_SLAVE_ADDRESS);
|
|
requestMsg[3] = (byte)('0');
|
|
requestMsg[4] = (byte)('3');
|
|
|
|
requestMsg[5] = (byte)('0');
|
|
requestMsg[6] = (byte)('0');
|
|
requestMsg[7] = (byte)('0');
|
|
requestMsg[8] = (byte)('B');
|
|
|
|
requestMsg[9] = (byte)('0');
|
|
requestMsg[10] = (byte)('0');
|
|
requestMsg[11] = (byte)('0');
|
|
requestMsg[12] = (byte)('0');
|
|
|
|
requestMsg[13] = (byte)('F');
|
|
requestMsg[14] = (byte)('1');
|
|
|
|
requestMsg[15] = BYTE_CR;
|
|
requestMsg[16] = BYTE_LF;
|
|
return requestMsg;
|
|
}
|
|
|
|
private bool ParsingRegisterData(byte[] responseMsg)
|
|
{
|
|
//if (responseMsg[3] != (byte)'0' || responseMsg[4] != (byte)'3')
|
|
// return false;
|
|
//if (responseMsg[5] != (byte)'0' || responseMsg[6] != (byte)'E')
|
|
// return false;
|
|
if (responseMsg[3] != (byte)'0' || responseMsg[4] != (byte)'4')
|
|
return false;
|
|
if (responseMsg[5] != (byte)'2' || responseMsg[6] != (byte)'8')
|
|
return false;
|
|
|
|
string msgStr = Encoding.Default.GetString(responseMsg);
|
|
//Information of 0030h (circulating fluid discharge temperature)
|
|
//responseMsg[7,8,9,10]
|
|
string dataStr = msgStr.Substring(7, 4);
|
|
int tempCH1 = Int32.Parse(dataStr, System.Globalization.NumberStyles.HexNumber);
|
|
MonitoringItems.CurrentTempCH1 = (double)tempCH1 / 10;
|
|
|
|
//Information of 0031h (circulating fluid discharge temperature ch2)
|
|
//responseMsg[11,12,13,14]
|
|
dataStr = msgStr.Substring(11, 4);
|
|
int tempCH2 = Int32.Parse(dataStr, System.Globalization.NumberStyles.HexNumber);
|
|
MonitoringItems.CurrentTempCH2 = (double)tempCH2 / 10;
|
|
//Information of 0032h (Electric resistivity / conductivity of the circulating fluid)
|
|
//responseMsg[15,16,17,18]
|
|
dataStr = msgStr.Substring(15, 4);
|
|
int Electric = Int32.Parse(dataStr, System.Globalization.NumberStyles.HexNumber);
|
|
|
|
//Information of 0033h (Electric resistivity / conductivity of the circulating fluid ch2)
|
|
//responseMsg[19,20,21,22]
|
|
dataStr = msgStr.Substring(19, 4);
|
|
//int pressure = Int32.Parse(dataStr, System.Globalization.NumberStyles.HexNumber);
|
|
|
|
//Information of 0034h (circulating fluid discharge pressure)
|
|
//responseMsg[23,24,25,26]
|
|
dataStr = msgStr.Substring(23, 4);
|
|
int pressure = Int32.Parse(dataStr, System.Globalization.NumberStyles.HexNumber);
|
|
|
|
//Information of 0035h (circulating fluid discharge pressure ch2)
|
|
//responseMsg[27,28,29,30]
|
|
dataStr = msgStr.Substring(27, 4);
|
|
|
|
//Information of 0036h (circulating fluid flow rate)
|
|
//responseMsg[30,31,32]
|
|
dataStr = msgStr.Substring(31, 4);
|
|
|
|
//Information of 0037h (circulating fluid flow rate ch2)
|
|
//responseMsg[30,31,32]
|
|
dataStr = msgStr.Substring(35, 4);
|
|
|
|
//Information of 0038h (Data1)
|
|
//responseMsg[30,31,32]
|
|
dataStr = msgStr.Substring(39, 4);
|
|
|
|
//Information of 0039h (Data2)
|
|
//responseMsg[30,31,32]
|
|
dataStr = msgStr.Substring(43, 4);
|
|
|
|
//Information of 003Ah (Data3)
|
|
//responseMsg[30,31,32]
|
|
dataStr = msgStr.Substring(47, 4);
|
|
|
|
//Information of 003Bh (Data4)
|
|
//responseMsg[30,31,32]
|
|
dataStr = msgStr.Substring(51, 4);
|
|
|
|
//Information of 003Ch (status flag )
|
|
//responseMsg[30,31,32]
|
|
dataStr = msgStr.Substring(55, 4);
|
|
int Status = Int32.Parse(dataStr, System.Globalization.NumberStyles.HexNumber);
|
|
MonitoringItems.Status = Status;
|
|
|
|
//Information of 003Dh (Alarm flag 1)
|
|
//responseMsg[30,31,32]
|
|
dataStr = msgStr.Substring(59, 4);
|
|
int Alarm1 = Int32.Parse(dataStr, System.Globalization.NumberStyles.HexNumber);
|
|
MonitoringItems.AlamSt1 = Alarm1;
|
|
|
|
//Information of 003Eh (Alarm flag 2)
|
|
//responseMsg[30,31,32]
|
|
dataStr = msgStr.Substring(63, 4);
|
|
int Alarm2 = Int32.Parse(dataStr, System.Globalization.NumberStyles.HexNumber);
|
|
MonitoringItems.AlamSt2 = Alarm2;
|
|
|
|
//Information of 003Fh (Alarm flag 3)
|
|
//responseMsg[30,31,32]
|
|
dataStr = msgStr.Substring(67, 4);
|
|
int Alarm3 = Int32.Parse(dataStr, System.Globalization.NumberStyles.HexNumber);
|
|
MonitoringItems.AlamSt3 = Alarm3;
|
|
|
|
dataStr = msgStr.Substring(71, 4);
|
|
int TargetTemp = Int32.Parse(dataStr, System.Globalization.NumberStyles.HexNumber);
|
|
MonitoringItems.TargetTempCH1 = (double)TargetTemp / 10;
|
|
|
|
dataStr = msgStr.Substring(75, 4);
|
|
int TargetTemp2 = Int32.Parse(dataStr, System.Globalization.NumberStyles.HexNumber);
|
|
MonitoringItems.TargetTempCH2 = (double)TargetTemp2 / 10;
|
|
|
|
|
|
dataStr = msgStr.Substring(79, 4);
|
|
//int Alarm3 = Int32.Parse(dataStr, System.Globalization.NumberStyles.HexNumber);
|
|
//MonitoringItems.AlamSt3 = Alarm3;
|
|
|
|
dataStr = msgStr.Substring(83, 4);
|
|
//int Alarm3 = Int32.Parse(dataStr, System.Globalization.NumberStyles.HexNumber);
|
|
//MonitoringItems.AlamSt3 = Alarm3;
|
|
return true;
|
|
}
|
|
|
|
private bool ParsingAllRequestData(byte[] responseMsg, int RequestAddress)
|
|
{
|
|
if (responseMsg[3] != (byte)'0' || responseMsg[4] != (byte)'4')
|
|
return false;
|
|
|
|
string msgStr = Encoding.Default.GetString(responseMsg);
|
|
string dataStr = msgStr.Substring(7, 4);
|
|
|
|
int RequestAddressData = Int32.Parse(dataStr, System.Globalization.NumberStyles.HexNumber);
|
|
bool bResult = ((RequestAddressData & (1 << RequestAddress)) > 0) ? true : false;
|
|
|
|
return bResult;
|
|
}
|
|
|
|
private int ReadRegister()
|
|
{
|
|
if (!IsPortOpen())
|
|
{
|
|
Log.E("Serial Port is NOT Open!");
|
|
return E1.NETWORK;
|
|
}
|
|
|
|
lock (LockThis)
|
|
{
|
|
byte[] requestMsg = CreateReadRegisterMsg();
|
|
if (Write(requestMsg))
|
|
{
|
|
byte[] responseMsg = new byte[LENGTH_RESPONSE_REGISTER_MSG];
|
|
if (ReadMessage(responseMsg, (byte)(':'), BYTE_LF) <= 0)
|
|
{
|
|
Log.E("ReadError");
|
|
return E1.GENERIC;
|
|
}
|
|
if (ParsingRegisterData(responseMsg) == false)
|
|
return E1.GENERIC;
|
|
}
|
|
}
|
|
|
|
return E1.OK;
|
|
}
|
|
private bool ReadAllRequestStatus(string strReg, string strValue, string strFunc)
|
|
{
|
|
if (!IsPortOpen())
|
|
{
|
|
Log.E("Serial Port is NOT Open!");
|
|
return false;
|
|
}
|
|
|
|
lock (LockThis)
|
|
{
|
|
byte[] requestMsg = CreateWriteRegisterMsg(strReg, strValue, strFunc);
|
|
if (Write(requestMsg))
|
|
{
|
|
byte[] responseMsg = new byte[LENGTH_RESPONSE_REGISTER_MSG];
|
|
if (ReadMessage(responseMsg, (byte)(':'), BYTE_LF) <= 0)
|
|
{
|
|
Log.E("ReadError");
|
|
return false;
|
|
}
|
|
return ParsingAllRequestData(responseMsg, Convert.ToInt32(strValue));
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
public int SetControlMode(int iValue)
|
|
{
|
|
if (IsSimul)
|
|
{
|
|
return E1.OK;
|
|
}
|
|
string strReg = "06";
|
|
string strValue = iValue.ToString("X4");
|
|
string strFunc = "0042";
|
|
|
|
return WriteRegister(strReg, strValue, strFunc);
|
|
}
|
|
public bool GetAllRequestStatus(string bitValue)
|
|
{
|
|
if (IsSimul)
|
|
{
|
|
return false;
|
|
}
|
|
string strReg = "04";
|
|
string strValue = bitValue;
|
|
string strFunc = "0042";
|
|
|
|
return ReadAllRequestStatus(strReg, strValue, strFunc);
|
|
}
|
|
public int SetRunStart(bool bStart)
|
|
{
|
|
if (IsSimul)
|
|
{
|
|
return E1.OK;
|
|
}
|
|
if (bStart)
|
|
{
|
|
SetControlMode(1);
|
|
return E1.OK;
|
|
|
|
}
|
|
else
|
|
{
|
|
SetControlMode(0);
|
|
return E1.OK;
|
|
}
|
|
|
|
}
|
|
public int RequestTest()
|
|
{
|
|
if (IsSimul)
|
|
{
|
|
return E1.OK;
|
|
}
|
|
|
|
SetControlMode(2);
|
|
return E1.OK;
|
|
|
|
}
|
|
|
|
public int SetTargetTemp(string Channel, double value)
|
|
{
|
|
if (IsSimul)
|
|
{
|
|
SimulTargetTemp = value;
|
|
return E1.OK;
|
|
}
|
|
string strFunc = "";
|
|
int iValue = (int)(value * 10);
|
|
string strReg = "06";
|
|
string strValue = iValue.ToString("X4");//ConvertDataDoubleToString(value);
|
|
if(Channel == "CH1")
|
|
{
|
|
strFunc = "0040"; //64.ToString("X4");//ConvertDataIntToString(40);
|
|
}
|
|
if (Channel == "CH2")
|
|
{
|
|
strFunc = "0041"; //64.ToString("X4");//ConvertDataIntToString(40);
|
|
}
|
|
|
|
return WriteRegister(strReg, strValue, strFunc);
|
|
}
|
|
|
|
|
|
private int WriteRegister(string Reg, string value, string Func)
|
|
{
|
|
if (!IsPortOpen())
|
|
{
|
|
Log.E("Serial Port is NOT Open!");
|
|
return E1.NETWORK;
|
|
}
|
|
|
|
lock (LockThis)
|
|
{
|
|
byte[] requestMsg = CreateWriteRegisterMsg(Reg, value, Func);
|
|
if (Write(requestMsg))
|
|
{
|
|
byte[] responseMsg = new byte[LENGTH_RESPONSE_REGISTER_MSG];
|
|
if (ReadMessage(responseMsg, (byte)(':'), BYTE_LF) <= 0)
|
|
{
|
|
Log.E("ReadError");
|
|
return E1.GENERIC;
|
|
}
|
|
}
|
|
}
|
|
|
|
return E1.OK;
|
|
}
|
|
|
|
private int WriteRegisterTest(string Reg, string value, string Func)
|
|
{
|
|
if (!IsPortOpen())
|
|
{
|
|
Log.E("Serial Port is NOT Open!");
|
|
return E1.NETWORK;
|
|
}
|
|
|
|
lock (LockThis)
|
|
{
|
|
string requestMsg = CreateWriteRegisterMsgTest(Reg, value, Func);
|
|
if (Write(requestMsg))
|
|
{
|
|
byte[] responseMsg = new byte[LENGTH_RESPONSE_REGISTER_MSG];
|
|
if (ReadMessage(responseMsg, (byte)(':'), BYTE_LF) <= 0)
|
|
{
|
|
Log.E("ReadError");
|
|
return E1.GENERIC;
|
|
}
|
|
}
|
|
}
|
|
|
|
return E1.OK;
|
|
}
|
|
|
|
private int WriteRequestRegister(string Reg, string value, string Func)
|
|
{
|
|
if (!IsPortOpen())
|
|
{
|
|
Log.E("Serial Port is NOT Open!");
|
|
return E1.NETWORK;
|
|
}
|
|
|
|
lock (LockThis)
|
|
{
|
|
byte[] requestMsg = CreateWriteRegisterMsg(Reg, value, Func);
|
|
if (Write(requestMsg))
|
|
{
|
|
byte[] responseMsg = new byte[LENGTH_RESPONSE_REGISTER_MSG];
|
|
if (ReadMessage(responseMsg, (byte)(':'), BYTE_LF) <= 0)
|
|
{
|
|
Log.E("ReadError");
|
|
return E1.GENERIC;
|
|
}
|
|
if (ParsingRegisterData(responseMsg) == false)
|
|
return E1.GENERIC;
|
|
}
|
|
}
|
|
|
|
return E1.OK;
|
|
}
|
|
|
|
private byte[] CreateWriteRegisterMsg(string Reg, string value, string Func)
|
|
{
|
|
string strWriteMessage = ConvertSlaveAddressIntToString(_Config.SlaveAddress) + "06" + Func + value;
|
|
byte[] requestMsg = new byte[LENGTH_READ_REGISTER_MSG];
|
|
byte[] bccMessage = HexStringToByteArray(strWriteMessage);
|
|
byte[] byteWriteMessage = Encoding.Default.GetBytes(strWriteMessage);
|
|
byte[] bcc = CalculateLRC(bccMessage);
|
|
|
|
requestMsg[0] = (byte)(':');
|
|
|
|
Array.Copy(byteWriteMessage, 0, requestMsg, 1, byteWriteMessage.Length);
|
|
|
|
Array.Copy(bcc, 0, requestMsg, 13, bcc.Length);
|
|
|
|
requestMsg[15] = BYTE_CR;
|
|
requestMsg[16] = BYTE_LF;
|
|
string check = Encoding.Default.GetString(requestMsg);
|
|
|
|
return requestMsg;
|
|
}
|
|
private string CreateWriteRegisterMsgTest(string Reg, string value, string Func)
|
|
{
|
|
string strWriteMessage = ConvertSlaveAddressIntToString(_Config.SlaveAddress) + "06" + Func + value;
|
|
byte[] requestMsg = new byte[LENGTH_READ_REGISTER_MSG];
|
|
byte[] bccMessage = HexStringToByteArray(strWriteMessage);
|
|
byte[] byteWriteMessage = Encoding.Default.GetBytes(strWriteMessage);
|
|
byte[] bcc = CalculateLRC(bccMessage);
|
|
|
|
requestMsg[0] = (byte)(':');
|
|
|
|
Array.Copy(byteWriteMessage, 0, requestMsg, 1, byteWriteMessage.Length);
|
|
|
|
Array.Copy(bcc, 0, requestMsg, 13, bcc.Length);
|
|
|
|
requestMsg[15] = BYTE_CR;
|
|
requestMsg[16] = BYTE_LF;
|
|
string check = Encoding.Default.GetString(requestMsg);
|
|
return check;
|
|
}
|
|
private byte[] CalculateLRC(byte[] bytes)
|
|
{
|
|
int sum = 0;
|
|
foreach (byte b in bytes)
|
|
{
|
|
sum += b;
|
|
}
|
|
byte truncatedSum = (byte)(sum & 0xFF);
|
|
byte lrc = (byte)((~truncatedSum + 1) & 0xFF);
|
|
string lrcHex = lrc.ToString("X2");
|
|
|
|
return new byte[]
|
|
{
|
|
(byte)lrcHex[0], (byte)lrcHex[1]
|
|
};
|
|
|
|
}
|
|
private byte[] HexStringToByteArray(string hex)
|
|
{
|
|
int length = hex.Length;
|
|
byte[] bytes = new byte[length / 2];
|
|
for(int i=0; i< length; i += 2)
|
|
{
|
|
bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
|
|
}
|
|
return bytes;
|
|
}
|
|
|
|
|
|
}
|
|
}
|