Solved

ReadFile() in C++ builder

Posted on 1998-08-05
16
2,743 Views
Last Modified: 2012-05-04
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????
0
Comment
Question by:castong
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 7
  • 2
16 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 1169475
What is the third parameter, nbytestoread, set to?  That is the number of bytes to read.  Is it non-zero?
0
 
LVL 22

Expert Comment

by:nietod
ID: 1169476
You might try the following to read 8 butes.

BYTE Data[8],
DWORD nbytesthatwereread.

ReadFile(hCom, Data, 8,&nbytesthatwereread,NULL);
0
 

Author Comment

by:castong
ID: 1169477
nbytestoread is set to 8.
I try this:
ReadFile(hCom, Data, 8,&nbytesthatwereread,NULL);   with no result.




0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:castong
ID: 1169478
nbytestoread is set to 8.
I try this:
ReadFile(hCom, Data, 8,&nbytesthatwereread,NULL);   with no result.




0
 
LVL 22

Expert Comment

by:nietod
ID: 1169479
Is there data to be read?  If no bytes come in  in the 10 second wait time, it will return without reading anything?
0
 
LVL 22

Expert Comment

by:nietod
ID: 1169480
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
 

Author Comment

by:castong
ID: 1169481
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
 

Author Comment

by:castong
ID: 1169482
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
 
LVL 22

Expert Comment

by:nietod
ID: 1169483
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
 

Author Comment

by:castong
ID: 1169484
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
 
LVL 22

Expert Comment

by:nietod
ID: 1169485
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
 

Author Comment

by:castong
ID: 1169486


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
 

Author Comment

by:castong
ID: 1169487
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
 

Expert Comment

by:etep
ID: 1169488
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
 

Accepted Solution

by:
etep earned 70 total points
ID: 1169489
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
 
LVL 22

Expert Comment

by:nietod
ID: 1169490
etep, please read the questions before responding!  The problem was already solved.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question