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

20151209 김태현 개인업무일지 RFID 도스 시리얼

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

int main()
{
    u_char caString[255] = { 0x0D, 0x00, 0x71, 0x00, 0x30
                , 0x00, 0x00, 0x00
                , 0x0A, 0x00, 0x00, 0x46, 0x41 };
    DWORD dwCnt; 

    DCB sPState;        //struct Port State 시리얼 포트 상태 저장.
    HANDLE hComm = CreateFile("COM4"
        , 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;
    }

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

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

    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;
}

배열 값을 어떻게 주느냐에 따라서 소리를 바꾸거나 LED의 깜빡임을 조절할수 있다.
실행장면은 아직 장비가 저한테까지 안돌아와서....


728x90