Thread: Error while reading data from serial port

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    5

    Error while reading data from serial port

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


    Code:
        #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:-
    Code:
        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.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Saad Rafey View Post
    Code:
    void main()
    this is the first mistake that immediately stood out to me. the correct form is

    Code:
    int main()
    
    or
    
    int main(int argc, char** argv)
    the second mistake you made was posting C code in the C++ forum. an alternative to this would be to post in the windows forum, as you are clearly using windows-specific system calls.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    First of all, your indentation is poor.
    Code:
    #include "stdafx.h"
    #include <windows.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <commdlg.h>
    
    //!! why are these global?
    int nread, nwrite;
    
    int main()  //!! main returns int - not void!
    {
      COMMTIMEOUTS timeouts;
      COMMCONFIG dcbSerialParams;
      char *words, *buffRead, *buffWrite;
      DWORD dwBytesWritten, dwBytesRead;
    
      //!! http://msdn.microsoft.com/en-us/library/c426s321%28v=vs.80%29.aspx
      //!! Use _T("COM1") then your code will still compile and work
      //!! if you switch between ANSI and UNICODE
      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);  //!! see the 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);
    
      //!! all done successfully
      return 0;
    }
    1. Pay attention to all the other //!! comments in the code.

    2. The reason for the crash is as follows - you're not pointing to a buffer you can write to.
    buffRead is a NULL pointer!

    Try something like
    char buffRead[100];
    nread = sizeof(buffRead); // note, not strlen()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Feb 2013
    Posts
    5
    @Elkvis thanks for your help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Serial port reading data
    By saikgr in forum C Programming
    Replies: 6
    Last Post: 12-07-2012, 07:57 AM
  2. Data structure for storing serial port data in firmware
    By james457 in forum C Programming
    Replies: 4
    Last Post: 06-15-2009, 09:28 AM
  3. my serial port data reading program isn't working
    By gnychis in forum C Programming
    Replies: 5
    Last Post: 06-02-2005, 08:40 AM
  4. how to get data from the serial port?
    By Jasonymk in forum C++ Programming
    Replies: 1
    Last Post: 02-26-2003, 07:31 AM
  5. serial port, reading from
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 06-01-2002, 09:37 AM

Tags for this Thread