Serial DCB Win32 Structure

  • Thread starter Thread starter Rafael Pivato
  • Start date Start date
R

Rafael Pivato

I need help declaring this structure in VB.
Particularly the bit flags, like fOutxCtsFlow.


typedef struct _DCB {
DWORD DCBlength;
DWORD BaudRate;
DWORD fBinary:1;
DWORD fParity:1;
DWORD fOutxCtsFlow:1;
DWORD fOutxDsrFlow:1;
DWORD fDtrControl:2;
DWORD fDsrSensitivity:1;
DWORD fTXContinueOnXoff:1;
DWORD fOutX: 1;
DWORD fInX: 1;
DWORD fErrorChar:1;
DWORD fNull:1;
DWORD fRtsControl:2;
DWORD fAbortOnError:1;
DWORD fDummy2:17;
WORD wReserved;
WORD XonLim;
WORD XoffLim;
BYTE ByteSize;
BYTE Parity;
BYTE StopBits;
char XonChar;
char XoffChar;
char ErrorChar;
char EofChar;
char EvtChar;
WORD wReserved1;
} DCB;
 
Rafael,

Structure DCB
Public DCBlength, BaudRate, Flags As Integer
Public wReserved, XonLim, XoffLim As Short
Public ByteSize, Parity, StopBits, XonChar As Byte
Public XoffChar, ErrorChar, EofChar, EvtChar As Byte
Public wReserved1 As Short
End Structure

You'll have to use the bit operators to retrieve the separate flags in
the Flags member (or you could define an Enum with the corresponding
bit values).



Mattias
 
Hi,

Your DCB is not correct. It looks like:

typedef struct _DCB {
DWORD DCBlength;
DWORD BaudRate;
DWORD Flags;
WORD wReserved;
WORD XonLim;
WORD XoffLim;
BYTE ByteSize;
BYTE Parity;
BYTE StopBits;
char XonChar;
char XoffChar;
char ErrorChar;
char EofChar;
char EvtChar;
WORD wReserved1;
} DCB;

Flags is bit-mapped, and contains bit-flags that control things like parity
and flow control.

Dick

--
Richard Grier (Microsoft Visual Basic MVP)

See www.hardandsoftware.net for contact information.

Author of Visual Basic Programmer's Guide to Serial Communications, 3rd
Edition ISBN 1-890422-27-4 (391 pages) published February 2002.
 
I thought that there was an marshall attribute (or something)
that automates the bit flags access.

But, with your advices I think it's better to implement
a wrapper class to access this structure in a easy manner
for the client.

Thanks
 
Rafael,
When I implemented the DCB structure in VB.NET I defined Flags as a Private
BitVector32. Then used Property procedures that extracted the individual
values, using the methods of BitVector32.

Public enum RtsControl As Byte
Disable = 0
Enable = 1
Handshake = 2
Toggle = 3
End Enum

Public Structure DBC
...
Private Flags As BitVector32
...

Private Shared Readonly m_fBinary As Integer
Private Shared Readonly m_Parity As Integer
...
Private Shared Readonly m_fRtsControl As BitVector32.Section
...

Shared Sub New()
' create boolean masks
m_fBinary = BitVector32.CreateMask()
...
m_fParity = BitVector32.CreateMask(m_fBinary)
...
' create section masks
Dim previousSection As BitVector32.Section
previousSection = BitVector32.CreateSection(1)
...
m_fRtsControl = BitVector32.CreateSection(3,
previousSection)
...
End Sub

Public Property Binary As Boolean
Get
Return Flags.Item(m_fBinary)
End Get
Set(ByVal value As Boolean)
Flags.Item(m_fBinary) = value
End Set
End Property

...

Public Property RtsControl As RtsControl
Get
Return CType(Flags.Item(m_fRtsControl), RtsControl)
End Get
Set(ByVal value As RtsControl)
Flags.Item(m_fRtsControl) = value
End Set
End Property

...

End Structure

BitVector32 is marshaled as an Integer (DWORD) so you can use them
interchangeably.

I made Flags Private as its an implementation detail.

The property procedures expose the flags as the respective types, For
example RtsControl & DtrControl as enums with valid values, others as
Boolean. I had to change a couple names as there is a field & a flag
(ErrorChar).

For the BitVector32 masks I had to make two passes, once for the boolean
values, then a second one for the section (enum) values.

Using the BitVector32 class simplified my property procedures at the
"expense" of a little extra setup (the shared constructor).

I have not posted my class on got dot net yet, as I am still working on
making the WaitEvent async.

Hope this helps
Jay
 
very, very usefull....
Thanks...


Jay B. Harlow said:
Rafael,
When I implemented the DCB structure in VB.NET I defined Flags as a Private
BitVector32. Then used Property procedures that extracted the individual
values, using the methods of BitVector32.

Public enum RtsControl As Byte
Disable = 0
Enable = 1
Handshake = 2
Toggle = 3
End Enum

Public Structure DBC
...
Private Flags As BitVector32
...

Private Shared Readonly m_fBinary As Integer
Private Shared Readonly m_Parity As Integer
...
Private Shared Readonly m_fRtsControl As BitVector32.Section
...

Shared Sub New()
' create boolean masks
m_fBinary = BitVector32.CreateMask()
...
m_fParity = BitVector32.CreateMask(m_fBinary)
...
' create section masks
Dim previousSection As BitVector32.Section
previousSection = BitVector32.CreateSection(1)
...
m_fRtsControl = BitVector32.CreateSection(3,
previousSection)
...
End Sub

Public Property Binary As Boolean
Get
Return Flags.Item(m_fBinary)
End Get
Set(ByVal value As Boolean)
Flags.Item(m_fBinary) = value
End Set
End Property

...

Public Property RtsControl As RtsControl
Get
Return CType(Flags.Item(m_fRtsControl), RtsControl)
End Get
Set(ByVal value As RtsControl)
Flags.Item(m_fRtsControl) = value
End Set
End Property

...

End Structure

BitVector32 is marshaled as an Integer (DWORD) so you can use them
interchangeably.

I made Flags Private as its an implementation detail.

The property procedures expose the flags as the respective types, For
example RtsControl & DtrControl as enums with valid values, others as
Boolean. I had to change a couple names as there is a field & a flag
(ErrorChar).

For the BitVector32 masks I had to make two passes, once for the boolean
values, then a second one for the section (enum) values.

Using the BitVector32 class simplified my property procedures at the
"expense" of a little extra setup (the shared constructor).

I have not posted my class on got dot net yet, as I am still working on
making the WaitEvent async.

Hope this helps
Jay
 
Back
Top