示例#1
0
    byte[] StructureToByteArray_data_resp(data_resp obj)
    {
        int len = Marshal.SizeOf(obj);

        byte[] arr = new byte[len];

        IntPtr ptr = Marshal.AllocHGlobal(len);

        Marshal.StructureToPtr(obj, ptr, true);

        Marshal.Copy(ptr, arr, 0, len);

        Marshal.FreeHGlobal(ptr);

        return(arr);
    }
示例#2
0
    byte[] CRC_data_resp_msg(data_resp msg, bool bad)
    {
        byte data_s;

        data_s = 8;
        int    len;
        ushort crc;

        ////////////////////////////////////
        //CRC
        len        = 3 + data_s;
        crc        = 0xFFFF;
        buffer_out = StructureToByteArray_data_resp(msg);

        for (int pos = 0; pos < len; pos++)
        {
            crc ^= buffer_out[pos];    // XOR byte into least sig. byte of crc

            for (int i = 8; i != 0; i--)
            {                  // Loop over each bit
                if ((crc & 0x0001) != 0)
                {              // If the LSB is set
                    crc >>= 1; // Shift right and XOR 0xA001
                    crc  ^= 0xA001;
                }
                else                            // Else LSB is not set  checkBoxBadCRC1
                {
                    crc >>= 1;                  // Just shift right
                }
            }
        }

        buffer_out[len + 0] = (byte)(crc & 0xFF);
        buffer_out[len + 1] = (byte)((crc >> 8) & 0xFF);

        if (bad)
        {
            buffer_out[len + 0] = 0;
            buffer_out[len + 1] = 0;
        }
        len = len + 2;

        ///////////   end of CRC  ///////////////
        return(buffer_out);
    }