none
problem in the serial progrmming code!!!

    Întrebare

  • 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 me 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);
     
    }


    BAA

    7 martie 2012 04:29

Răspunsuri

Toate mesajele

  • You don't have a valid read buffer.  The buffRead variable is an uninitialized pointer containing some garbage value.  Windows expects it to be the address of some memory that you have allocated.  Try this instead:

    char buffRead[256];

    7 martie 2012 05:29
  • Ya it worked but if there is no data on RXD pin compiler is not executing readfile function and remaining too. readfile Code is here... PLease help me..

    if (!ReadFile(hCom, &buffRead, nread, &dwBytesRead, NULL))

    { printf("error reading from input buffer \n"); getchar();

    }

    else

    {

    printf("Data read from read buffer is \n%s %d\n",buffRead,dwBytesRead);

    getchar();

    }

    CloseHandle(hCom);

    getchar();

    return (0); }


    BAA


    7 martie 2012 08:04
  • Ya it worked but if there is no data on RXD pin compiler is not executing readfile function and remaining too.

    Unless you have somehow arranged for your source code to be provided on the serial port, it's not the compiler which is 'not executing readfile function' here, but the running program itself.

    I  interpret your  phrase 'not executing readfile function and remaining too' as meaning that the ReadFile is not returning.

    This is normal behaviour for a synchronous ReadFile call on such a device.  It won't return until there is some data to read (in this case, on RXD pin).  If you want asynchronous behaviour, you need to use ReadFile's Overlapped mechanism (the final paramater which you have set to NULL), or that of ReadFileEx.


    Answering policy: see profile.

    7 martie 2012 09:37
  •  It won't return until there is some data to read (in this case, on RXD pin).  If you want asynchronous behaviour, you need to use ReadFile's Overlapped mechanism (the final paramater which you have set to NULL), or that of ReadFileEx.

    Besides of the Overlapped thing, you can specify read timeout, and any available data (or none) will be returned. See this

    -- pa

    7 martie 2012 10:27
  • Have you try to disable control in the DCB structure to configure your port:

    fRtsControl 

    fRtsControl

    fParity

    and all other parameters ...

    http://msdn.microsoft.com/en-us/library/windows/desktop/aa363214(v=vs.85).aspx


    Delphine GARRO

    • Marcat ca răspuns de Rob Pan 9 martie 2012 08:54
    7 martie 2012 11:35
  • Another error is in the printf of buffRead. The received string is not nul terminated, so printf will attempt to print more than just the number of bytes received.  This will print garbage and it might also cause an access violation. After receiving some bytes you should add

    buffRead[dwBytesRead] = 0;

    to properly nul terminate the string.

     

    7 martie 2012 15:19
  • Thanks alot for your suggestions Scott...

    BAA

    7 martie 2012 17:13
  • Thank you Pavel... I'll work on it..

    BAA

    7 martie 2012 17:15
  • Thank you for the information David...

    BAA

    7 martie 2012 17:23
  • Have you the solution ?

    Delphine GARRO

    7 martie 2012 17:34
  • Yeah Delphine thank you for the help...

    BAA

    7 martie 2012 18:19