Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

DCB (Structures)
 
.

Summary:

The DCB structure defines the control setting for a serial communications device.

C# Definition:

// Able to handle C++ BitFlags.

using System.Collections.Specialized;
using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Sequential)]
internal struct Dcb
{
    internal uint DCBLength;
    internal uint BaudRate;
    private BitVector32 Flags;

    private ushort wReserved;        // not currently used
    internal ushort XonLim;           // transmit XON threshold
    internal ushort XoffLim;          // transmit XOFF threshold             

    internal byte ByteSize;
    internal Parity Parity;
    internal StopBits StopBits;

    internal sbyte XonChar;          // Tx and Rx XON character
    internal sbyte XoffChar;         // Tx and Rx XOFF character
    internal sbyte ErrorChar;        // error replacement character
    internal sbyte EofChar;          // end of input character
    internal sbyte EvtChar;          // received event character
    private ushort wReserved1;       // reserved; do not use     
    using System.Collections.Specialized;
    using System.Runtime.InteropServices;

    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;

[StructLayout(LayoutKind.Sequential)]

    internal struct Dcb

{

    internal uint DCBLength;
    internal uint BaudRate;
    private BitVector32 Flags;

    //I've missed some members...
    private uint wReserved;        // not currently used
    internal uint XonLim;           // transmit XON threshold
    internal uint XoffLim;          // transmit XOFF threshold             

    internal byte ByteSize;
    internal Parity Parity;
    internal StopBits StopBits;

    //...and some more
    internal char XonChar;          // Tx and Rx XON character
    internal char XoffChar;         // Tx and Rx XOFF character
    internal char ErrorChar;        // error replacement character
    internal char EofChar;          // end of input character
    internal char EvtChar;          // received event character
    private uint wReserved1;       // reserved; do not use     

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

        // 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
    static Dcb()
    {
        get { return Flags[fBinary]; }
        set { Flags[fBinary] = value; }
    }
        // 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);

    public bool CheckParity
    {
        get { return Flags[fParity]; }
        set { Flags[fParity] = value; }
        // 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 OutxCtsFlow
    public bool Binary
    {
        get { return Flags[fOutxCtsFlow]; }
        set { Flags[fOutxCtsFlow] = value; }
        get { return Flags[fBinary]; }
        set { Flags[fBinary] = value; }
    }

    public bool OutxDsrFlow
    public bool CheckParity
    {
        get { return Flags[fOutxDsrFlow]; }
        set { Flags[fOutxDsrFlow]  = value; }
        get { return Flags[fParity]; }
        set { Flags[fParity] = value; }
    }

    public DtrControl DtrControl
    public bool OutxCtsFlow
    {
        get { return (DtrControl)Flags[fDtrControl]; }
        set { Flags[fDtrControl] = (int)value; }
        get { return Flags[fOutxCtsFlow]; }
        set { Flags[fOutxCtsFlow] = value; }
    }

    public bool DsrSensitivity
    public bool OutxDsrFlow
    {
        get { return Flags[fDsrSensitivity]; }
        set { Flags[fDsrSensitivity] = value; }
        get { return Flags[fOutxDsrFlow]; }
        set { Flags[fOutxDsrFlow]  = value; }
    }

    public bool TxContinueOnXoff
    public DtrControl DtrControl
    {
        get { return Flags[fTXContinueOnXoff]; }
        set { Flags[fTXContinueOnXoff] = value; }
        get { return (DtrControl)Flags[fDtrControl]; }
        set { Flags[fDtrControl] = (int)value; }
    }

    public bool OutX
    public bool DsrSensitivity
    {
        get { return Flags[fOutX]; }
        set { Flags[fOutX] = value; }
        get { return Flags[fDsrSensitivity]; }
        set { Flags[fDsrSensitivity] = value; }
    }

    public bool InX
    public bool TxContinueOnXoff
    {
        get { return Flags[fInX]; }
        set { Flags[fInX] = value; }
        get { return Flags[fTXContinueOnXoff]; }
        set { Flags[fTXContinueOnXoff] = value; }
    }

    public bool ReplaceErrorChar
    public bool OutX
    {
        get { return Flags[fErrorChar]; }
        set { Flags[fErrorChar] = value; }
        get { return Flags[fOutX]; }
        set { Flags[fOutX] = value; }
    }

    public bool Null
    public bool InX
    {
        get { return Flags[fNull]; }
        set { Flags[fNull] = value; }
        get { return Flags[fInX]; }
        set { Flags[fInX] = value; }
    }

    public RtsControl RtsControl
    public bool ReplaceErrorChar
    {
        get { return (RtsControl)Flags[fRtsControl]; }
        set { Flags[fRtsControl]  = (int)value; }
        get { return Flags[fErrorChar]; }
        set { Flags[fErrorChar] = value; }
    }

    public bool AbortOnError
    public bool Null
    {
        get { return Flags[fAbortOnError]; }
        set { Flags[fAbortOnError] = value; }
        get { return Flags[fNull]; }
        set { Flags[fNull] = value; }
    }
}

User-Defined Field Types:

public enum Parity : byte
{
    None = 0,
    Odd = 1,
    Even = 2,
    Mark = 3,
    Space = 4,
}
    public RtsControl RtsControl
    {
        get { return (RtsControl)Flags[fRtsControl]; }
        set { Flags[fRtsControl]  = (int)value; }
    }

public enum StopBits : byte
{
    One = 0,
    OnePointFive = 1,
    Two = 2
}
    public bool AbortOnError
    {
        get { return Flags[fAbortOnError]; }
        set { Flags[fAbortOnError] = value; }
    }

}

See also DtrControl and RtsControl

VB Definition:

Private Type DCB
    DCBlength As Long
    BaudRate As Long
    fBitFields As Long 'See Comments in Win32API.Txt
    wReserved As Integer
    XonLim As Integer
    XoffLim As Integer
    ByteSize As Byte
    Parity As Byte
    StopBits As Byte
    XonChar As Byte
    XoffChar As Byte
    ErrorChar As Byte
    EofChar As Byte
    EvtChar As Byte
    wReserved1 As Integer 'Reserved; Do Not Use
End Type

VB.Net Definition

Note there is a Microsoft KB Artcile describing how to access serial and parallel ports in VB.NET

http://support.microsoft.com/default.aspx?scid=kb;en-us;823179&Product=vb6

Here is the structure - worked for me:

  Public Structure DCB
    Public DCBlength As Int32
    Public BaudRate As Int32
    Public fBitFields As Int32
    Public wReserved As Int16
    Public XonLim As Int16
    Public XoffLim As Int16
    Public ByteSize As Byte
    Public Parity As Byte
    Public StopBits As Byte
    Public XonChar As Byte
    Public XoffChar As Byte
    Public ErrorChar As Byte
    Public EofChar As Byte
    Public EvtChar As Byte
    Public wReserved1 As Int16 'Reserved; Do Not Use
  End Structure

User-Defined Field Types:

None.

Notes:

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

Documentation
DCB on MSDN

Please edit this page!

Do you have...

  • helpful tips?
  • corrections to the existing content?
  • alternate definitions?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing any supporting types needed.

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions