ReadFile() in C++ builder

I want to read a character from serial port.

I use the fonction hCom=CreateFile("COM2",GENERIC_READ, 0,NULL,OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL|FILE_FLAG_NO_BUFFERING) to open the port.
 I already verify the configuration of the port with GetCommConfig(...) and set timeout
 with SetCommTimeouts(hCom, &tMyTimeOuts) where tMyTimeout is:

COMMTIMEOUTS tMyTimeOuts= {
MAXWORD,
MAXWORD,
10000,
1000,
1000};
 to have a delay of 10 sec to read the port.  After 10 sec.,the rest of the program is execute.

I call the fonction ReadFile() that way:

HANDLE hCom,
BYTE Data[8],
DWORD nbytestoread.


ReadFile(hCom, Data, nbytestoread,&nbytestoread,NULL);

The GetLastError() fonction return 0, ERROR_SUCCESS but there is nothing in the buffer Data...

printf("Data read:\"%s", Data);        

result on screen=

Data read:""

What's wrong with that code????
castongAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
etepConnect With a Mentor Commented:
You must init parameters for RS232 and use
 the functions :
    SetCommState(): set parameters for COM2 like speed,...
    GetCommState() : get parameters
    BuildCommBCD(): construct a command block for SetCommState
    WaitCommEvent(): indicate if you are received data or error
    CreateEvent()

Sample/Example:
    hPortSerie = CreateFile(      "COM2",                              GENERIC_READ|GENERIC_WRITE,
        0, NULL,                                          OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL ,NULL
                                          );
0
 
nietodCommented:
What is the third parameter, nbytestoread, set to?  That is the number of bytes to read.  Is it non-zero?
0
 
nietodCommented:
You might try the following to read 8 butes.

BYTE Data[8],
DWORD nbytesthatwereread.

ReadFile(hCom, Data, 8,&nbytesthatwereread,NULL);
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
castongAuthor Commented:
nbytestoread is set to 8.
I try this:
ReadFile(hCom, Data, 8,&nbytesthatwereread,NULL);   with no result.




0
 
castongAuthor Commented:
nbytestoread is set to 8.
I try this:
ReadFile(hCom, Data, 8,&nbytesthatwereread,NULL);   with no result.




0
 
nietodCommented:
Is there data to be read?  If no bytes come in  in the 10 second wait time, it will return without reading anything?
0
 
nietodCommented:
Is there a reason you are using FILE_FLAG_NO_BUFFERING?  It is hard to say what affect that would have on a communication port.  It may be ignored, or it may cause problems.  Try it without it.
0
 
castongAuthor Commented:
Even without the FILE_FLAG_NO_BUFFERING argument, the command doesn't seem to read the character transmit via modems.  When I run "Hyperterminal" with  the 2 computers, it communicate both ways( in/out for both computers). But when I try to send a character or a string from one computer to other (where my program run), there is no result.
 I have already tested the physical data transmission and it is ok.


0
 
castongAuthor Commented:
Even without the FILE_FLAG_NO_BUFFERING argument, the command doesn't seem to read the character transmit via modems.  When I run "Hyperterminal" with  the 2 computers, it communicate both ways( in/out for both computers). But when I try to send a character or a string from one computer to other (where my program run), there is no result.
 I have already tested the physical data transmission and it is ok.


0
 
nietodCommented:
Can you post the code related to the communication timeouts.  that is the values you specify and how you specify them?  Do you check the return value of SetCommTimeouts() to make sure that the settings are really getting changed?
0
 
castongAuthor Commented:
Here is the code concerning the timeouts.  



COMMTIMEOUTS tMyTimeOuts =  {  
    MAXDWORD, //  DWORD ReadIntervalTimeout;      
    MAXDWORD, //  DWORD ReadTotalTimeoutMultiplier;      
    10000,    //  DWORD ReadTotalTimeoutConstant;        
    1000,     //  DWORD WriteTotalTimeoutMultiplier;      
    1000,     //  DWORD WriteTotalTimeoutConstant;      
    };

    SetCommTimeouts(hCom, &tMyTimeOuts);  


    ReadFile(hCom, Data, nbytestoread, &nbytestoread,NULL);

    printf("\nErreur # %ld", GetLastError());
    char * pcMsgBuf;
    FormatMessage(
        FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,    NULL,
        GetLastError(),
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
        (char *)&pcMsgBuf,    0,    NULL );
    printf("\nError: %s", pcMsgBuf);
    printf("\nData read: \"%s", Data);
    printf("\"\n");


0
 
nietodCommented:
Are you getting an error when you set the timeouts?
I don't like the fact that you have set ReadTotalTimeoutMultiplier to a high value.  That could cause an arithmetic overflow when the value is used.  (That(may have nothing to do with your problems, however, its possible).

if you used

COMMTIMEOUTS tMyTimeOuts =  {    
         MAXDWORD, //  DWORD ReadIntervalTimeout;      
         1000, //  DWORD ReadTotalTimeoutMultiplier;      
         10000,    //  DWORD ReadTotalTimeoutConstant;        
         1000,     //  DWORD WriteTotalTimeoutMultiplier;      
         1000,     //  DWORD WriteTotalTimeoutConstant;        
         };

it would wait 1 second per byte to be read plus an extra 10 seconds.  Is that what you want?  or

COMMTIMEOUTS tMyTimeOuts =  {    
         MAXDWORD, //  DWORD ReadIntervalTimeout;      
         0, //  DWORD ReadTotalTimeoutMultiplier;      
         10000,    //  DWORD ReadTotalTimeoutConstant;        
         1000,     //  DWORD WriteTotalTimeoutMultiplier;      
         1000,     //  DWORD WriteTotalTimeoutConstant;        
         };

will just wait 10 seconds.

Try one of those, if that doesn't help (and I doubt it will)  reject my answer and see if anyone else can help.  You might need to up the points a little however.
0
 
castongAuthor Commented:


I can't read the port with the ReadTotalTimeoutMultiplier set to a lower value...  I'll increase points and hope someone can answer me!

Thanks a lot for your time and help!
0
 
castongAuthor Commented:
I solve the problem.  It was about the configuration of the port(Xon,Xoff characters...)  Thank you very much, nietod, for your usefull help!  You're a real expert!
0
 
etepCommented:
You must init parameters for RS232 and use
 the functions :
    SetCommState(): set parameters for COM2 like speed,...
    GetCommState() : get parameters
    BuildCommBCD(): construct a command block for SetCommState
    WaitCommEvent(): indicate if you are received data or error
    CreateEvent()

Sample/Example:
    hPortSerie = CreateFile(      "COM2",                              GENERIC_READ|GENERIC_WRITE,
        0, NULL,                                          OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL ,NULL
                                          );
0
 
nietodCommented:
etep, please read the questions before responding!  The problem was already solved.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.