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

20151210 22번 우대희 업무일지

by 알 수 없는 사용자 2015. 12. 10.
728x90
반응형
#include <Windows.h>
#include <stdio.h>

#define  CRC_POLYNOM    0x8408
#define  CRC_PRESET    0xFFFF

unsigned short CRC16(void *, unsigned int);

int main()
{
int iCnt;

u_char caString[255] =
{
0x05, 0x00, 0x65,
0x00, 0x3F,
0x00, 0x20,
0x00, 0x0A,
0x00, 0x00,
};
DWORD dwCnt;
DCB sPState;
HANDLE hComm = CreateFile("COM7", GENERIC_READ | GENERIC_WRITE,
0,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);

if (INVALID_HANDLE_VALUE == hComm)
{
printf("포트 열 수 없음\n");
return 0;
}

if (0 == SetupComm(hComm, 4096, 4096))
{
printf("버퍼 설정 에러\n");
CloseHandle(hComm);
return 0;
}

if (0 == PurgeComm(hComm, PURGE_TXABORT | PURGE_TXCLEAR))
{
printf("버퍼 초기화 에러\n");
CloseHandle(hComm);
return 0;
}

sPState.DCBlength = sizeof(sPState);

if (0 == GetCommState(hComm, &sPState))
{
printf("시리얼 상태 읽기 에러\n");
CloseHandle(hComm);
return 0;
}

sPState.BaudRate = CBR_38400;
sPState.ByteSize = 8;
sPState.Parity = EVENPARITY;
sPState.StopBits = ONESTOPBIT;

if (0 == SetCommState(hComm, &sPState))
{
printf("시리얼 상태 설정 에러\n");
CloseHandle(hComm);
return 0;
}

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

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 : %02X%02X\n", caString[4], caString[5]);
printf("D-REV   : %02X\n", caString[6]);
printf("HW-Type : %02X\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 (caString[10] & 0x80)
{
printf("I-Code UID, ");
}
if (caString[10] & 0x40)
{
printf("I-Code EPC, ");
}
if (caString[10] & 0x08)
{
printf("ISO 15693, ");
}
if (caString[10] & 0x02)
{
printf("Tag-it HF, ");
}
if (caString[10] & 0x01)
{
printf("I-Code 1, ");
}


//printf("수신한 바이트는 [%d]입니다.\n", (dwCnt+1));

CloseHandle(hComm);
return 0;
}

unsigned short CRC16(void *vpData, unsigned int uiLen)
{
unsigned int i;
unsigned int j;
unsigned char *DATA = vpData;
unsigned short crc = CRC_PRESET;
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;
}

아직 리더기를 못받아서 기다리는 중입니다 





728x90