コード例 #1
0
 public DataLinkFrame(DataLinkHeader dataLinkHeader)
 {
     DataLynkHeader = dataLinkHeader ?? throw new ArgumentNullException();
 }
コード例 #2
0
        public byte[] PackDown(byte[] data, bool direction, bool primary, DataLinkFunctionCodes functionCode)
        {
            DataLinkHeader dataLinkHeader = new DataLinkHeader();

            dataLinkHeader.Start[0] = 0x05;
            dataLinkHeader.Start[1] = 0x64;

            dataLinkHeader.Length = BitConverter.GetBytes(5 + data.Count())[0];

            dataLinkHeader.Control[7] = direction;  // DIR (Master/Outstation)
            dataLinkHeader.Control[6] = primary;    // PRM (Initiated/Completed)

            dataLinkHeader.Control[5] = false;      // FCB
            dataLinkHeader.Control[4] = false;      // FCV

            BitArray bitArray = new BitArray(new Byte[] { (byte)functionCode });

            dataLinkHeader.Control[3] = bitArray[3];    // Function code
            dataLinkHeader.Control[2] = bitArray[2];
            dataLinkHeader.Control[1] = bitArray[1];
            dataLinkHeader.Control[0] = bitArray[0];

            dataLinkHeader.Destination[0] = 1;
            dataLinkHeader.Destination[1] = 1;

            dataLinkHeader.Source[0] = 2;
            dataLinkHeader.Source[1] = 2;

            byte[] header    = dataLinkHeader.ToBytes();
            byte[] tempFrame = new byte[frameMaxSize]; // HARD CODE!!!!!!!!!!!!!!!!

            header.CopyTo(tempFrame, 0);

            byte[] crc = CallCalclulateCRC(header);

            tempFrame[8] = crc[0];   // Header CRC
            tempFrame[9] = crc[1];

            int dataCount  = data.Count();
            int dataIndex  = 0;
            int frameIndex = 10;

            while (dataCount > 0)
            {
                byte[] temp = null;

                if (dataCount < 16)
                {
                    temp = new byte[dataCount];
                    data.CopyTo(temp, dataIndex);
                    temp.CopyTo(tempFrame, frameIndex);

                    byte[] crc1 = CallCalclulateCRC(temp);

                    frameIndex += dataCount;
                    tempFrame[frameIndex++] = crc1[0];
                    tempFrame[frameIndex++] = crc1[1];

                    break;
                }

                temp = new byte[16];

                data.CopyTo(temp, dataIndex);
                temp.CopyTo(tempFrame, frameIndex);

                byte[] crc2 = CallCalclulateCRC(temp);

                frameIndex += 16;
                tempFrame[frameIndex++] = crc2[0];
                tempFrame[frameIndex++] = crc2[1];

                dataIndex += 16;
                dataCount -= 16;
            }

            byte[] frame = new byte[frameIndex];

            for (int i = 0; i < frameIndex; i++)
            {
                frame[i] = tempFrame[i];
            }

            return(frame);
        }