프로그램을 설치
>>> Eltima Software Virtual Serial Port Driver 6.0.1.115
송신용
/* 단방향 통신 main.cpp*/
#include <stdio.h>
#include <windows.h>
#include <iostream>
using namespace std;
int main(void)
{
//int a;
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);
//int b;
int check;
char buf[100] = "\r\n"; //출력할 내용
while(check != -1)
{
cin >> buf;
WriteFile(m_hcomm, buf, strlen(buf), NULL, &osWrite);
printf("send1:%s\n" , buf); //buf 받아서 출력
Sleep(1000);
/*ReadFile(m_hcomm, buf, 100, NULL, &osRead);
printf("recv1:%s\n" , buf); //buf 받아서 출력
Sleep(1000);*/
check = atoi(buf);
}
CloseHandle(m_hcomm);
return 0;
}
수신용
/* 단방향 통신 main.cpp*/
#include <stdio.h>
#include <windows.h>
#include <iostream>
using namespace std;
int main(void)
{
int a;
char szPort[15]; //문자배열 생성.
wsprintf(szPort, "COM%d" , 5); //포트 설정하는 부분
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);
int check;
char buf[100] = "\r\n";
//출력할 내용
while(check != -1)
{
ReadFile(m_hcomm, buf, 100, NULL, &osRead);
printf("recv1:%s \n" , buf); //buf 받아서 출력
Sleep(1000);
/*WriteFile(m_hcomm, buf, strlen(buf), NULL, &osWrite);
printf("write:%s\n" , buf); //buf 받아서 출력
Sleep(1000);*/
}
CloseHandle(m_hcomm);
return 0;
system("PAUSE");
return EXIT_SUCCESS;
}
'About 프로그래밍!!! > ATmega128' 카테고리의 다른 글
[ATmega128] 복습 (0) | 2010.07.20 |
---|---|
[ATmega128] PC내에서 통신하기.(양방향) (0) | 2010.07.19 |
[ATmega128] ATmega128의 메모리 구조. (0) | 2010.07.19 |
[ATmega128] EEPROM 실패~몰겠당 (0) | 2010.06.18 |
[ATmeaga129] 99~0 (0) | 2010.06.17 |