• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2883
  • Last Modified:

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????
0
castong
Asked:
castong
  • 7
  • 7
  • 2
1 Solution
 
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
 
castongAuthor Commented:
nbytestoread is set to 8.
I try this:
ReadFile(hCom, Data, 8,&nbytesthatwereread,NULL);   with no result.




0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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
 
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

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 7
  • 7
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now