gpt4 book ai didi

c++ - 从串口读取数据时出错

转载 作者:行者123 更新时间:2023-11-30 02:56:54 25 4
gpt4 key购买 nike

我正在用 Visual C++ 编写程序来访问串行端口。代码如下:-

#include "stdafx.h"
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <commdlg.h>

int nread,nwrite;


void main()
{

COMMTIMEOUTS timeouts;
COMMCONFIG dcbSerialParams;
char *words,*buffRead, *buffWrite;
DWORD dwBytesWritten,dwBytesRead;



HANDLE hSerial= CreateFile(L"COM1", GENERIC_READ | GENERIC_WRITE,
0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
if ( hSerial == INVALID_HANDLE_VALUE)
{
if (GetLastError() == ERROR_FILE_NOT_FOUND)
{
printf(" serial port does not exist \n");
}
printf(" some other error occured. Inform user.\n");
}


//DCB dcbSerialParams ;
//GetCommState( hSerial, &dcbSerialParams.dcb);
if (!GetCommState(hSerial, &dcbSerialParams.dcb))
{
printf("error getting state \n");
}

dcbSerialParams.dcb.DCBlength = sizeof(dcbSerialParams.dcb);


dcbSerialParams.dcb.BaudRate = CBR_1200;
dcbSerialParams.dcb.ByteSize = 8;
dcbSerialParams.dcb.StopBits = ONESTOPBIT;
dcbSerialParams.dcb.Parity = NOPARITY;

dcbSerialParams.dcb.fBinary = TRUE;
dcbSerialParams.dcb.fDtrControl = DTR_CONTROL_DISABLE;
dcbSerialParams.dcb.fRtsControl = RTS_CONTROL_DISABLE;
dcbSerialParams.dcb.fOutxCtsFlow = FALSE;
dcbSerialParams.dcb.fOutxDsrFlow = FALSE;
dcbSerialParams.dcb.fDsrSensitivity= FALSE;
dcbSerialParams.dcb.fAbortOnError = TRUE;


if (!SetCommState(hSerial, &dcbSerialParams.dcb))
{
printf(" error setting serial port state \n");
}


GetCommTimeouts(hSerial,&timeouts);

timeouts.ReadIntervalTimeout = 1000;
timeouts.ReadTotalTimeoutConstant = 1000;
timeouts.ReadTotalTimeoutMultiplier = 1000;
timeouts.WriteTotalTimeoutConstant = 1000;
timeouts.WriteTotalTimeoutMultiplier= 1000;

if(!SetCommTimeouts(hSerial, &timeouts))
{
printf("error setting port state \n");
}


//****************Write Operation*********************//
words = "B";
nwrite = strlen(words);

buffWrite = words;
dwBytesWritten = 0;

if (!WriteFile(hSerial, buffWrite, nwrite, &dwBytesWritten, NULL))
{
printf("error writing to output buffer \n");
}
printf("Data written to write buffer is\n %s \n",buffWrite);





//***************Read Operation******************//
buffRead = 0;
dwBytesRead = 0;
nread = strlen(words);

if (!ReadFile(hSerial, buffRead, nread, &dwBytesRead, NULL))
{
printf("error reading from input buffer \n");
}
printf("Data read from read buffer is \n %s \n",buffRead);

CloseHandle(hSerial);

}

以上程序在写操作(即在串口上写入数据)时正常工作,但在读操作时它不能从串口读取数据。

我在控制台窗口中得到的输出如下:-

Data written to write buffer is
B
error reading from input buffer
Data read from read buffer is
<null>

我想知道我在哪里出错以及如何解决它。

最佳答案

第二个 ReadFile 参数不能为 NULL。它应该是指向某个缓冲区的有效指针,例如:

dwBytesRead = 0;
nread = strlen(words);
buffRead = new char[nread + 1];
memset(buffRead, 0, nread+1]; // ensure that string will be null-terminated

if (!ReadFile(hSerial, buffRead, nread, &dwBytesRead, NULL))
{
printf("error reading from input buffer \n");
}

...
delete[] buffRead;

关于c++ - 从串口读取数据时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15128172/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com