从串口读取数据时出错
Posted
技术标签:
【中文标题】从串口读取数据时出错【英文标题】:Error while reading data from serial port 【发布时间】:2013-02-28 05:30:47 【问题描述】:我正在用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>
我想知道我哪里出错了以及如何解决它。
【问题讨论】:
我知道这条评论可能没有实际意义,但你确定有什么要接收的吗?也就是说,对方有没有回送东西? 【参考方案1】:第二个 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;
【讨论】:
以上是关于从串口读取数据时出错的主要内容,如果未能解决你的问题,请参考以下文章