(Solved) Change DCB Fields From SerialPort Instance C# - CodeProject
(Solved) Change DCB Fields From SerialPort Instance C# - CodeProject
I am trying to emulate communication between an existing piece of software and hardware, using Advanced Serial Port Monitor I
was able to come up with this information about the communication.
First I tried using the built in SerialPort class on it's own, but it lacks the versatility to be able to complete such a task, I then tried
using System.Reflection to set the DCB fields, but whenever I come to a field that begins with a lowercase f, such as "fParity" or
"fDtrControl", it gives me a NullReferenceException.
Is there any other way that I can modify these values to match the above?
This is the extension method I wrote enabling the modification of DCB fields
Hide Expand Copy Code
try
{
// Get the base stream and its type which is System.IO.Ports.SerialStream
object baseStream = port.BaseStream;
Type baseStreamType = baseStream.GetType();
https://www.codeproject.com/Questions/856414/Change-DCB-fields-from-SerialPort-instance-Csharp 1/6
9/11/2019 [Solved] Change DCB fields from SerialPort instance C# - CodeProject
EDIT
Upon further investigation it appears that the fields prefixed with f are not in the DCB structure used by SerialPort for some odd
reason. However there is a flags field, but it is of type UInt32, so I'm unsure of how to use it, if anyone has further information on
how to use this field, please submit it as an answer or comment.
Add a Solution
1 solution
Alright I solved my problem, I'll post the code and explanation here if anyone is interested.
The DCB struct requires a C++ class known as a bitfield, which C# does not have, so instead they have a field called "Flags" stored
as a UInt32. They have their own melarchy set up there as to how it's stored and read from, but they did put a method in the
SerialStream called SetDcbFlag which accepts two ints. After digging through the .net source a bit, I managed to
come up with a set of constants equating to the original DCB Fields and their new int code, I haven't yet made a list of values for
these flags, but those can be easily found in the DCB Documentation. I used system.reflection to gain access to the method as it
was an internal method only to be used by internal .NET source.
So here it is, the code, it's an extension class for the SerialPort class which is shipped stock with .NET 2.0+. My extension
class adds three methods, SetField(string name, object value) which will set any of the fields that aren't
prefixed with "f", SetFlag(int Flag, int Value) which will take care of those fields prefixed with "f" (I'll provide a list
of constants for use with the Flag parameter), and finally UpdateComm() this will update the serial connection once you have
https://www.codeproject.com/Questions/856414/Change-DCB-fields-from-SerialPort-instance-Csharp 2/6
9/11/2019 [Solved] Change DCB fields from SerialPort instance C# - CodeProject
changed all of your values, it was originally part of the SetField method, but it takes slightly longer to complete things if it's
calling that every single time during initialization.
NOTE:
The serial port must be opened before using any of these methods!
Usage:
Hide Copy Code
Constants:
Hide Copy Code
internal const int FBINARY = 0;
internal const int FPARITY = 1;
internal const int FOUTXCTSFLOW = 2;
internal const int FOUTXDSRFLOW = 3;
internal const int FDTRCONTROL = 4;
internal const int FDSRSENSITIVITY = 6;
internal const int FTXCONTINUEONXOFF = 7;
internal const int FOUTX = 8;
internal const int FINX = 9;
internal const int FERRORCHAR = 10;
internal const int FNULL = 11;
internal const int FRTSCONTROL = 12;
internal const int FABORTONOERROR = 14;
internal const int FDUMMY2 = 15;
try
{
https://www.codeproject.com/Questions/856414/Change-DCB-fields-from-SerialPort-instance-Csharp 3/6
9/11/2019 [Solved] Change DCB fields from SerialPort instance C# - CodeProject
Comments
https://www.codeproject.com/Questions/856414/Change-DCB-fields-from-SerialPort-instance-Csharp 4/6
9/11/2019 [Solved] Change DCB fields from SerialPort instance C# - CodeProject
Preview
…
2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem.
Insults are not welcome.
4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the
next question.
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)
https://www.codeproject.com/Questions/856414/Change-DCB-fields-from-SerialPort-instance-Csharp 5/6
9/11/2019 [Solved] Change DCB fields from SerialPort instance C# - CodeProject
CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100
https://www.codeproject.com/Questions/856414/Change-DCB-fields-from-SerialPort-instance-Csharp 6/6