从串行端口读取数据时出错

时间:2013-02-28 05:30:48

标签: c++ visual-c++ serial-port

我正在用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 个答案:

答案 0 :(得分:3)

第二个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;