c++ - 读取文件缓冲区传递

标签 c++ windows parameter-passing readfile uart

这可能是微不足道的,但我不明白为什么我没有在我的程序中读回。它似乎适用于所有“复杂的东西”,并且它说它已读取 1(字符/字节),但我无法掌握它;这似乎是类型匹配问题(顺便说一句,编译器 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]);

注意尾随 [0] Buf .

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

char Buf;

然后调用将是:

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

(在 & 上有一个 Buf 。)

关于c++ - 读取文件缓冲区传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34876955/

相关文章:

C++将长字符串传递给构造函数或setter

mysql - 您可以将 mysql 列别名传递给用户定义的函数吗

python - 函数的返回值作为类属性

c++ - Qt 有没有办法获取表单上的所有小部件?

fork 程序的 C++ shell 问题

windows - 使用 Windows 批处理脚本在 FOR 循环中计数

c++ - 如何释放指针数组指向的结构的内存?

c++ - 在 MinGW 上使用 iostreams 错误地打印了 Long double

c++ - VC++6显示中文

linux - 如何将 CMAKE 生成器添加到 Linux 版本的 CMAKE?