오전 ATmega
self boot loader-
DK128에 cmd를 이용하여 loader 시키는 방법.
통신 -
PC에서 가상 포트를 설정하여 통신 (com2 -com3)
단방향 통신 - cmd => 하이퍼 터미널
-
Port com2 --> com3
하이퍼터미널로 문자가 찍히게 된다.
#include <stdio.h>
#include <windows.h>
int main(void)
{
char szPort[15];
wsprintf(szPort, "COM%d", 3);
HANDLE m_hComm = NULL;
m_hComm = CreateFile(szPort, GENERIC_READ |
GENERIC_WRITE, 0, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);
if(m_hComm == INVALID_HANDLE_VALUE)
{
printf("(!) Failed to create a comm Device file");
return FALSE;
}
DCB dcb;
dcb.DCBlength = sizeof(DCB);
GetCommState(m_hComm, &dcb);
dcb.BaudRate = 9600;
dcb.ByteSize = 8;
dcb.Parity = 0;
dcb.StopBits = 0;
SetCommState(m_hComm, &dcb);
OVERLAPPED osWrite;
osWrite.Offset = 0;
osWrite.OffsetHigh = 0;
osWrite.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
char buf[100] = "안뇽 까꿍\r\n";
while(1)
{
printf("send : %s", buf);
WriteFile(m_hComm, buf, strlen(buf), NULL, &osWrite);
Sleep(1000);
}
CloseHandle(m_hComm);
return 0;
}
-양방향통신과 단방향통신-
위는 단방향 통신 예제와 양방향 통신 PPT 이다.
설명하기가 조금 애매 해서 위의자료를 참고 해야한다.
양방향 주고받기통신 소스(먼저받는부분)
#include <stdio.h>
#include <windows.h>
int main(void)
{
char szPort[15];
wsprintf(szPort, "COM%d", 3);
HANDLE m_hComm = NULL;
m_hComm = CreateFile(szPort, GENERIC_READ |
GENERIC_WRITE, 0, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);
if(m_hComm == INVALID_HANDLE_VALUE)
{
printf("(!) Failed to create a comm Device file");
return FALSE;
}
DCB dcb;
dcb.DCBlength = sizeof(DCB);
GetCommState(m_hComm, &dcb);
dcb.BaudRate = 9600;
dcb.ByteSize = 8;
dcb.Parity = 0;
dcb.StopBits = 0;
SetCommState(m_hComm, &dcb);
OVERLAPPED osWrite;
osWrite.Offset = 0;
osWrite.OffsetHigh = 0;
osWrite.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
OVERLAPPED osRead;
osRead.Offset = 0;
osRead.OffsetHigh = 0;
osRead.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
char buf[100] = "안뇽까꿍\r\n";
char buf1[100];
while(1)
{
ReadFile(m_hComm, buf1, 100, NULL, &osRead);
printf("recv1 : %s", buf1);
WriteFile(m_hComm, buf, strlen(buf)+1, NULL, &osWrite);
printf("send1 : %s", buf);
Sleep(2000);
}
CloseHandle(m_hComm);
return 0;
}
양방향 주고받기통신 소스(먼저보내는부분)
#include <stdio.h>
#include <windows.h>
int main(void)
{
char szPort[15];
wsprintf(szPort, "COM%d", 2);
HANDLE m_hComm = NULL;
m_hComm = CreateFile(szPort, GENERIC_READ |
GENERIC_WRITE, 0, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);
if(m_hComm == INVALID_HANDLE_VALUE)
{
printf("(!) Failed to create a comm Device file");
return FALSE;
}
DCB dcb;
dcb.DCBlength = sizeof(DCB);
GetCommState(m_hComm, &dcb);
dcb.BaudRate = 9600;
dcb.ByteSize = 8;
dcb.Parity = 0;
dcb.StopBits = 0;
SetCommState(m_hComm, &dcb);
OVERLAPPED osWrite;
osWrite.Offset = 0;
osWrite.OffsetHigh = 0;
osWrite.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
OVERLAPPED osRead;
osRead.Offset = 0;
osRead.OffsetHigh = 0;
osRead.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
char buf[100] = "안뇽\r\n";
char buf1[100];
while(1)
{
WriteFile(m_hComm, buf, strlen(buf)+1, NULL, &osWrite);
printf("send2 : %s", buf);
ReadFile(m_hComm, buf1, 100, NULL, &osRead);
printf("recv2 : %s", buf1);
Sleep(2000);
}
CloseHandle(m_hComm);
return 0;
}