读取文件缓冲区传递

A readfile buffer passing

这可能是微不足道的,但我不明白为什么我没有在我的程序中读回。它似乎对所有 "complex stuff" 都能正常工作,它说它已读取 1 (character/byte),但我无法掌握它;这似乎是类型匹配问题(顺便说一句,编译器 g++(即 gcc)这真的很奇怪)。

我如何更改 Buf 的不同变体(如指针、字符、字符数组等)我无法获取输入。

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

#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 ************************************

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

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

注意 Buf 上的尾随 [0]

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

char Buf;

然后调用将是:

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

& Buf。)