一个readfile缓冲区传递

时间:2016-01-19 12:33:49

标签: c++ windows parameter-passing readfile uart

这可能是微不足道的,但我无法理解为什么我的程序中没有得到回读。它似乎适用于所有复杂的东西"并且它说它已读取1(字符/字节),但我无法掌握它;它似乎是典型匹配问题(编译器g ++(即gcc)顺便说一句,这真的很奇怪。)

我如何改变Buf的不同变体(如指针,char,char数组等)我无法掌握输入。

以下是现在的剥离代码和同步读取版本。哪个也应该编译。

#define WIN32_LEAN_AND_MEAN
#include <stdio.h>
#include <windows.h>
using namespace std;

//**********************************
//*******   M A I N  ****************
//**********************************

int main()    
{

HANDLE hComm;
int choice;

// non overlap test case (2nd last par = 0)

hComm = CreateFile( "COM4",  
                    GENERIC_READ | GENERIC_WRITE, 
                    0, 
                    NULL, 
                    OPEN_EXISTING,
                    0,
                    0);

if (hComm == INVALID_HANDLE_VALUE){
   // error opening port; abort
   printf("open error COM4\n");
   }
else
   printf("COM4 open");

printf("\n Hi, this is a UART attempt:"); scanf("%d", &choice);


// build the control block

DCB           dcb={0};


printf("******dcb******\n");

dcb.DCBlength = sizeof(dcb);

if ( !GetCommState(hComm, &dcb)) printf("Get DCB error");  // I dont think this should be needed ?


if ( ! BuildCommDCB( "4800,n,8,2" , &dcb ) ) {
    // error
    printf("COM4 buidDCB -- error\n");
    return(1);
    }


printf("****here*****\n");

// put the control block into action

if (! SetCommState( hComm, &dcb )  ){
    // error
    printf("COM4 setCommState -- error:%d \n",(int)GetLastError() );
    return(1);
    }

printf("seem successfull \n");


/*************************************READ non-ASYNC TESTING ************************************/
/********************************************************************************************/

char  Buf[1];

/* initiate waiting for reading on UART */

DWORD dwRead;

// Issue rea


if ( ! ReadFile(hComm, Buf, 1 , &dwRead, 0)) {
      // Error in communications; report it.
      printf("ReadFile --- error:%d",(int)GetLastError());
}
else {    
   // read completed
   printf("read <%d>---%o--- \n",(int)dwRead,Buf[0]);
}


return 0;
}  //*** end main ************************************

1 个答案:

答案 0 :(得分:1)

你要踢自己:问题是最后的printf(显示值)应该是:

  printf("--- immediate read <%d>---%o--- \n",(int)dwRead,Buf[0]);

请注意[0]上的结尾 Buf

或者,您可以将Buf声明为:

char Buf;

然后调用将是:

    if ( ! ReadFile(hComm, &Buf, 1, &dwRead, &osReader)) {

&上有 Buf 。)