Solved

ReadFile() in C++ builder

Posted on 1998-08-05
16
2,688 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Want to delete all my personal data 13 138
is twain_32.dll cmpatible with windows 10 ? 10 127
gdb doesn't stop on breakpoint 2 50
designing in object programming 12 42
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

896 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

11 Experts available now in Live!

Get 1:1 Help Now