internal static bool[] ExtractValues(ReadMemoryAreaBitRequest request, FINSResponse response) { if (response.Data.Length < request.Length) { throw new FINSException("The Response Data Length of '" + response.Data.Length.ToString() + "' was too short - Expecting a Length of '" + request.Length.ToString() + "'"); } return(response.Data.Select <byte, bool>(value => value == 0 ? false : true).ToArray()); }
internal static ClockResult ExtractClock(ReadClockRequest request, FINSResponse response) { if (response.Data.Length < DATE_LENGTH + DAY_OF_WEEK_LENGTH) { throw new FINSException("The Response Data Length of '" + response.Data.Length.ToString() + "' was too short - Expecting a Length of '" + (DATE_LENGTH + DAY_OF_WEEK_LENGTH).ToString() + "'"); } ReadOnlyMemory <byte> data = response.Data; return(new ClockResult { ClockDateTime = getClockDateTime(data.Slice(0, DATE_LENGTH).ToArray()), DayOfWeek = BCDConverter.ToByte(data.ToArray()[DATE_LENGTH]), }); }
internal static short[] ExtractValues(ReadMemoryAreaWordRequest request, FINSResponse response) { if (response.Data.Length < request.Length * 2) { throw new FINSException("The Response Data Length of '" + response.Data.Length.ToString() + "' was too short - Expecting a Length of '" + (request.Length * 2).ToString() + "'"); } List <short> values = new List <short>(); for (int i = 0; i < request.Length * 2; i += 2) { values.Add(BitConverter.ToInt16(new byte[] { response.Data[i + 1], response.Data[i] })); } return(values.ToArray()); }
internal static CPUUnitDataResult ExtractData(FINSResponse response) { int expectedLength = CONTROLLER_MODEL_LENGTH + CONTROLLER_VERSION_LENGTH + SYSTEM_RESERVED_LENGTH + AREA_DATA_LENGTH; if (response.Data.Length < expectedLength) { throw new FINSException("The Response Data Length of '" + response.Data.Length.ToString() + "' was too short - Expecting a Length of '" + expectedLength.ToString() + "'"); } ReadOnlyMemory <byte> data = response.Data; CPUUnitDataResult result = new CPUUnitDataResult(); result.ControllerModel = extractStringValue(data.Slice(0, CONTROLLER_MODEL_LENGTH).ToArray()); result.ControllerVersion = extractStringValue(data.Slice(CONTROLLER_MODEL_LENGTH, CONTROLLER_VERSION_LENGTH).ToArray()); return(result); }
internal static CycleTimeResult ExtractCycleTime(ReadCycleTimeRequest request, FINSResponse response) { if (response.Data.Length < CYCLE_TIME_ITEM_LENGTH * 3) { throw new FINSException("The Response Data Length of '" + response.Data.Length.ToString() + "' was too short - Expecting a Length of '" + (CYCLE_TIME_ITEM_LENGTH * 3).ToString() + "'"); } ReadOnlyMemory <byte> data = response.Data; return(new CycleTimeResult { AverageCycleTime = getCycleTime(data.Slice(0, CYCLE_TIME_ITEM_LENGTH).ToArray()), MaximumCycleTime = getCycleTime(data.Slice(CYCLE_TIME_ITEM_LENGTH, CYCLE_TIME_ITEM_LENGTH).ToArray()), MinimumCycleTime = getCycleTime(data.Slice(CYCLE_TIME_ITEM_LENGTH * 2, CYCLE_TIME_ITEM_LENGTH).ToArray()), }); }
internal static void Validate(WriteMemoryAreaWordRequest request, FINSResponse response) { // TODO: Consider if any Checks can be made on the FINS Response }
internal static FINSResponse CreateNew(Memory <byte> message, FINSRequest request) { if (message.Length < HEADER_LENGTH + COMMAND_LENGTH + RESPONSE_CODE_LENGTH) { throw new FINSException("The FINS Response Message Length was too short"); } FINSResponse response = new FINSResponse(); byte[] header = message.Slice(0, HEADER_LENGTH).ToArray(); response.ServiceID = header[9]; byte[] command = message.Slice(HEADER_LENGTH, COMMAND_LENGTH).ToArray(); if (ValidateFunctionCode(command[0]) == false) { throw new FINSException("Invalid Function Code '" + command[0].ToString() + "'"); } response.FunctionCode = command[0]; if (response.FunctionCode != request.FunctionCode) { throw new FINSException("Unexpected Function Code '" + Enum.GetName(typeof(enFunctionCode), response.FunctionCode) + "' - Expecting '" + Enum.GetName(typeof(enFunctionCode), request.FunctionCode) + "'"); } if (ValidateSubFunctionCode(command[0], command[1]) == false) { throw new FINSException("Invalid Sub Function Code '" + command[1].ToString() + "' for Function Code '" + command[0].ToString() + "'"); } response.SubFunctionCode = command[1]; if (response.SubFunctionCode != request.SubFunctionCode) { throw new FINSException("Unexpected Sub Function Code '" + getSubFunctionCodeName(response.FunctionCode, response.SubFunctionCode) + "' - Expecting '" + getSubFunctionCodeName(request.FunctionCode, request.SubFunctionCode) + "'"); } byte[] responseCode = message.Slice(HEADER_LENGTH + COMMAND_LENGTH, RESPONSE_CODE_LENGTH).ToArray(); if (hasNetworkRelayError(responseCode[0])) { throw new FINSException("A Network Relay Error has occurred"); } response.MainResponseCode = getMainResponseCode(responseCode[0]); response.SubResponseCode = getSubResponseCode(responseCode[1]); throwIfResponseError(response.MainResponseCode, response.SubResponseCode); if (request.ServiceID != response.ServiceID) { throw new FINSException("The Service ID for the FINS Request '" + request.ServiceID + "' did not match the FINS Response '" + response.ServiceID + "'"); } response.Data = message.Length > HEADER_LENGTH + COMMAND_LENGTH + RESPONSE_CODE_LENGTH ? message.Slice(HEADER_LENGTH + COMMAND_LENGTH + RESPONSE_CODE_LENGTH, message.Length - (HEADER_LENGTH + COMMAND_LENGTH + RESPONSE_CODE_LENGTH)).ToArray() : new byte[0]; return(response); }