Com port를 사용해서 핸드폰과 PC를 연결 하려고 합니다.
우선 write test를 했을때 Tx Buffer에 쓰여지기는 하는것 같습니다. 근데 핸드폰 쪽으로 data가 실제 전송이 일어나진 않습니다.
제가 test를 해본 방법은요,
아래 source로 컴파일 해서 실행시키면 "at%LANG?"라는 명령어를 writeFile 로 buffer에 쓰면요, buffer에 쓰이긴 했는데
data가 핸드폰으로 전송되지 않아서 핸드폰에서 아무 반응이 없습니다.
근데 이 상태에서 위 프로그램 종료 시키면 아직 아무변화 없구요,
Hyper Terminal 열어서 ComPort로 핸드폰으로 연결해서 그냥 Termianl 상 Connected 상에서 Enter 만 치면,
제가 아까 WriteFile 로 입력했던 "at%LANG?" 명령어가 실행이 됩니다. ㅠㅠ 왜그런지 모르겠네요. ㅠㅠ
Tx Buffer 에 쓰이긴 했는데, 실제로 data 가 핸드폰 쪽으로 날아가지 않는 이유가 무엇인가요?
Hyper Terminal 프로그램에서 Enter 만 쳐도 buffer에 저장되었던 data가 전송이 되는데 ㅡㅡ;
실제로 Buffer에 data를 쓴다음에 SendPort 이런 명령어로 buffer 에껄 Comport로 써줘야하는건가요?
도움좀 부탁드립니다. Hyper Terminal 과같은 연결할 수 있는 프로그램을 만들고자 합니다.
#include "stdafx.h"
#include "windows.h"
#include "conio.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
HANDLE hFile;
#ifndef _OVERLAPPED
#define _OVERLAPPED
#endif
OVERLAPPED lpOverlapped;
void OpenCom() // Com 4번 Setting
{
DCB dcb;
COMMTIMEOUTS ct;
hFile = CreateFile("COM5", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL , NULL);
if(hFile == INVALID_HANDLE_VALUE)
{
printf("COM port 연결 할 수 없음\n");
exit(1);
}
if(!SetupComm(hFile, 4096, 4096)) exit(1);
if(!GetCommState(hFile, &dcb)) exit(1);
dcb.BaudRate = 115200;
//((DWORD*)(&dcb))[2] = 0x1001; // set port properties for TXDI + no flow-control
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = 1;
SetCommState(hFile, &dcb);
// set the timeouts to 0
ct.ReadIntervalTimeout = MAXDWORD;
ct.ReadTotalTimeoutMultiplier = 0;
ct.ReadTotalTimeoutConstant = 0;
ct.WriteTotalTimeoutMultiplier = 0;
ct.WriteTotalTimeoutConstant = 0;
if(!SetCommTimeouts(hFile, &ct)) exit(1);
}
void WriteCom(LPCVOID IpBuf, DWORD len) // Com Write function
{
DWORD nSend;
WriteFile(hFile, IpBuf, len, &nSend, NULL );
printf( " length of Write[%d] = %s\n",nSend, IpBuf);
}
void ReadCom(char *a, DWORD lgg) // Com Read function
{
DWORD nRec;
ReadFile(hFile, a, lgg, &nRec, NULL);
printf(" length of Receive[%d] : %s , size of a[]=%d\n", nRec,a, strlen(a));
}
int main() // Main
{
char c,s[256];
char read_buf[256]="";
OpenCom();
WriteCom("at%LANG?", 8); // ComPort 4에 AT command 를 Write
Sleep(100);
ReadCom(read_buf, 256); // ComPort 4로 IN buffer로 들어오는 data Read
CloseHandle(hFile);
return 0;
}
아 ㅋㅋ 질문 올리면서 문득 생각난게 있어서 test 해봤는데 되네요. ㅡㅡ;
text를 보낼때 Return 문자를 넣어줬어야하는데,,
"at%LANG?\r" 요렇게 쓰니깐 되네요. ^^;
혹시 다른분들에게 도움될까 글 남겨 두겠습니다.