// ------------------------------------------------------------------------ // Write data and and wait for response private byte[] WriteSyncData(byte[] write_data, ushort id) { if (tcpSynCl == null) { log.AddRow("Connection Lost"); } else if (tcpSynCl.Connected) { try { tcpSynCl.Send(write_data, 0, write_data.Length, SocketFlags.None); int result = tcpSynCl.Receive(tcpSynClBuffer, 0, tcpSynClBuffer.Length, SocketFlags.None); byte unit = tcpSynClBuffer[6]; byte function = tcpSynClBuffer[7]; byte[] data; if (result == 0) { CallException(id, unit, write_data[7], excExceptionConnectionLost); } // ------------------------------------------------------------ // Response data is slave exception if (function > excExceptionOffset) { function -= excExceptionOffset; CallException(id, unit, function, tcpSynClBuffer[8]); return(null); } // ------------------------------------------------------------ // Write response data else if ((function >= fctWriteSingleCoil) && (function != fctReadWriteMultipleRegister)) { data = new byte[2]; Array.Copy(tcpSynClBuffer, 10, data, 0, 2); } // ------------------------------------------------------------ // Read response data else { data = new byte[tcpSynClBuffer[8]]; Array.Copy(tcpSynClBuffer, 9, data, 0, tcpSynClBuffer[8]); } return(data); } catch (SystemException) { CallException(id, write_data[6], write_data[7], excExceptionConnectionLost); } } else // { CallException(id, write_data[6], write_data[7], excExceptionConnectionLost); } return(null); }