Click here to Skip to main content
15,890,372 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i my serial port reads only 30 bytes ?
i cant find issue in dcb though.

DCB dcb_ob;
COMMTIMEOUTS ct_ob;

dcb_ob.DCBlength =32;//sizeof(DCB);
dcb_ob.BaudRate = CBR_115200;                    // Current baud
dcb_ob.fBinary = TRUE;                       // Binary mode; no EOF check
dcb_ob.fParity = TRUE;                       // Enable parity checking
dcb_ob.fOutxCtsFlow = FALSE;                 // No CTS output flow control
dcb_ob.fOutxDsrFlow = FALSE;                 // No DSR output flow control
dcb_ob.fDtrControl = DTR_CONTROL_ENABLE ;    // DTR flow control type
dcb_ob.fDsrSensitivity = FALSE;          // DSR sensitivity
dcb_ob.fTXContinueOnXoff = TRUE;             // XOFF continues Tx
dcb_ob.fOutX = FALSE;                        // No XON/XOFF out flow control
dcb_ob.fInX = FALSE;                         // No XON/XOFF in flow control
dcb_ob.fErrorChar = FALSE;                  // Disable error replacement
dcb_ob.fNull = FALSE;                       // Disable null stripping
dcb_ob.fRtsControl = RTS_CONTROL_ENABLE ;    // RTS flow control
dcb_ob.fAbortOnError = FALSE;               // Do not abort reads/writes on error
dcb_ob.ByteSize = 8;                        // Number of bits/byte, 4-8
dcb_ob.Parity = NOPARITY;                   // 0-4=no,odd,even,mark,space
dcb_ob.StopBits = ONESTOPBIT;               // 0,1,2 = 1, 1.5, 2



ct_ob.ReadIntervalTimeout = MAXDWORD;
ct_ob.ReadTotalTimeoutMultiplier = 50;
ct_ob.ReadTotalTimeoutConstant = 50;
ct_ob.WriteTotalTimeoutMultiplier = 50;
ct_ob.WriteTotalTimeoutConstant = 1000;



functions

C#
CreateFile(   "COM3",
            GENERIC_READ | GENERIC_WRITE,
            0,                               // must be opened with exclusive-access
            NULL,                            // no security attributes
            OPEN_EXISTING,                   // must use OPEN_EXISTING
            0,                               // not overlapped I/O
            NULL                             // hTemplate must be NULL for comm devices
         );

    if (htoCOM == INVALID_HANDLE_VALUE)
      return (GetLastError());
    else
    {
        if(!SetCommTimeouts(htoCOM, &ct_ob)){
            printf ("\n\t ERROR :  1");
        }
        //GetCommState(htoCOM,&dcb_ob);
        if(!SetCommState(htoCOM, &dcb_ob)){
            printf ("\n\t ERROR :   2");
        }


Read function

C#
while(ln!=0)
    {

        Sleep(10);
        bResult= ReadFile( htoCOM,&tm,1,&ln,NULL );
    if (!bResult)
    { printf("READ ERROR");}


     printf("%c",tm);
}


but it reads only 30 -31 bytes.?
Posted
Comments
CPallini 11-Nov-11 9:11am    
Did you get any error (you didn't specify)?
You are updating the DCB parameters without first retrieving its state, why?
BTW why did you hardcoded DCB struct size?
01.mandar 11-Nov-11 10:10am    
hi
it fails at SetCommState(htoCOM, &dcb_ob)
i tried different combination in dcb_ob setting but it still fails

i have hardcoded DCB struct size because in some forum they have said problem is because of 64bit machine so they suggested me to give value in multiples of 8


Richard MacCutchan 11-Nov-11 11:51am    
You should use GetLastError() to find the reason why it failed.
CPallini 11-Nov-11 15:53pm    
As already suggested by Richard, whenever an API function fails you must call GetLastError() to get useful info about.
You should really call GetCommState to retrieve current settings before updating the DCB struct, please see the following MSDN code sample:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa363201(v=vs.85).aspx
As about the DCB struct size, YOUR PROGRAM is passing it to the API function, hence YOUR PROGRAM knows its size (that is sizeof(DCB) ).
01.mandar 12-Nov-11 2:05am    
Thanks all of you
when i did GetCommState() DCB was populated with garbage values
baudrate as 1200 and binary to TRUE rest all was set to 0

i dont know why it is necessary to do GetCommState() before SetCommState() why cant we directly set values.

if some one pulled serial cable when the code is running GetLastError() is 995.
how can i close all handles safely because i have done same application in c#.net and it gives "Safe handle has been closed" and crashed

You cannot read more than written to COM; can you check what happens on the other end of RS-232 cable?

—SA
 
Share this answer
 
Quote:
it is necessary to do GetCommState() before SetCommState()

reference code: http://msdn.microsoft.com/en-us/library/windows/desktop/aa363201(v=vs.85).aspx[^]
 
Share this answer
 
This may or may not answer your question, but is more a piece of programming advice. Instead of using the code style you have, I suggest:
C++
DCB dcb_ob = {sizeof(DCB)}; // [Note change here]
COMMTIMEOUTS ct_ob = {0};   // [Note change here]

//dcb_ob.DCBlength = sizeof(DCB);  // [redundant]
dcb_ob...
...
ct_ob.WriteTotalTimeoutConstant = 1000;

which will properly initialize each of the structures to the value(s) you provide, with trailing 'zeroes'. This means for example that (following the initial size field in DCB), everything is zero and you don't need to set zero/false/NULL values, and your code still works in subsequent operating system versions when Microsoft changes the size of the structure.

For example, suppose that in Windows XP, Microsoft introduces a structure:
C++
struct blorg {
     int BlorgSize;
     int BlorgData;
};

and you write code using it in XP using your style above. Then in Windows Vista they change the struct to:
C++
struct blorg {
     int BlorgSize;
     int BlorgData;
     void* pOtherBlorg;
};

and then in Windows 7, they change it again:
C++
struct blorg {
     int BlorgSize;
     int BlorgData;
     void* pOtherBlorg;
     void* pOtherBlorgEx;
     BOOL bImportantFlag;
};

When compiled in Vista (or later), my code suggestion works fine, but yours just broke (uninitialized pointer). Even if you fix for Vista, yours breaks again in 7.

[I refuse to acknowledge "8" (yuck) on principle.]
 
Share this answer
 
Comments
Richard C Bishop 12-Apr-13 12:34pm    
Did you realize this thread is a year and a half old?
H.Brydon 12-Apr-13 13:08pm    
Ha ha - I do now. It was in today's list of issues. Thanks for the const pointer exception.
Richard C Bishop 12-Apr-13 13:38pm    
It happens, I did the same thing once or twice before.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900