win32 를 이용한 시리얼 프로그래밍에서 에러의 원인을 잘 모르

ssif의 이미지

#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이 나옵니다.즉,쓰기가 않되는 상황에 있습니다.왜 그런지 원인을 못찾고 있습니다.
로직상에서는 크게 문제가 될 곳이 없다고 보여지는 실제 왜 에러가 나오는지 잘 모르겠습니다.미리 감사드립니다.

lovemyin의 이미지

CreateFile 첫번째 인자에 "COM1"이라는 값을 넣었을 것입니다.

그것을 \\\\.\\COM1 형식으로 바꿔주셔야 합니다.

char buf[32];
sprintf(buf,"\\\\.\\%s", pcCommPort);
CreateFile(buf, ....

다시 한번 해보세요...

/***************************************************
* 가장 심플한 것이 가장 아름다운 것이다.
***************************************************/

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.