private int SendIMessage(BufferFrame asdu) { byte[] buffer = asdu.GetBuffer(); int msgSize = asdu.GetMsgSize(); /* ASDU size + ACPI size */ buffer [0] = 0x68; /* set size field */ buffer [1] = (byte)(msgSize - 2); buffer [2] = (byte)((sendCount % 128) * 2); buffer [3] = (byte)(sendCount / 128); buffer [4] = (byte)((receiveCount % 128) * 2); buffer [5] = (byte)(receiveCount / 128); try { lock (socketStream) { socketStream.Write(buffer, 0, msgSize); DebugLog("SEND I (size = " + msgSize + ") : " + BitConverter.ToString(buffer, 0, msgSize)); sendCount = (sendCount + 1) % 32768; unconfirmedReceivedIMessages = 0; timeoutT2Triggered = false; } } catch (System.IO.IOException) { // socket error --> close connection running = false; } return(sendCount); }
public byte[] AsByteArray() { int expectedSize = parameters.MaxAsduLength - spaceLeft; BufferFrame frame = new BufferFrame(new byte[expectedSize], 0); Encode(frame, parameters); if (frame.GetMsgSize() == expectedSize) { return(frame.GetBuffer()); } else { return(null); } }
internal void SendVariableLengthFrameSecondary(FunctionCodeSecondary fc, int address, bool acd, bool dfc, BufferFrame frame) { buffer [0] = 0x68; /* START */ buffer [3] = 0x68; /* START */ byte c = (byte)((int)fc & 0x1f); if (linkLayerMode == LinkLayerMode.BALANCED) { if (dir) { c += 0x80; } } if (acd) { c += 0x20; } if (dfc) { c += 0x10; } buffer [4] = c; int bufPos = 5; if (linkLayerParameters.AddressLength > 0) { buffer [bufPos++] = (byte)(address % 0x100); if (linkLayerParameters.AddressLength > 1) { buffer [bufPos++] = (byte)((address / 0x100) % 0x100); } } byte[] userData = frame.GetBuffer(); int userDataLength = frame.GetMsgSize(); int l = 1 + linkLayerParameters.AddressLength + userDataLength; if (l > 255) { return; } buffer [1] = (byte)l; buffer [2] = (byte)l; for (int i = 0; i < userDataLength; i++) { buffer [bufPos++] = userData[i]; } byte checksum = 0; for (int i = 4; i < bufPos; i++) { checksum += buffer[i]; } buffer [bufPos++] = checksum; buffer [bufPos++] = 0x16; /* END */ if (sentRawMessageHandler != null) { sentRawMessageHandler(sentRawMessageHandlerParameter, buffer, bufPos); } transceiver.SendMessage(buffer, bufPos); }
public void SendVariableLengthFramePrimary(FunctionCodePrimary fc, int address, bool fcb, bool fcv, BufferFrame frame) { buffer [0] = 0x68; /* START */ buffer [3] = 0x68; /* START */ byte c = (byte)fc; if (dir) { c += 0x80; } c += 0x40; // PRM = 1; if (fcv) { c += 0x10; } if (fcb) { c += 0x20; } buffer [4] = c; int bufPos = 5; if (linkLayerParameters.AddressLength > 0) { buffer [bufPos++] = (byte)(address % 0x100); if (linkLayerParameters.AddressLength > 1) { buffer [bufPos++] = (byte)((address / 0x100) % 0x100); } } byte[] userData = frame.GetBuffer(); int userDataLength = frame.GetMsgSize(); for (int i = 0; i < userDataLength; i++) { buffer [bufPos++] = userData[i]; } int l = 1 + linkLayerParameters.AddressLength + frame.GetMsgSize(); if (l > 255) { return; } buffer [1] = (byte)l; buffer [2] = (byte)l; byte checksum = 0; for (int i = 4; i < bufPos; i++) { checksum += buffer[i]; } buffer [bufPos++] = checksum; buffer [bufPos++] = 0x16; /* END */ if (sentRawMessageHandler != null) { sentRawMessageHandler(sentRawMessageHandlerParameter, buffer, bufPos); } transceiver.SendMessage(buffer, bufPos); }