win32 를 이용한 시리얼 프로그래밍에서 에러의 원인을 잘 모르
글쓴이: ssif / 작성시간: 화, 2005/08/16 - 12:00오후
#include <windows.h>
#include <stdio.h>
//#define PORT "COM2"
#define BUFSIZE 3
OVERLAPPED ov;
char buf[BUFSIZE];
HANDLE hCom;
int main(void)
{
DCB dcb;
//HANDLE hCom;
BOOL fSuccess;
char *pcCommPort = "COM1";
//int buf[BUFSIZE];
DWORD dwWritten;
int writefile_return;
/********************************************************************************
비동기 입출력은 WINDOWS 2000/NT 만 지원이 된다.
즉,98SE ,ME 이하의 버전에서는 동작하지 않는다.
CREATEfILE() 에서 플래그는 6번째 인수에 FILE_FLAG_OVERLAPPED 로 사용하면 된다.
단, OVERLAPPED 구조체를 선언하고 사용해야 한다.왜냐하면 비동기 입출력 을 하는 동안
계속 유효해야 하므로 전역으로 선언하고 사용을 해야 한다.
*********************************************************************************
*/
hCom = CreateFile( pcCommPort,
GENERIC_READ | GENERIC_WRITE,
0, // must be opened with exclusive-access
NULL, // no security attributes
OPEN_EXISTING, // must use OPEN_EXISTING
FILE_FLAG_OVERLAPPED, //0, not overlapped I/O
NULL // hTemplate must be NULL for comm devices
);
printf("createfile init\n");
if (hCom == INVALID_HANDLE_VALUE)
{
// Handle the error.
printf ("CreateFile failed with error %d.\n", GetLastError());
return (1);
}
// Build on the current configuration, and skip setting the size
// of the input and output buffers with SetupComm.
fSuccess = GetCommState(hCom, &dcb);
printf("getcommstate test\n");
if (!fSuccess)
{
// Handle the error.
printf ("GetCommState failed with error %d.\n", GetLastError());
return (2);
}
else
{
ov.Offset=0;
ov.OffsetHigh=0;
ov.hEvent=NULL;
printf("message input: ");
fgets(buf,BUFSIZE,stdin);
fputs(buf,stdout);
/*************************************************************************
비동기 입출력을 위해 writefile()의 마지막 인수에 NULL 을 전달 하지 않고
OVERLAPPED구조체를 사용한다.이 구조체의 주소를 전달하면 된다.
OVERLAPPED 구조체는 전역으로 선언했다.
************************************************************************
*/
writefile_return=WriteFile(hCom,buf,BUFSIZE,&dwWritten,&ov);
if(writefile_return==0) //if writefile failed....
{
printf("writefile error!\n");
return 0;
}
else
{
printf("writefile success!\n");
}
}
CloseHandle(hCom);
printf ("Serial port %s successfully reconfigured.\n", pcCommPort);
return (0);
}
devcpp에서 win32 api를 사용해서 시리얼 통신 프로그래밍을 하고 있습니다.장치를 열고 쓴 다음 닫는 그런 코드입니다.그런대 실재로 실행을 시켜보면 "writefile_return"값이 0이 나옵니다.즉,쓰기가 않되는 상황에 있습니다.왜 그런지 원인을 못찾고 있습니다.
로직상에서는 크게 문제가 될 곳이 없다고 보여지는 실제 왜 에러가 나오는지 잘 모르겠습니다.미리 감사드립니다.
Forums:


CreateFile 하실때 인자값중...
CreateFile 첫번째 인자에 "COM1"이라는 값을 넣었을 것입니다.
그것을 \\\\.\\COM1 형식으로 바꿔주셔야 합니다.
char buf[32];
sprintf(buf,"\\\\.\\%s", pcCommPort);
CreateFile(buf, ....
다시 한번 해보세요...
/***************************************************
* 가장 심플한 것이 가장 아름다운 것이다.
***************************************************/
댓글 달기