Summary:
The DCB structure defines the control setting for a serial communications device.
// Able to handle C++ BitFlags.
[StructLayout(LayoutKind.Sequential)]
internal struct Dcb
{
internal uint DCBLength;
internal uint BaudRate;
private BitVector32 Flags;
//what about this struct members?
public uint wReserved; // not currently used
public uint XonLim; // transmit XON threshold
public uint XoffLim; // transmit XOFF threshold
internal byte ByteSize;
internal Parity Parity;
internal StopBits StopBits;
//and this?
public char XonChar; // Tx and Rx XON character
public char XoffChar; // Tx and Rx XOFF character
public char ErrorChar; // error replacement character
public char EofChar; // end of input character
public char EvtChar; // received event character
public uint wReserved1; // reserved; do not use
private static readonly int fBinary;
private static readonly int fParity;
private static readonly int fOutxCtsFlow;
private static readonly int fOutxDsrFlow;
private static readonly BitVector32.Section fDtrControl;
private static readonly int fDsrSensitivity;
private static readonly int fTXContinueOnXoff;
private static readonly int fOutX;
private static readonly int fInX;
private static readonly int fErrorChar;
private static readonly int fNull;
private static readonly BitVector32.Section fRtsControl;
private static readonly int fAbortOnError;
static Dcb()
{
// Create Boolean Mask
int previousMask;
fBinary = BitVector32.CreateMask();
fParity = BitVector32.CreateMask(fBinary);
fOutxCtsFlow = BitVector32.CreateMask(fParity);
fOutxDsrFlow = BitVector32.CreateMask(fOutxCtsFlow);
previousMask = BitVector32.CreateMask(fOutxDsrFlow);
previousMask = BitVector32.CreateMask(previousMask);
fDsrSensitivity = BitVector32.CreateMask(previousMask);
fTXContinueOnXoff = BitVector32.CreateMask(fDsrSensitivity);
fOutX = BitVector32.CreateMask(fTXContinueOnXoff);
fInX = BitVector32.CreateMask(fOutX);
fErrorChar = BitVector32.CreateMask(fInX);
fNull = BitVector32.CreateMask(fErrorChar);
previousMask = BitVector32.CreateMask(fNull);
previousMask = BitVector32.CreateMask(previousMask);
fAbortOnError = BitVector32.CreateMask(previousMask);
// Create section Mask
BitVector32.Section previousSection;
previousSection = BitVector32.CreateSection(1);
previousSection = BitVector32.CreateSection(1, previousSection);
previousSection = BitVector32.CreateSection(1, previousSection);
previousSection = BitVector32.CreateSection(1, previousSection);
fDtrControl = BitVector32.CreateSection(2, previousSection);
previousSection = BitVector32.CreateSection(1, fDtrControl);
previousSection = BitVector32.CreateSection(1, previousSection);
previousSection = BitVector32.CreateSection(1, previousSection);
previousSection = BitVector32.CreateSection(1, previousSection);
previousSection = BitVector32.CreateSection(1, previousSection);
previousSection = BitVector32.CreateSection(1, previousSection);
fRtsControl = BitVector32.CreateSection(3, previousSection);
previousSection = BitVector32.CreateSection(1, fRtsControl);
}
public bool Binary
{
get { return Flags[fBinary]; }
set { Flags[fBinary] = value; }
}
public bool CheckParity
{
get { return Flags[fParity]; }
set { Flags[fParity] = value; }
}
public bool OutxCtsFlow
{
get { return Flags[fOutxCtsFlow]; }
set { Flags[fOutxCtsFlow] = value; }
}
public bool OutxDsrFlow
{
get { return Flags[fOutxDsrFlow]; }
set { Flags[fOutxDsrFlow] = value; }
}
public DtrControl DtrControl
{
get { return (DtrControl)Flags[fDtrControl]; }
set { Flags[fDtrControl] = (int)value; }
}
public bool DsrSensitivity
{
get { return Flags[fDsrSensitivity]; }
set { Flags[fDsrSensitivity] = value; }
}
public bool TxContinueOnXoff
{
get { return Flags[fTXContinueOnXoff]; }
set { Flags[fTXContinueOnXoff] = value; }
}
public bool OutX
{
get { return Flags[fOutX]; }
set { Flags[fOutX] = value; }
}
public bool InX
{
get { return Flags[fInX]; }
set { Flags[fInX] = value; }
}
public bool ReplaceErrorChar
{
get { return Flags[fErrorChar]; }
set { Flags[fErrorChar] = value; }
}
public bool Null
{
get { return Flags[fNull]; }
set { Flags[fNull] = value; }
}
public RtsControl RtsControl
{
get { return (RtsControl)Flags[fRtsControl]; }
set { Flags[fRtsControl] = (int)value; }
}
public bool AbortOnError
{
get { return Flags[fAbortOnError]; }
set { Flags[fAbortOnError] = value; }
}
}
<StructLayout(LayoutKind.Sequential)> Public Structure Dcb
Friend DCBLength As System.UInt32
Friend BaudRate As System.UInt32
Private Flags As System.Collections.Specialized.BitVector32
Public wReserved As System.UInt32 ' not currently used
Public XonLim As System.UInt32 ' transmit XON threshold
Public XoffLim As System.UInt32 ' transmit XOFF threshold
Friend ByteSize As Byte
Friend Parity As Parity
Friend StopBits As StopBits
Public XonChar As Char ' Tx and Rx XON character
Public XoffChar As Char ' Tx and Rx XOFF character
Public ErrorChar As Char ' error replacement character
Public EofChar As Char ' end of input character
Public EvtChar As Char ' received event charater
Public wReserved1 As System.UInt32 ' reserved; do not use
Private Shared ReadOnly fBinary As Integer
Private Shared ReadOnly fParity As Integer
Private Shared ReadOnly fOutxCtsFlow As Integer
Private Shared ReadOnly fOutxDsrFlow As Integer
Private Shared ReadOnly fDtrControl As System.Collections.Specialized.BitVector32.Section
Private Shared ReadOnly fDsrSensitivity As Integer
Private Shared ReadOnly fTXContinueOnXoff As Integer
Private Shared ReadOnly fOutX As Integer
Private Shared ReadOnly fInX As Integer
Private Shared ReadOnly fErrorChar As Integer
Private Shared ReadOnly fNull As Integer
Private Shared ReadOnly fRtsControl As System.Collections.Specialized.BitVector32.Section
Private Shared ReadOnly fAbortOnError As Integer
Shared Sub New()
' Create Boolean Mask
Dim previousMask As Integer
fBinary = System.Collections.Specialized.BitVector32.CreateMask()
fParity = System.Collections.Specialized.BitVector32.CreateMask(fBinary)
fOutxCtsFlow = System.Collections.Specialized.BitVector32.CreateMask(fParity)
fOutxDsrFlow = System.Collections.Specialized.BitVector32.CreateMask(fOutxCtsFlow)
previousMask = System.Collections.Specialized.BitVector32.CreateMask(fOutxDsrFlow)
previousMask = System.Collections.Specialized.BitVector32.CreateMask(previousMask)
fDsrSensitivity = System.Collections.Specialized.BitVector32.CreateMask(previousMask)
fTXContinueOnXoff = System.Collections.Specialized.BitVector32.CreateMask(fDsrSensitivity)
fOutX = System.Collections.Specialized.BitVector32.CreateMask(fTXContinueOnXoff)
fInX = System.Collections.Specialized.BitVector32.CreateMask(fOutX)
fErrorChar = System.Collections.Specialized.BitVector32.CreateMask(fInX)
fNull = System.Collections.Specialized.BitVector32.CreateMask(fErrorChar)
previousMask = System.Collections.Specialized.BitVector32.CreateMask(fNull)
previousMask = System.Collections.Specialized.BitVector32.CreateMask(previousMask)
fAbortOnError = System.Collections.Specialized.BitVector32.CreateMask(previousMask)
' Create section Mask
Dim previousSection As System.Collections.Specialized.BitVector32.Section
previousSection = System.Collections.Specialized.BitVector32.CreateSection(1)
previousSection = System.Collections.Specialized.BitVector32.CreateSection(1, previousSection)
previousSection = System.Collections.Specialized.BitVector32.CreateSection(1, previousSection)
previousSection = System.Collections.Specialized.BitVector32.CreateSection(1, previousSection)
fDtrControl = System.Collections.Specialized.BitVector32.CreateSection(2, previousSection)
previousSection = System.Collections.Specialized.BitVector32.CreateSection(1, fDtrControl)
previousSection = System.Collections.Specialized.BitVector32.CreateSection(1, previousSection)
previousSection = System.Collections.Specialized.BitVector32.CreateSection(1, previousSection)
previousSection = System.Collections.Specialized.BitVector32.CreateSection(1, previousSection)
previousSection = System.Collections.Specialized.BitVector32.CreateSection(1, previousSection)
previousSection = System.Collections.Specialized.BitVector32.CreateSection(1, previousSection)
fRtsControl = System.Collections.Specialized.BitVector32.CreateSection(3, previousSection)
previousSection = System.Collections.Specialized.BitVector32.CreateSection(1, fRtsControl)
End Sub ' New
Public Property Binary() As Boolean
Get
Return Flags(fBinary)
End Get
Set(ByVal Value As Boolean)
Flags(fBinary) = Value
End Set
End Property ' Binary
Public Property CheckParity() As Boolean
Get
Return Flags(fParity)
End Get
Set(ByVal Value As Boolean)
Flags(fParity) = Value
End Set
End Property ' CheckParity
Public Property OutxCtsFlow() As Boolean
Get
Return Flags(fOutxCtsFlow)
End Get
Set(ByVal Value As Boolean)
Flags(fOutxDsrFlow) = Value
End Set
End Property ' OutxCtsFlow
Public Property OutxDsrFlow() As Boolean
Get
Return Flags(fOutxDsrFlow)
End Get
Set(ByVal Value As Boolean)
Flags(fOutxDsrFlow) = Value
End Set
End Property ' OutxDsrFlow
Public Property DtrControl() As DtrControl
Get
Return CType(Flags(fDtrControl), DtrControl)
End Get
Set(ByVal Value As DtrControl)
Flags(fDtrControl) = CType(Value, Integer)
End Set
End Property ' DtrControl
Public Property DsrSensitivity() As Boolean
Get
Return Flags(fDsrSensitivity)
End Get
Set(ByVal Value As Boolean)
Flags(fDsrSensitivity) = Value
End Set
End Property ' DsrSensitivity
Public Property TxContinueOnXoff() As Boolean
Get
Return Flags(fTXContinueOnXoff)
End Get
Set(ByVal Value As Boolean)
Flags(fTXContinueOnXoff) = Value
End Set
End Property ' TxContinueOnXoff
Public Property OutX() As Boolean
Get
Return Flags(fOutX)
End Get
Set(ByVal Value As Boolean)
Flags(fOutX) = Value
End Set
End Property ' OutX
Public Property InX() As Boolean
Get
Return Flags(fInX)
End Get
Set(ByVal Value As Boolean)
Flags(fInX) = Value
End Set
End Property ' InX
Public Property ReplaceErrorChar() As Boolean
Get
Return Flags(fErrorChar)
End Get
Set(ByVal Value As Boolean)
Flags(fErrorChar) = Value
End Set
End Property ' ReplaceErrorChar
Public Property Null() As Boolean
Get
Return Flags(fNull)
End Get
Set(ByVal Value As Boolean)
Flags(fNull) = Value
End Set
End Property ' Null
Public Property RtsControl() As RtsControl
Get
Return CType(Flags(fRtsControl), RtsControl)
End Get
Set(ByVal Value As RtsControl)
Flags(fRtsControl) = CType(Value, Integer)
End Set
End Property ' RtsControl
Public Property AbortOnError() As Boolean
Get
Return Flags(fAbortOnError)
End Get
Set(ByVal Value As Boolean)
Flags(fAbortOnError) = Value
End Set
End Property ' AbortOnError
End Structure ' Dcb
None.
Special thanks to Jay's Outlook MVP from Visual C# Community for the guidance.
fDtrControl = BitVector32.CreateSection(2, previousSection);
fRtsControl = BitVector32.CreateSection(3, previousSection);
You will notice that fDtrControl expects 2 and fRtsControl expect 3. Why?
The reason is that the highest value within enum DtrControl is 2, and highest valye within enum RtsControl is 3.
This helps you to fix the bitflag issue in c to c#. I had tested it and it works greatly.
To use this code,
Example:
C#
Dcb myDcb = new Dcb();
myDcb.Binary = true;
myDcb.DtrControl = DtrControl.Disable; // DtrControl is an enum