Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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
 
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
 
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 11-Nov-11 3:40am
01.mandar1.2K
Comments
CPallini at 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 at 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 at 11-Nov-11 11:51am
   
You should use GetLastError() to find the reason why it failed.
CPallini at 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 at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You cannot read more than written to COM; can you check what happens on the other end of RS-232 cable?
 
—SA
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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[^]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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:
    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:
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:
struct blorg {
     int BlorgSize;
     int BlorgData;
     void* pOtherBlorg;
};
and then in Windows 7, they change it again:
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.]
  Permalink  
Comments
richcb at 12-Apr-13 12:34pm
   
Did you realize this thread is a year and a half old?
H.Brydon at 12-Apr-13 13:08pm
   
Ha ha - I do now. It was in today's list of issues. Thanks for the const pointer exception.
richcb at 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)

  Print Answers RSS
0 OriginalGriff 7,853
1 Sergey Alexandrovich Kryukov 7,107
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,790


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 12 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100