簡體   English   中英

一個readfile緩沖區傳遞

[英]A readfile buffer passing

這可能是微不足道的,但我無法理解為什么我不能在我的程序中讀回。 它似乎適用於所有“復雜的東西”,它說它已讀取1(字符/字節),但我無法掌握它; 它似乎是典型匹配問題(編譯器g ++(即gcc)btw這真的很奇怪)。

我如何改變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 ************************************

你要踢自己:問題是最終的printf (顯示值)應該是:

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

注意Buf上的尾隨[0]

或者,您可以將Buf聲明為:

char Buf;

然后電話會是:

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

(有一個& on Buf 。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM