본문 바로가기

[ATmega128] PC내에서 통신하기.(양방향)

반응형

실행하기 전에 하이퍼터미널을 한 번 사용해서 전화를 걸어줘야 한다.

에러~ -1을 입력하면 한번 주고 받고 프로그램이 종료된다.

11111111111을 이렇게 보내고 나서 받는 쪽에서 333을 보내면 333111111111

이렇게 출력된다. 초기화 됬다 안됬다 한다.

   

<송신부>

   

   

/* 단방향 통신 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)

  {  

    szPort[15= 0;

    cin >> buf;

      

    WriteFile(m_hcomm, buf, strlen(buf), NULL, &osWrite);

    printf("send1:%s\n" , buf);    //buf 받아서 출력

        Sleep(1000);

   

    check = atoi(buf);

      

        szPort[15= 0;

   

    ReadFile(m_hcomm, buf, 100, NULL, &osRead);

    printf("recv1:%s\n" , buf);    //buf 받아서 출력

    Sleep(1000);

        

      

  }

  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)

  {

    szPort[15= 0;

      

    ReadFile(m_hcomm, buf, 100, NULL, &osRead);

    printf("recv1:%s \n" , buf);    //buf 받아서 출력

    Sleep(1000);

      

    szPort[15= 0;

      

    check = atoi(buf);

    cin >> buf;

      

      

      

    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;

}

   

   

반응형
-->