locked
problem in the serial progrmming code!!! RRS feed

  • Question

  • Hello, I am using Windows 7, the code i have written for serial programming is not receiving the data transmitted :when I connected a null modem to the serial port to read the string i wrote to the serial port but nothing is read on serial port. The program copiles, builds and executes but i get error reading. 
    can anybody help  please !!!

    The code i wrote is:

    #include <windows.h>
    #include <stdio.h>
    int nread,nwrite;
    int main(int argc, char *argv[])
    {
       DCB dcb;
       HANDLE hCom;
       BOOL fSuccess;
       char *pcCommPort = "COM2";
       char *words, *buffRead, *buffWrite;
       DWORD dwBytesWritten, dwBytesRead;
     
       hCom = CreateFile( pcCommPort, GENERIC_READ | GENERIC_WRITE, 0,    // must be opened with exclusive-access
                        NULL, // no security attributes
                        OPEN_EXISTING, // must use OPEN_EXISTING
                        0,    // not overlapped I/O
                        NULL  // hTemplate must be NULL for comm devices
                        );
     
       if (hCom == INVALID_HANDLE_VALUE)
       {
           // Handle the error.
           printf ("CreateFile failed with error %d.\n", GetLastError());
           getch();
           return (1);
       }
     
       // Build on the current configuration, and skip setting the size
       // of the input and output buffers with SetupComm.
     
       fSuccess = GetCommState(hCom, &dcb);
     
       if (!fSuccess)
       {
          // Handle the error.
          printf ("GetCommState failed with error %d.\n", GetLastError());
          getch();
          return (2);
       }
     
       // Fill in DCB: 57,600 bps, 8 data bits, no parity, and 1 stop bit.
     
       dcb.BaudRate = CBR_9600;     // set the baud rate
       dcb.ByteSize = 8;             // data size, xmit, and rcv
       dcb.Parity = NOPARITY;        // no parity bit
       dcb.StopBits = ONESTOPBIT;    // one stop bit
     
       fSuccess = SetCommState(hCom, &dcb);
     
       if (!fSuccess)
       {
          // Handle the error.
          printf ("SetCommState failed with error %d.\n", GetLastError());
          getch();
          return (3);
       }
     
       printf ("Serial port %s successfully reconfigured.\n", pcCommPort);
      
       
    //****************Write Operation*********************//
    //words = "This is a string to be written to serial port COM1";
    words = "Ab";
    nwrite = strlen(words);
     
    buffWrite = words;
    dwBytesWritten = 0;
     
    if (!WriteFile(hCom, 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);
    printf ( "Nread = %d\n",nread );
     
    if (!ReadFile(hCom, buffRead, nread, &dwBytesRead, NULL))
    {
    printf("error reading from input buffer \n");
    }
    else
    {
    printf("Data read from read buffer is \n %s %d\n",buffRead,dwBytesRead);
    }
    getch();
     
    CloseHandle(hCom);  
     
    return (0);
     
    }
     

    Monday, March 5, 2012 7:31 PM

All replies