1

I am writing a program in Visual C++ to access serial port.Code is given below:-

#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);

}

Above program is working properly while write operation (i.e writing data on serial port) but while read operation its is not reading data from serial port.

I am getting output on console window is given below:-

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

I want to know where I am getting wrong and how to resolve it.

1
  • I realize this comment might be moot, but are you sure that there is something to receive? That is, is the other end sending something back? Commented Feb 28, 2013 at 7:37

1 Answer 1

3

The second ReadFile parameter cannot be NULL. It should be valid pointer to some buffer, for example:

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;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.