⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 C51下面,用IO口模拟uart的源代码,里面有文档说明.
💻 C
📖 第 1 页 / 共 2 页
字号:

        return 0;

}

BYTE  ReadCard(BYTE BlockNo,BYTE KeyType,BYTE *CardKey,BYTE *CardData)
{
        BYTE i;

//--------------------------------------------------------------------
// Command: 0x60 0x0A 0x01 0x62 KEYTYPE BlockNo KEY0 … KEY5 BCC 0x03
// send 27 byte  13 * 2 + 1 = 27
//--------------------------------------------------------------------

        tmpData[0] = 0x60;
        tmpData[1] = 0x0A;
        tmpData[2] = 0x01;
        tmpData[3] = 0x62;
        tmpData[4] = KeyType;
        tmpData[5] = BlockNo;
        for(i = 0; i < 6; i++) tmpData[6 + i] = CardKey[i];
        tmpData[12] = XORCheck(tmpData,12);

        ChangeHexToDbb(tmpData,CommArray,13);

        for(i = 0; i < 27; i++)
        {
                SoftUART_PutByte(CommArray[i]);
        }

//--------------------------------------------------------------------
// Right: 0x60 0x11 DATA0 … DATA15 0x00 BCC 0x03
// recieve 41 byte  20 * 2 + 1 = 41
// Error: 0x60 0x01 ERRNO BCC 0x03
// recieve 9 byte  4 * 2 + 1 = 9
//--------------------------------------------------------------------

        for(i = 0; i < 41; i++)
        {
                CommArray[i] = SoftUART_GetByte();
                if(CommArray[i] == 0x03) break;
        }

        if(CommArray[0] != '6') return 1;
        if(i < 10) return 1;

        ChangeDbbToHex(&CommArray[4],tmpData,32);

        for(i = 0; i < 16; i++) CardData[i] = tmpData[i];

//--------------------------------------------------------------------

        return 0;

}

BYTE WriteCard(BYTE BlockNo,BYTE KeyType,BYTE *CardKey,BYTE *CardData)
{
        BYTE i;

//--------------------------------------------------------------------
// Command: 0x60 0x1A 0x01 0x63 KEYTYPE BlockNo KEY0 … KEY5 DATA0 … DATA15 BCC 0x03
// send 59 byte  29 * 2 + 1 = 59
//--------------------------------------------------------------------

        tmpData[0] = 0x60;
        tmpData[1] = 0x1A;
        tmpData[2] = 0x01;
        tmpData[3] = 0x63;
        tmpData[4] = KeyType;
        tmpData[5] = BlockNo;
        for(i = 0; i < 6; i++)  tmpData[6 + i] = CardKey[i];
        for(i = 0; i < 16; i++) tmpData[12 + i] = CardData[i];

        tmpData[28] = XORCheck(tmpData,28);

        ChangeHexToDbb(tmpData,CommArray,29);

        for(i = 0; i < 59; i++)
        {
                SoftUART_PutByte(CommArray[i]);
        }

//--------------------------------------------------------------------
// Right: 0x60 0x01 0x00 BCC 0x03
// recieve 9 byte  4 * 2 + 1 = 9
// Error: 0x60 0x01 ERRNO BCC 0x03
// recieve 9 byte  4 * 2 + 1 = 9
//--------------------------------------------------------------------

        for(i = 0; i < 9; i++)
        {
                CommArray[i] = SoftUART_GetByte();
                if(CommArray[i] == 0x03) break;
        }

        if(CommArray[0] != '6') return 1;

        ChangeDbbToHex(&CommArray[4],tmpData,2);

        return tmpData[0];

//--------------------------------------------------------------------

}

BYTE FormatPurse(BYTE BlockNo,BYTE KeyType,BYTE *CardKey,BYTE *Value)
{
        BYTE i;

//--------------------------------------------------------------------
// Command: 0x60 0x0E 0x01 0x64 KEYTYPE BlockNo KEY0 … KEY5 Value0 …Value3 BCC 0x03
// send 35 byte  17 * 2 + 1 = 35
//--------------------------------------------------------------------

        tmpData[0] = 0x60;
        tmpData[1] = 0x0E;
        tmpData[2] = 0x01;
        tmpData[3] = 0x64;
        tmpData[4] = KeyType;
        tmpData[5] = BlockNo;
        for(i = 0; i < 6; i++)  tmpData[6 + i] = CardKey[i];
        for(i = 0; i < 4; i++) tmpData[12 + i] = Value[i];

        tmpData[16] = XORCheck(tmpData,16);

        ChangeHexToDbb(tmpData,CommArray,17);

        for(i = 0; i < 35; i++)
        {
                SoftUART_PutByte(CommArray[i]);
        }

//--------------------------------------------------------------------
// Right: 0x60 0x01 0x00 BCC 0x03
// recieve 9 byte  4 * 2 + 1 = 9
// Error: 0x60 0x01 ERRNO BCC 0x03
// recieve 9 byte  4 * 2 + 1 = 9
//--------------------------------------------------------------------

        for(i = 0; i < 9; i++)
        {
                CommArray[i] = SoftUART_GetByte();
                if(CommArray[i] == 0x03) break;
        }

        if(CommArray[0] != '6') return 1;

        ChangeDbbToHex(&CommArray[4],tmpData,2);

        return tmpData[0];

//--------------------------------------------------------------------

}

BYTE Increase(BYTE BlockNo,BYTE KeyType,BYTE *CardKey,BYTE *Value)
{
        BYTE i;

//--------------------------------------------------------------------
// Command: 0x60 0x0E 0x01 0x65 KEYTYPE BlockNo KEY0 … KEY5 Value0 …Value3 BCC 0x03
// send 35 byte  17 * 2 + 1 = 35
//--------------------------------------------------------------------

        tmpData[0] = 0x60;
        tmpData[1] = 0x0E;
        tmpData[2] = 0x01;
        tmpData[3] = 0x65;
        tmpData[4] = KeyType;
        tmpData[5] = BlockNo;
        for(i = 0; i < 6; i++)  tmpData[6 + i] = CardKey[i];
        for(i = 0; i < 4; i++) tmpData[12 + i] = Value[i];

        tmpData[16] = XORCheck(tmpData,16);

        ChangeHexToDbb(tmpData,CommArray,17);

        for(i = 0; i < 35; i++)
        {
                SoftUART_PutByte(CommArray[i]);
        }

//--------------------------------------------------------------------
// Right: 0x60 0x01 0x00 BCC 0x03
// recieve 9 byte  4 * 2 + 1 = 9
// Error: 0x60 0x01 ERRNO BCC 0x03
// recieve 9 byte  4 * 2 + 1 = 9
//--------------------------------------------------------------------

        for(i = 0; i < 9; i++)
        {
                CommArray[i] = SoftUART_GetByte();
                if(CommArray[i] == 0x03) break;
        }

        if(CommArray[0] != '6') return 1;

        ChangeDbbToHex(&CommArray[4],tmpData,2);

        return tmpData[0];

//--------------------------------------------------------------------

}

BYTE Decrease(BYTE BlockNo,BYTE KeyType,BYTE *CardKey,BYTE *Value)
{
        BYTE i;

//--------------------------------------------------------------------
// Command: 0x60 0x0E 0x01 0x66 KEYTYPE BlockNo KEY0 … KEY5 Value0 …Value3 BCC 0x03
// send 35 byte  17 * 2 + 1 = 35
//--------------------------------------------------------------------

        tmpData[0] = 0x60;
        tmpData[1] = 0x0E;
        tmpData[2] = 0x01;
        tmpData[3] = 0x66;
        tmpData[4] = KeyType;
        tmpData[5] = BlockNo;
        for(i = 0; i < 6; i++)  tmpData[6 + i] = CardKey[i];
        for(i = 0; i < 4; i++) tmpData[12 + i] = Value[i];

        tmpData[16] = XORCheck(tmpData,16);

        ChangeHexToDbb(tmpData,CommArray,17);

        for(i = 0; i < 35; i++)
        {
                SoftUART_PutByte(CommArray[i]);
        }

//--------------------------------------------------------------------
// Right: 0x60 0x01 0x00 BCC 0x03
// recieve 9 byte  4 * 2 + 1 = 9
// Error: 0x60 0x01 ERRNO BCC 0x03
// recieve 9 byte  4 * 2 + 1 = 9
//--------------------------------------------------------------------

        for(i = 0; i < 9; i++)
        {
                CommArray[i] = SoftUART_GetByte();
                if(CommArray[i] == 0x03) break;
        }

        if(CommArray[0] != '6') return 1;

        ChangeDbbToHex(&CommArray[4],tmpData,2);

        return tmpData[0];

//--------------------------------------------------------------------

}

BYTE ReadPurse(BYTE BlockNo,BYTE KeyType,BYTE *CardKey,BYTE *Balance)
{
        BYTE i;

//--------------------------------------------------------------------
// Command: 0x60 0x0A 0x01 0x67 KEYTYPE BlockNo KEY0 … KEY5 BCC 0x03
// send 27 byte  13 * 2 + 1 = 27
//--------------------------------------------------------------------

        tmpData[0] = 0x60;
        tmpData[1] = 0x0A;
        tmpData[2] = 0x01;
        tmpData[3] = 0x67;
        tmpData[4] = KeyType;
        tmpData[5] = BlockNo;
        for(i = 0; i < 6; i++)  tmpData[6 + i] = CardKey[i];

        tmpData[12] = XORCheck(tmpData,12);

        ChangeHexToDbb(tmpData,CommArray,13);

        for(i = 0; i < 27; i++)
        {
                SoftUART_PutByte(CommArray[i]);
        }

//--------------------------------------------------------------------
// Right: 0x60 0x05 Balance0 …Balance3 0x00 BCC 0x03
// recieve 17 byte  8 * 2 + 1 = 17
// Error: 0x60 0x01 ERRNO BCC 0x03
// recieve 9 byte  4 * 2 + 1 = 9
//--------------------------------------------------------------------

        for(i = 0; i < 17; i++)
        {
                CommArray[i] = SoftUART_GetByte();
                if(CommArray[i] == 0x03) break;
        }

        if(CommArray[0] != '6') return 1;
        if(i < 10) return 1;

        ChangeDbbToHex(&CommArray[4],tmpData,8);

        for(i = 0; i < 4; i++) Balance[i] = tmpData[i];

//--------------------------------------------------------------------

        return 0;
}

//--------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -