본문 바로가기
코스웨어/15년 스마트컨트롤러

20151210 수업 / RFID_3-남수진

by 알 수 없는 사용자 2015. 12. 10.
728x90
반응형

RFID

  • RFID 관련 정보 얻기

image

Image(166)

Image(168)

#include <windows.h>

#include <stdio.h>

#define CRC_POLYNOM     0x8408

#define CRC_PRESET          0xFFFF

unsigned short CRC16(void * vpData, unsigned int uiLen);

int main(void)

{

    //u_char caString[255] = {0x0D, 0, 0x71, 0, 0x0F, 0, 0, 0, 0x0A, 0, 0};

    u_char caString[255] = {0x05, 0, 0x65, 0, 0x0F, 0, 0, 0, 0x0A, 0, 0};

    int iCnt;

    DWORD dwCnt;

    DCB sPState;

    u_char ucTmp;

    // 1. 시리얼 포트 열기

    HANDLE hComm = CreateFile("COM6"        // 포트 이름

        , GENERIC_READ|GENERIC_WRITE        // RW가능

        , 0

        , NULL

        , OPEN_EXISTING

        , FILE_ATTRIBUTE_NORMAL

        , 0);

    if(INVALID_HANDLE_VALUE == hComm)

    {

        printf("포트 열 수 없음\n");

        return 0;

    }

    // 2. Read버퍼와 write 버퍼 길이 설정

    if( 0 == SetupComm(hComm, 4096, 4096))

    {

        printf("버퍼 설정 에러\n");

        CloseHandle(hComm);

        return 0;

    }

    // 3. 버퍼 초기화

    if(0 == PurgeComm(hComm, PURGE_TXABORT|PURGE_TXCLEAR))   

    {

        printf("버퍼 초기화 에러\n");

        CloseHandle(hComm);

        return 0;

    }

    sPState.DCBlength = sizeof(sPState);

    // 4. 현재 시리얼 세팅을 읽어온다

    if( 0 == GetCommState(hComm, &sPState))

    {

        printf("시리얼 상태 읽기 에러\n");

        CloseHandle(hComm);

        return 0;

    }

    // 5. 시리얼 설정

    sPState.BaudRate = CBR_38400;

    sPState.ByteSize = 8;

    sPState.Parity = EVENPARITY;

    sPState.StopBits = ONESTOPBIT;

    // 6. 시리얼 세팅 적용

    if(0 == SetCommState(hComm, &sPState))

    {

        printf("시리얼 상태 설정 에러\n");

        CloseHandle(hComm);

        return 0;

    }

    // 7. CRC값 대입

    *((unsigned short *)(caString + caString[0] -2)) = CRC16(caString, caString[0]-2);

    // 8. 시리얼 통신

    WriteFile(hComm, caString, caString[0], &dwCnt, 0);

    ReadFile(hComm, caString, 1, &dwCnt, 0);

    ReadFile(hComm, caString+1, caString[0]-1, &dwCnt, 0);

    for(iCnt = 0; iCnt<caString[0]; iCnt++)

    {

        printf("0x%02X, ", caString[iCnt]);

    }

    putchar('\n');

    printf("SW-REV : %d\n", *((unsigned short *)(caString+4)));

    printf("D-REV  : %d\n", caString[6]);

    printf("HW-TYPE: %d\n", caString[7]);

    printf("SW-TYPE: ");

    switch(caString[8])

    {

        case 30: printf("ID ISC.M01\n");

                break;

        case 31: printf("ID ISC.M02\n");

                break;

        case 71: printf("ID ISC.PRH100-U (USB-Version)\n");

                break;

        case 72: printf("ID ISC.PRH100\n");

                break;

        case 73: printf("ID ISC.MR100-U (USB-Version)\n");

                break;

        case 74: printf("ID ISC.MR100 / .PR100\n");

                break;

        case 75: printf("ID ISC.MR200-A / -E\n");

                break;

        case 40: printf("ID ISC.LR100\n");

                break;

        case 41: printf("ID ISC.LR200\n");

                break;

        case 91: printf("ID ISC.LRU1000\n");

                break;

        case 80: printf("ID CPR.M02\n");

                break;

        case 81: printf("ID CPR.02\n");

                break;

        case 84: printf("ID CPR.M03 (586/#)\n");

                break;

    }

    printf("TR-TYPE: ");

    if(0 != (caString[10]&0x80))

    {

        printf("I-Code UID, ");

    }

    if(0 != (caString[10]&0x40))

    {

        printf("I-Code EPC, ");

    }

    if(0 != (caString[10]&0x08))

    {

        printf("ISO 15693, ");

    }

    if(0 != (caString[10]&0x02))

    {

        printf("Tag-it HF, ");

    }

    if(0 != (caString[10]&0x01))

    {

        printf("I-Code 1, ");

    }

    putchar('\n');

    CloseHandle(hComm);

    return 0;

}

unsigned short CRC16(void * vpData, unsigned int uiLen)

{

    int i, j;

    unsigned short crc = CRC_PRESET;

    unsigned char * DATA = vpData;

    for (i = 0; i < uiLen; i++)

    {

        crc ^= DATA[i];

        for (j = 0; j < 8; j++)

        {

            if (crc & 0x0001)

                crc = (crc >> 1) ^ CRC_POLYNOM;

            else

                crc = (crc >> 1);

        }

    }

    return crc;

}

 


  • 호스트에서 리더기로 데이터를 요청하는 명령(0xB0)
    • 데이터 길이로 카드의 타입을 알 수 있다

Image(169)

    • 0xB0 중 0x01(Inventory)의 명령/응답

Image(170)

Image(171)


728x90