none
BytesRead=0 on serial com eVc4 RRS feed

  • Question

  • To readout data from the serial comm port "com1:" I use:

    song=ReadFile(

    hComm , // handlePort: handle of file to read

    pMsg , // inputData:handle of file to read

    400 , // sizeBuffer:number of bytes to read

    &BytesRead, // pointer to number of bytes read

    NULL ); // pointer to structure for data

    After executing the above line I find BytesRead=0. The platform configured with STANDARDSDK_500 Emulator. There are no error messages in my application. This same application runs successfuly on the api win32. Is there any missing setup in my application? Any hint is appreciated. Thanks.


    Esmat
    Sunday, September 25, 2011 2:12 AM

All replies

  • Which are the values of communication timeouts in the COMMTIMEOUT struct?

    Which value (TRUE/FALSE) does the ReadFile function returns?

    When you say "This same application runs successfuly on the api win32": does it mean that if you recompile the same code and you run it on the PC everything works as expected? What is connected to the serial port of the PC?

    Have you configured the emulator serial port "COM1:" to link to the above mentioned PC serial port?


    Luca Calligaris lucaDOTcalligarisATeurotechDOTcom www.eurotech.com Check my blog: http://lcalligaris.wordpress.com
    Monday, September 26, 2011 8:16 AM
  • Thanks Luca for your help again. Here are the answers to your queries:
    1. the communication timeouts:

     TimeOut.ReadIntervalTimeout         = 50;
     TimeOut.ReadTotalTimeoutConstant    = 50;
     TimeOut.ReadTotalTimeoutMultiplier  = 10;
     TimeOut.WriteTotalTimeoutConstant   = 50;
     TimeOut.WriteTotalTimeoutMultiplier = 10;

    2. ReadFile function returns TRUE

    3. This same application runs successfuly on the api win32. There are exactly 2 differnces: the first is for displaying the icons and this is resolved in http://social.msdn.microsoft.com/Forums/en-US/winembplatdev/thread/f57440aa-76fd-46ef-b331-1d23025a73dd/. The second difference is for using the PlaySound function. Otherwise they match exactly.

    4. Could you explain how to configure the emulator serial port "COM1:".
    Thank you.


    Esmat
    Tuesday, September 27, 2011 7:06 PM
  • I am reviving this thread because the problem of not reading serial data using ReadFile() is still persistent. This time I created a code that runs on windows and ReadFile performs as it should and reads serial data successfully. Exactly the same code compiles with no error on embedded vc++, but when executed, ReadFile() does not read even one byte. Under debugging, the function output is not null and there is no system error code after the execution of the ReadFile statement. I am at loss as to why ReadFile() is not performing and always shows that bytes_read = 0. Could there be a project setup problem? Is there any alternative for using ReadFile() to read serial data? I appreciate any hint. Thanks.


    Esmat

    Saturday, May 19, 2012 1:21 PM
  • There are lots of reasons why you might get bytes_read == 0:

    1. A problem with the driver
    2. Hardware problem including that the port may not be an RS-232, but instead RS-422/485
    3. You don't show initializing the serial port, do you initialize it?   If not, the defaults may be different from your PC.
    4. The timeouts may be different on you PC


    Bruce Eitman (eMVP)
    Senior Engineer
    Bruce.Eitman AT Eurotech DOT com
    My BLOG http://geekswithblogs.net/bruceeitman

    Eurotech Inc.
    www.Eurotech.com

    Monday, May 21, 2012 12:47 PM
    Moderator
  • Thanks Bruce for your reply. Please note that my code already is working with win32and ReadFile is working fine with it. I am using the same code with eVc++ and compile with no errors; ReadFile returns 1 (indicating it is working fine) and there is no error code that follow the read statement. Please refer to my earlier reply to see the comm setup.

    Esmat

    Monday, May 21, 2012 10:01 PM
  • Read my response again, I did take all of that into account.  No place do I assume that the code doesn't work someplace.

    Please review your earlier post, you don't show anything about the COM setup other than timeouts.   What I meant though was that the timeouts may need to be different from what you have set on your PC.   But more important, if you think that you did show your COM setup, then you must not be initializing the serial port with things like baud rate, stop bit... and that would be a critical mistake.


    Bruce Eitman (eMVP)
    Senior Engineer
    Bruce.Eitman AT Eurotech DOT com
    My BLOG http://geekswithblogs.net/bruceeitman

    Eurotech Inc.
    www.Eurotech.com

    Tuesday, May 22, 2012 12:41 PM
    Moderator
  • Bruce, thank you for time and patience. I took off the initialization part in the serial port and still could not get the ReadFile to read the data. Please see the code I used for setting up the serial port below. If there is still some improper initialization please let me know. Thanks.

    HANDLE SerComm(

    void){

    HANDLE hComm ;

    // Serial Com handle

    DCB dcb={0} ;

    // Data Comm Block

    COMMTIMEOUTS TimeOut={0};

    hComm = CreateFile( TEXT(

    "COM1:"),GENERIC_READ | GENERIC_WRITE,

    0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,0);

    /*

    Setup the Serial Port

    */

    dcb.DCBlength=

    sizeof(dcb);

    if(!GetCommState(hComm, &dcb)) {

    MessageBox(NULL, TEXT(

    "Error Getting CommState\n")

    , TEXT(

    "Error!"), MB_ICONEXCLAMATION | MB_OK);

    return 0;

    }

    //dcb.BaudRate= CBR_4800 ;// Specify buad rate of communicaiton.

    //dcb.ByteSize= 8 ;// Specify byte of size of communication. /*

    //dcb.StopBits= ONESTOPBIT;// Specify stopbit of communication.

    //dcb.Parity = NOPARITY ;// Specify parity of communication.

    //dcb.XonChar = '$' ;// Specify char on.

    // Load default comm timeouts

    if (GetCommTimeouts(hComm,&TimeOut) == 0){

    MessageBox(NULL, TEXT(

    "Error reading comm timeouts\n")

    , TEXT(

    "Error!"), MB_ICONEXCLAMATION | MB_OK);

    return 0;

    }

    TimeOut.ReadIntervalTimeout = 50;

    TimeOut.ReadTotalTimeoutConstant = 50;

    //

    TimeOut.ReadTotalTimeoutMultiplier = 10;

    TimeOut.WriteTotalTimeoutConstant = 50;

    TimeOut.WriteTotalTimeoutMultiplier = 10;

    if(!SetCommTimeouts( hComm, &TimeOut))

    {

    MessageBox(NULL, TEXT(

    "Error setting timeouts\n"),

    TEXT(

    "Error!"), MB_ICONEXCLAMATION | MB_OK);

    return 0;

    }

    return

    hComm;


    Esmat

    Wednesday, May 23, 2012 3:32 PM
  • Again, you need to set the baud rate, stop bits...   You simply cannot expect the defaults to work for your special needs.

    I don't see in your code where you do this, or have ever done this.  You commented out lines that set values in a structure, but if you don't send those to the driver it doesn't make any difference to how the driver works.


    Bruce Eitman (eMVP)
    Senior Engineer
    Bruce.Eitman AT Eurotech DOT com
    My BLOG http://geekswithblogs.net/bruceeitman

    Eurotech Inc.
    www.Eurotech.com

    Wednesday, May 23, 2012 5:48 PM
    Moderator
  • Hi Bruce, I actually was initializing the baud rate, stop bits ... then I misunderstood your earlier reply and commented them out. Here is my original function that sets up the serial function:

    #include

    <Windows.h>

    #include

    <commctrl.h>

    #include

    <Mmsystem.h>

    #pragma

    comment(lib, "Winmm.lib")

    #pragma

    comment(lib, "comctl32.lib")

    static

    HDC hDC ;

    RECT scr ;

    // screen

    HANDLE SerComm(

    void){

    HANDLE hComm ;

    // Serial Com handle

    DCB dcb={0} ;

    // Data Comm Block

    COMMTIMEOUTS TimeOut={0};

    hComm = CreateFile( TEXT(

    "COM1:"),

    GENERIC_READ | GENERIC_WRITE,

    0,

    0,

    OPEN_EXISTING,

    FILE_ATTRIBUTE_NORMAL,

    0);

    if (hComm == INVALID_HANDLE_VALUE)

    {

    MessageBox(NULL, TEXT(

    "Failed to open the communication port COM1!")

    , TEXT(

    "Error!"), MB_ICONEXCLAMATION | MB_OK);

    if(GetLastError()==ERROR_FILE_NOT_FOUND)

    MessageBox(NULL, TEXT(

    "Serial Port does not exist\n")

    , TEXT(

    "Error!"), MB_ICONEXCLAMATION | MB_OK);

    else

    MessageBox(NULL, TEXT(

    "Failed to open the communication port COM1\n")

    , TEXT(

    "Error!"), MB_ICONEXCLAMATION | MB_OK);

    return 0;

    }

    else{

    DrawText( hDC, TEXT(

    "COM1 opened successfully\0"), -1, &scr, DT_WORDBREAK);

    }

    /*

    Setup the Serial Port

    */

    dcb.DCBlength=

    sizeof(dcb);

    if(!GetCommState(hComm, &dcb)) {

    MessageBox(NULL, TEXT(

    "Error Getting CommState\n")

    , TEXT(

    "Error!"), MB_ICONEXCLAMATION | MB_OK);

    return 0;

    }

    dcb.BaudRate= CBR_4800 ;

    // Specify buad rate of communicaiton.

    dcb.ByteSize= 8 ;

    // Specify byte of size of communication. /*

    dcb.StopBits= ONESTOPBIT;

    // Specify stopbit of communication.

    dcb.Parity = NOPARITY ;

    // Specify parity of communication.

    dcb.XonChar =

    '$' ;// Specify char on.

    if(!SetCommState(hComm, &dcb)){

    MessageBox(NULL, TEXT(

    "Error setting serial port state\n")

    , TEXT(

    "Error!"), MB_ICONEXCLAMATION | MB_OK);

    return 0;

    }

    // Load default comm timeouts

    if (GetCommTimeouts(hComm,&TimeOut) == 0){

    MessageBox(NULL, TEXT(

    "Error reading comm timeouts\n")

    , TEXT(

    "Error!"), MB_ICONEXCLAMATION | MB_OK);

    return 0;

    }

    TimeOut.ReadIntervalTimeout = 50;

    TimeOut.ReadTotalTimeoutConstant = 50;

    //

    TimeOut.ReadTotalTimeoutMultiplier = 10;

    TimeOut.WriteTotalTimeoutConstant = 50;

    TimeOut.WriteTotalTimeoutMultiplier = 10;

    if(!SetCommTimeouts( hComm, &TimeOut))

    {

    MessageBox(NULL, TEXT(

    "Error setting timeouts\n"),

    TEXT(

    "Error!"), MB_ICONEXCLAMATION | MB_OK);

    return 0;

    }

    return

    hComm;

    }


    Esmat

    Thursday, May 24, 2012 3:30 PM