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;
                }
            }
            catch (System.IO.IOException) {
                // socket error --> close connection
                running = false;
            }

            return(sendCount);
        }
示例#2
0
        private int SendIMessage(ASDU asdu)
        {
            BufferFrame frame = new BufferFrame(new byte[260], 6);             /* reserve space for ACPI */

            asdu.Encode(frame, parameters);

            byte[] buffer = frame.GetBuffer();

            int msgSize = frame.GetMsgSize();              /* ACPI + ASDU */

            buffer [0] = 0x68;

            /* set size field */
            buffer [1] = (byte)(msgSize - 2);

            buffer [2] = (byte)((sendSequenceNumber % 128) * 2);
            buffer [3] = (byte)(sendSequenceNumber / 128);

            buffer [4] = (byte)((receiveSequenceNumber % 128) * 2);
            buffer [5] = (byte)(receiveSequenceNumber / 128);

            if (running)
            {
                socket.NoDelay = true;
                socket.Send(buffer, msgSize, SocketFlags.None);
                sendSequenceNumber = (sendSequenceNumber + 1) % 32768;
                statistics.SentMsgCounter++;
                unconfirmedReceivedIMessages = 0;

                if (sentMessageHandler != null)
                {
                    sentMessageHandler(sentMessageHandlerParameter, buffer, msgSize);
                }

                return(sendSequenceNumber);
            }
            else
            {
                if (lastException != null)
                {
                    throw new ConnectionException(lastException.Message, lastException);
                }
                else
                {
                    throw new ConnectionException("not connected", new SocketException(10057));
                }
            }
        }
示例#3
0
        public byte[] AsByteArray()
        {
            int expectedSize = IEC60870_5_104_MAX_ASDU_LENGTH - spaceLeft;

            BufferFrame frame = new BufferFrame(new byte[expectedSize], 0);

            Encode(frame, parameters);

            if (frame.GetMsgSize() == expectedSize)
            {
                return(frame.GetBuffer());
            }
            else
            {
                return(null);
            }
        }