Solved

ReadFile() in C++ builder

Posted on 1998-08-05
16
2,676 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
  • 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
 

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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now