#include <windows.h>
#include "resource.h"
#define ID_EDIT 100
#define ID_EDIT2 101
#define ID_BUTTON 102
#define ID_BUTTON2 103
#define CRC_POLYNOM 0x8408
#define CRC_PRESET 0xFFFF
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
void OpenSPort();
DWORD WINAPI Thread_Read(LPVOID NotUse);
unsigned short CRC16();
void ISO15693(void * vp);
HINSTANCE g_hInst;
LPCTSTR lpszClass=TEXT("Serial");
HWND hMain;
int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance
,LPSTR lpszCmdParam,int nCmdShow)
{
HWND hWnd;
MSG Message;
WNDCLASS WndClass;
g_hInst=hInstance;
WndClass.cbClsExtra=0;
WndClass.cbWndExtra=0;
WndClass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
WndClass.hCursor=LoadCursor(NULL,IDC_ARROW);
WndClass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
WndClass.hInstance=hInstance;
WndClass.lpfnWndProc=WndProc;
WndClass.lpszClassName=lpszClass;
WndClass.lpszMenuName=MAKEINTRESOURCE(IDR_MENU1);
WndClass.style=CS_HREDRAW | CS_VREDRAW;
RegisterClass(&WndClass);
hWnd=CreateWindow(lpszClass,lpszClass,WS_BORDER | WS_SYSMENU | WS_MINIMIZEBOX,
CW_USEDEFAULT,CW_USEDEFAULT,550,250,
NULL,(HMENU)NULL,hInstance,NULL);
ShowWindow(hWnd,nCmdShow);
while (GetMessage(&Message,NULL,0,0)) {
TranslateMessage(&Message);
DispatchMessage(&Message);
}
return (int)Message.wParam;
}
HANDLE hComm;
HANDLE hThread;
BOOL bPortOnOff;
BOOL bStart;
BOOL bEnd = TRUE;
BOOL bSwitch;
HWND hEdit;
HWND hEdit2;
LRESULT CALLBACK WndProc(HWND hWnd,UINT iMessage,WPARAM wParam,LPARAM lParam)
{
TCHAR caString[128] = TEXT("\0\0");
DWORD dwWritten;
HDC hdc;
PAINTSTRUCT ps;
static unsigned char ucPostComm[2][13] = {
// beep 음과
//{0x0D, 0x00, 0x71, 0x00, 0x0F, 0x00, 0x20, 0x00, 0x0A, 0x00, 0x00, 0xB2, 0x58},
{0x07, 0x00, 0xB0, 0x01, 0x00, 0x00, 0x00}
};
switch (iMessage)
{
case WM_CREATE:
hMain = hWnd;
hEdit = CreateWindow(TEXT("edit"), NULL, WS_CHILD | WS_VISIBLE
| WS_BORDER | ES_AUTOHSCROLL | ES_READONLY
, 50, 10, 400, 25, hWnd, (HMENU)ID_EDIT, g_hInst, NULL);
hEdit2 = CreateWindow(TEXT("edit"), NULL, WS_CHILD | WS_VISIBLE
| WS_BORDER | ES_AUTOHSCROLL | ES_READONLY
, 50, 45, 400, 25, hWnd, (HMENU)ID_EDIT2, g_hInst, NULL);
CreateWindow(TEXT("button"), TEXT("Clear"), WS_CHILD | WS_VISIBLE
| BS_PUSHBUTTON
, 460, 10, 60, 25, hWnd, (HMENU)ID_BUTTON, g_hInst, NULL);
CreateWindow(TEXT("button"), TEXT("Clear"), WS_CHILD | WS_VISIBLE
| BS_PUSHBUTTON
, 460, 45, 60, 25, hWnd, (HMENU)ID_BUTTON2, g_hInst, NULL);
return 0;
case WM_CHAR:
if(FALSE == bPortOnOff)
{
return 0;
}
if('1' != (TCHAR)wParam)
{
return 0;
}
*((unsigned short *)( ucPostComm[(TCHAR)wParam-'1'] + ucPostComm[(TCHAR)wParam-'1'][0] - 2 )) = CRC16(ucPostComm[(TCHAR)wParam-'1']);
SetDlgItemText(hWnd, ID_EDIT, NULL);
bStart = TRUE;
if(0 == WriteFile(hComm, ucPostComm[(TCHAR)wParam-'1'], ucPostComm[(TCHAR)wParam-'1'][0], &dwWritten, 0))
{
MessageBox(hMain, TEXT("쓰기 에러\n"), TEXT("알림"), MB_OK);
}
else
{
SendMessage(hEdit2, EM_REPLACESEL, (WPARAM)FALSE, (LPARAM)&caString[0]);
}
return 0;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case ID_OPEN:
OpenSPort();
break;
case ID_CLOSE:
if(TRUE == bPortOnOff)
{
CloseHandle(hComm);
CloseHandle(hThread);
bPortOnOff = FALSE;
}
break;
case ID_BUTTON:
SetDlgItemText(hWnd, ID_EDIT, NULL);
SetFocus(hWnd);
break;
case ID_BUTTON2:
SetDlgItemText(hWnd, ID_EDIT2, NULL);
SetFocus(hWnd);
break;
}
return 0;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
TextOut(hdc, 10, 13, TEXT("수신"), lstrlen(TEXT("수신")));
TextOut(hdc, 10, 48, TEXT("송신"), lstrlen(TEXT("송신")));
EndPaint(hWnd, &ps);
return 0;
case WM_LBUTTONDOWN:
SetFocus(hWnd);
return 0;
case WM_DESTROY:
CloseHandle(hComm);
CloseHandle(hThread);
PostQuitMessage(0);
return 0;
}
return(DefWindowProc(hWnd,iMessage,wParam,lParam));
}
void OpenSPort()
{
static DCB sPState;
static COMMTIMEOUTS cTime;
DWORD ThreadID;
if(TRUE == bPortOnOff)
{
CloseHandle(hComm);
CloseHandle(hThread);
// return 0;
bPortOnOff = FALSE;
}
hComm = CreateFile(TEXT("COM2"), GENERIC_READ | GENERIC_WRITE
, 0, NULL, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL, 0);
if(INVALID_HANDLE_VALUE == hComm)
{
MessageBox(hMain, TEXT("포트 열 수 없음"), TEXT("알림"), MB_OK);
return ;
}
if(0 == SetupComm(hComm, 4096, 3096))
{
MessageBox(hMain, TEXT("버퍼 설정 에러"), TEXT("알림"), MB_OK);
CloseHandle(hComm);
return ;
}
if(0 == PurgeComm(hComm, PURGE_TXABORT | PURGE_TXCLEAR))
{
MessageBox(hMain, TEXT("버퍼 초기화 에러"), TEXT("알림"), MB_OK);
CloseHandle(hComm);
return ;
}
sPState.DCBlength = sizeof(sPState);
if(0 == GetCommState(hComm, &sPState))
{
MessageBox(hMain, TEXT("시리얼 상태 읽기 에러"), TEXT("알림"), MB_OK);
CloseHandle(hComm);
return ;
}
sPState.BaudRate = CBR_38400;
sPState.ByteSize = 8;
sPState.Parity = EVENPARITY;
sPState.StopBits = ONESTOPBIT;
cTime.ReadIntervalTimeout = MAXDWORD; // 시간 설정, 블록킹 됐을시 기다리는 시간
cTime.ReadTotalTimeoutMultiplier = 0; // 0은 무한 대기
cTime.ReadTotalTimeoutConstant = 0;
cTime.WriteTotalTimeoutMultiplier = 0; // 0은 무한 대기
cTime.WriteTotalTimeoutConstant = 0;
SetCommTimeouts(hComm, &cTime);
if(0 == SetCommState(hComm, &sPState))
{
MessageBox(hMain, TEXT("시리얼 상태 설정 에러"), TEXT("알림"), MB_OK);
CloseHandle(hComm);
return ;
}
hThread = CreateThread(NULL, 0, Thread_Read, NULL, 0, &ThreadID);
bPortOnOff = TRUE; // 포트 열기가 완료되었으므로 TRUE
}
DWORD WINAPI Thread_Read(LPVOID NotUse)
{
unsigned char tt;
unsigned int tt2;
unsigned int iCnt = 0;
TCHAR buff[100] = TEXT("0x00 ");
unsigned char ucaMsg[255] = {255,};
DWORD dwRead;
while(1)
{
Sleep(50);
ReadFile(hComm, &tt, 1, &dwRead, NULL);
if(TRUE == bStart)
{
bStart = FALSE;
bEnd = FALSE;
iCnt = 1;
}
if(FALSE == bEnd)
{
ucaMsg[iCnt-1] = tt;
if(ucaMsg[0] == iCnt)
{
ISO15693(ucaMsg);
bEnd = TRUE;
}
}
wsprintf(buff, TEXT("0x%02X "), tt);
if(0 != dwRead)
{
++iCnt;
SendMessage(hEdit, EM_REPLACESEL, (WPARAM)FALSE, (LPARAM)buff);
}
}
return 0;
}
unsigned short CRC16(void * DATA)
{
unsigned short crc = CRC_PRESET;
int i;
int j;
for (i = 0; i < ((unsigned char *)DATA)[0]-2; i++) /* cnt = number of protocol bytes without CRC */
{
crc ^= ((unsigned char *)DATA)[i];
for (j = 0; j < 8; j++)
{
if (crc & 0x0001)
{
crc = (crc >> 1) ^ CRC_POLYNOM;
}
else
{
crc = (crc >> 1);
}
}
}
return crc;
}
void ISO15693(void * vp)
{
TCHAR buff[1024];
HDC hdc;
hdc = GetDC(hMain);
wsprintf(buff, TEXT("수신된 데이터 크기: [%d]"), ((unsigned char *)vp)[0]);
TextOut(hdc, 10, 100, buff, lstrlen(buff));
wsprintf(buff, TEXT("Com Address: [%d]"), ((unsigned char *)vp)[1]);
TextOut(hdc, 10, 123, buff, lstrlen(buff));
wsprintf(buff, TEXT("Com Address: [%d]"), ((unsigned char *)vp)[1]);
TextOut(hdc, 10, 123, buff, lstrlen(buff));
ReleaseDC(hMain, hdc);
}
'코스웨어 > 14년 스마트컨트롤러' 카테고리의 다른 글
20141024서상우보고서 (8) | 2014.10.26 |
---|---|
업무일지 박영주 (8) | 2014.10.23 |
20141022 일일업무보고서 11번 문대영 (8) | 2014.10.22 |
2014년 10월 21일 일일업무 출석번호 9번 김해성 (9) | 2014.10.21 |
MDS테크 신입 및 경력 모집 (0) | 2014.10.21 |
20141020월요일 업무일지 출석10번 김화린 (8) | 2014.10.21 |
2014.10.17 일일 보고서 김진철 (9) | 2014.10.17 |
20141016업무일지 7번 김재성 (9) | 2014.10.16 |