Exemplo n.º 1
0
        void SendCmd(WORD wDevID, BYTE wCmdOrAck, int nParam)
        {
            CMD_PKT pkt = new CMD_PKT();

            BYTE[] pktBuffer = new BYTE[12];

            pkt.Head1   = (BYTE)COMMAND_START_CODE1;
            pkt.Head2   = (BYTE)COMMAND_START_CODE2;
            pkt.wDevId0 = (byte)(wDevID & 0xFF);
            pkt.wDevId1 = (byte)(wDevID >> 8);
            pkt.nParam0 = (byte)(nParam & 0xFF);
            pkt.nParam1 = (byte)((nParam & 0xFF00) >> 8);
            pkt.nParam2 = (byte)((nParam & 0xFF0000) >> 16);
            pkt.nParam3 = (byte)((nParam & 0xFF000000) >> 24);
            pkt.wCmd0   = wCmdOrAck;
            pkt.wCmd1   = 0x00;
            WORD chksum = CalcChkSumOfCmd(ref pkt);

            pkt.wChkSum0 = (byte)(chksum % 256);
            pkt.wChkSum1 = (byte)(chksum >> 8);

            pktBuffer = PktToByteArray(ref pkt);

            serial.Write(pktBuffer, 0, PKT_SIZE);
        }
Exemplo n.º 2
0
        WORD CalcChkSumOfCmd(ref CMD_PKT pPkt)
        {
            WORD wChkSum = 0;

            BYTE[] Buf = PktToByteArray(ref pPkt);
            int    i;

            for (i = 0; i < (PKT_SIZE - CHK_SUM_SIZE); i++)
            {
                wChkSum += Buf[i];
            }
            return(wChkSum);
        }
Exemplo n.º 3
0
        CMD_PKT ByteArrayToPkt(ref BYTE[] arr)
        {
            CMD_PKT package = new CMD_PKT();

            package.Head1    = arr[0];
            package.Head2    = arr[1];
            package.wDevId0  = arr[2];
            package.wDevId1  = arr[3];
            package.nParam0  = arr[4];
            package.nParam1  = arr[5];
            package.nParam2  = arr[6];
            package.nParam3  = arr[7];
            package.wCmd0    = arr[8];
            package.wCmd1    = arr[9];
            package.wChkSum0 = arr[10];
            package.wChkSum1 = arr[11];

            return(package);
        }
Exemplo n.º 4
0
        BYTE[] PktToByteArray(ref CMD_PKT package)
        {
            byte[] arr = new byte[12]; // 12 = sizeof(CMD_PKT)

            arr[0]  = package.Head1;
            arr[1]  = package.Head2;
            arr[2]  = package.wDevId0;
            arr[3]  = package.wDevId1;
            arr[4]  = package.nParam0;
            arr[5]  = package.nParam1;
            arr[6]  = package.nParam2;
            arr[7]  = package.nParam3;
            arr[8]  = package.wCmd0;
            arr[9]  = package.wCmd1;
            arr[10] = package.wChkSum0;
            arr[11] = package.wChkSum1;

            return(arr);
        }