Menu

#63 DCB.Flag bits not initialized properly

closed-fixed
nobody
Comm (38)
5
2007-03-24
2006-05-15
gregd29
No

If you run a program that uses the serial port and
leaves the DCB.Flags set in a particular way (like
fNull set), and then run a program using APro, the
DCB.Flags values are not initialized. This caused our
program to fail by not receiving any null characters.
We are using a binary protocol which requires nulls to
be sent through. The original 4.06 code looks like an
attempt was made to set them but it doesn't work. Can
anyone interested, please review the modified code
from the AwUser.pas shown below and give me input on
possible problems that it may cause. So far, I dont
see any problems with fixing the issue like this:

function TApdBaseDispatcher.SetLine(
Baud : LongInt;
Parity : Cardinal;
DataBits : TDatabits;
StopBits : TStopbits) : Integer;
var
NewBaudRate : DWORD;
NewParity : Cardinal;
NewByteSize : TDatabits;
NewStopBits : Byte;
NewFlags : LongInt; // GAD 12-May-06 Modified to
properly clear dcb_Null flag etc...
{-Set or change the line parameters}
begin
Result := ecOK;
EnterCriticalSection(DataSection);
try
{Get current DCB parameters}
GetComState(DCB);

{Set critical default DCB options}
NewFlags := DCB.Flags; // GAD 12-May-06 Modified to
properly clear dcb_Null flag etc...
NewFlags := NewFlags or dcb_Binary;
NewFlags := NewFlags and not dcb_Parity;
NewFlags := NewFlags and not dcb_DsrSensitivity;
NewFlags := NewFlags or dcb_TxContinueOnXoff;
NewFlags := NewFlags and not dcb_Null; // GAD 12-May-
06 Modified to properly clear dcb_Null flag etc...

{Validate stopbit range}
if StopBits <> DontChangeStopBits then
if StopBits < 1 then
StopBits := 1
else if StopBits > 2 then
StopBits := 2;

{Determine new line parameters}
if Baud <> DontChangeBaud then begin
NewBaudRate := Baud;
end else
NewBaudRate := DCB.BaudRate;

if Parity <> DontChangeParity then
NewParity := Parity
else
NewParity := DCB.Parity;

NewStopBits := DCB.StopBits;

if DataBits <> DontChangeDataBits then
begin
NewByteSize := DataBits;
if (DataBits = 5) then
NewStopBits := One5StopBits;
end else
NewByteSize := DCB.ByteSize;

if StopBits <> DontChangeStopBits then begin
NewStopBits := StopBitArray[StopBits];
if (NewByteSize = 5) then
NewStopBits := One5StopBits;
end;
finally
LeaveCriticalSection(DataSection);
end;

if ((DCB.BaudRate = NewBaudRate) and
(DCB.Parity = NewParity) and
(DCB.ByteSize = NewByteSize) and
(DCB.StopBits = NewStopBits) and
(DCB.Flags = NewFlags)) then // GAD 12-May-06 Modified
to properly clear dcb_Null flag etc...
Exit;

{ wait for the chars to be transmitted, don't want to
change line }
{ settings while chars are pending }
WaitTxSent;

EnterCriticalSection(DataSection);
try
{Get current DCB parameters}
GetComState(DCB);

{Change the parameters}
DCB.BaudRate := NewBaudRate;
DCB.Parity := NewParity;
DCB.ByteSize := NewByteSize;
DCB.StopBits := NewStopBits;
DCB.Flags := NewFlags; // GAD 12-May-06 Modified to
properly clear dcb_Null flag etc...

{Set line parameters}
Result := SetCommStateFix(DCB);
finally
LeaveCriticalSection(DataSection);
end;
end;

Discussion

  • Stephen Boyd

    Stephen Boyd - 2007-01-30

    Logged In: YES
    user_id=84969
    Originator: NO

    Aren't you making an assumption here that everyone would want to see the NULL characters? Might it not be better to provide an optional parameter on the SetLine call to allow / disallow NULL characters. This parameter would default to allow NULLs in order to not break existing code.

     
  • Stephen Boyd

    Stephen Boyd - 2007-03-24
    • status: open --> closed
     
  • Stephen Boyd

    Stephen Boyd - 2007-03-24

    Logged In: YES
    user_id=84969
    Originator: NO

    I have posted the suggested changes to CVS. I can see no harm in doing this, although it would be nice to be able to choose the setting for the fNull flag. But, since the other values of the Flags word are hard coded here I guess this isn't really any different. Something that existing users might have to be aware of though if they specifically set this option on and expect it to stay on.

    Thank you for your efforts for tracking down and fixing this problem.

     
  • Stephen Boyd

    Stephen Boyd - 2007-03-24
    • status: closed --> closed-fixed
     
  • Stephen Boyd

    Stephen Boyd - 2007-03-24

    Logged In: YES
    user_id=84969
    Originator: NO

    I have posted the suggested changes to CVS. I can see no harm in doing this, although it would be nice to be able to choose the setting for the fNull flag. But, since the other values of the Flags word are hard coded here I guess this isn't really any different. Something that existing users might have to be aware of though if they specifically set this option on and expect it to stay on.

    Thank you for your efforts for tracking down and fixing this problem.

     

Log in to post a comment.