public override byte[] PackRequest() { byte[] message = new byte[15]; DNP3ConfirmCommandParamters commandParam = (DNP3ConfirmCommandParamters)CommandParameters; CommandParameters.Length = 0x08; Buffer.BlockCopy(headerBuilder.Build(CommandParameters), 0, message, 0, 10); message[10] = commandParam.TransportControl; message[11] = commandParam.AplicationControl; message[12] = commandParam.FunctionCode; ushort crc = 0; for (int i = 10; i < 13; i++) { CrcCalculator.computeCRC(message[i], ref crc); } crc = (ushort)(~crc); Buffer.BlockCopy(BitConverter.GetBytes(crc), 0, message, 13, 2); return(message); }
private void ConnectionProcessorThread() { while (threadCancellationSignal) { try { if (connectionState == ConnectionState.DISCONNECTED) { numberOfConnectionRetries = 0; connection.Connect(); while (numberOfConnectionRetries < 10) { if (connection.CheckState()) { this.connectionState = ConnectionState.CONNECTED; numberOfConnectionRetries = 0; break; } else { numberOfConnectionRetries++; if (numberOfConnectionRetries == 10) { connection.Disconect(); connectionState = ConnectionState.DISCONNECTED; } } } } else { processConnection.WaitOne(); while (commandQueue.TryDequeue(out currentCommand)) { lock (lockObj) { connection.Send(currentCommand.PackRequest()); byte[] message; byte[] header = connection.Recv(10); int recvLen = CalculateRecvLength(header[2]); byte[] dataChunks = connection.Recv(recvLen); message = new byte[header.Length + recvLen]; Buffer.BlockCopy(header, 0, message, 0, 10); Buffer.BlockCopy(dataChunks, 0, message, 10, recvLen); bool unsolicited = CheckIfUnsolicited(message[11]); if (unsolicited) { HandleReceivedBytes(message, unsolicited); DNP3ConfirmCommandParamters dnp3Param = new DNP3ConfirmCommandParamters(0xc0, (byte)DNP3FunctionCode.CONFIRM, 0xc0); //podesiti parametre IDNP3Function function = DNP3FunctionFactory.CreateConfirmFunction(dnp3Param); connection.Send(function.PackRequest()); } else { HandleReceivedBytes(message, unsolicited); } currentCommand = null; } } } } catch (SocketException se) { currentCommand = null; connectionState = ConnectionState.DISCONNECTED; Log().Log(new LogEventModel() { EventType = LogEventType.ERROR, Message = $"{se.Message}" }).GetAwaiter().GetResult(); } catch (Exception ex) { currentCommand = null; Log().Log(new LogEventModel() { EventType = LogEventType.WARN, Message = $"{ex.Message}" }).GetAwaiter().GetResult(); } } }
public static IDNP3Function CreateConfirmFunction(DNP3ConfirmCommandParamters parameters) { return(new Confirm(parameters)); }