1
\$\begingroup\$

I've been reviewing some code that I maintain recently, and I came across this wrapper object for the serial port class. I'm trying to understand the advantage of this seemingly redundant object:

Public Class SerialPort_Class
Private WithEvents Serial_Port As IO.Ports.SerialPort

Private SerialPort_ReOpen_Flag As Boolean = True
Private ReadOnly Common_Methods As New Common_Class

Private _BaudRate As Integer = 115200
Private _DataBits As Integer = 8
Private _DiscardNull As Boolean = False
Private _DtrEnable As Boolean = False
Private _Encoding As Text.Encoding = Text.Encoding.ASCII
Private _Handshake As IO.Ports.Handshake = IO.Ports.Handshake.None
Private _NewLine As String = vbLf 'Default
Private _Parity As IO.Ports.Parity = IO.Ports.Parity.None
Private _ParityReplace As Byte = Byte.MinValue 'If the value is set to the null character, parity replacement is disabled
Private _PortName As String = "None" 'Default of Nothing, Prevent Occupying Com Port Until Set
Private _ReadBufferSize As Integer = 4096
Private _ReadTimeout As Integer = 250
Private _ReceivedBytesThreshold As Integer = 1
Private _RtsEnable As Boolean = False
Private _StopBits As IO.Ports.StopBits = IO.Ports.StopBits.One
Private _WriteBufferSize As Integer = 2048
Private _WriteTimeout As Integer = 250

Private Shared ReadOnly Search_Com_Lock_Object As New Object

Public Property BaudRate As Integer
    Get
        Return _BaudRate
    End Get
    Set(value As Integer)
        If value.Equals(_BaudRate) Then Return
        _BaudRate = value
        SerialPort_ReOpen_Flag = True
    End Set
End Property
Public Property DataBits As Integer
    Get
        Return _DataBits
    End Get
    Set(value As Integer)
        If value.Equals(_DataBits) Then Return
        _DataBits = value
        SerialPort_ReOpen_Flag = True
    End Set
End Property
Public Property DiscardNull As Boolean
    Get
        Return _DiscardNull
    End Get
    Set(value As Boolean)
        If value.Equals(_DiscardNull) Then Return
        _DiscardNull = value
        SerialPort_ReOpen_Flag = True
    End Set
End Property
Public Property DtrEnable As Boolean
    Get
        Return _DtrEnable
    End Get
    Set(value As Boolean)
        If value.Equals(_DtrEnable) Then Return
        _DtrEnable = value
        SerialPort_ReOpen_Flag = True
    End Set
End Property
Public Property Encoding As Text.Encoding
    Get
        Return _Encoding
    End Get
    Set(value As Text.Encoding)
        If value.Equals(_Encoding) Then Return
        _Encoding = value
        SerialPort_ReOpen_Flag = True
    End Set
End Property
Public Property Handshake As IO.Ports.Handshake
    Get
        Return _Handshake
    End Get
    Set(value As IO.Ports.Handshake)
        If value.Equals(_Handshake) Then Return
        _Handshake = value
        SerialPort_ReOpen_Flag = True
    End Set
End Property
Public Property NewLine As String
    Get
        Return _NewLine
    End Get
    Set(value As String)
        If value.Equals(_NewLine) Then Return
        _NewLine = value
        SerialPort_ReOpen_Flag = True
    End Set
End Property
Public Property Parity As IO.Ports.Parity
    Get
        Return _Parity
    End Get
    Set(value As IO.Ports.Parity)
        If value.Equals(_Parity) Then Return
        _Parity = value
        SerialPort_ReOpen_Flag = True
    End Set
End Property
Public Property ParityReplace As Byte
    Get
        Return _ParityReplace
    End Get
    Set(value As Byte)
        If value.Equals(_ParityReplace) Then Return
        _ParityReplace = value
        SerialPort_ReOpen_Flag = True
    End Set
End Property
Public Property PortName As String
    Get
        Return _PortName
    End Get
    Set(value As String)
        If value.Equals(_PortName) Then Return
        _PortName = value
        SerialPort_ReOpen_Flag = True
    End Set
End Property
Public Property ReadBufferSize As Integer
    Get
        Return _ReadBufferSize
    End Get
    Set(value As Integer)
        If value.Equals(_ReadBufferSize) Then Return
        _ReadBufferSize = value
        SerialPort_ReOpen_Flag = True
    End Set
End Property
Public Property ReadTimeout As Integer
    Get
        Return _ReadTimeout
    End Get
    Set(value As Integer)
        If value.Equals(_ReadTimeout) Then Return
        _ReadTimeout = value
        SerialPort_ReOpen_Flag = True
    End Set
End Property
Public Property ReceivedBytesThreshold As Integer
    Get
        Return _ReceivedBytesThreshold
    End Get
    Set(value As Integer)
        If value.Equals(_ReceivedBytesThreshold) Then Return
        _ReceivedBytesThreshold = value
        SerialPort_ReOpen_Flag = True
    End Set
End Property
Public Property RtsEnable As Boolean
    Get
        Return _RtsEnable
    End Get
    Set(value As Boolean)
        If value.Equals(_RtsEnable) Then Return
        _RtsEnable = value
        SerialPort_ReOpen_Flag = True
    End Set
End Property
Public Property StopBits As IO.Ports.StopBits
    Get
        Return _StopBits
    End Get
    Set(value As IO.Ports.StopBits)
        If value.Equals(_StopBits) Then Return
        _StopBits = value
        SerialPort_ReOpen_Flag = True
    End Set
End Property
Public Property WriteBufferSize As Integer
    Get
        Return _WriteBufferSize
    End Get
    Set(value As Integer)
        If value.Equals(_WriteBufferSize) Then Return
        _WriteBufferSize = value
        SerialPort_ReOpen_Flag = True
    End Set
End Property
Public Property WriteTimeout As Integer
    Get
        Return _WriteTimeout
    End Get
    Set(value As Integer)
        If value.Equals(_WriteTimeout) Then Return
        _WriteTimeout = value
        SerialPort_ReOpen_Flag = True
    End Set
End Property
\$\endgroup\$
0

1 Answer 1

2
+50
\$\begingroup\$

Is there more code in this class? it looks like other objects are instantiated, but not used:

Private ReadOnly Common_Methods As New Common_Class

and

Private Shared ReadOnly Search_Com_Lock_Object As New Object

To answer your question, this code can be simplified. Is the truth bit that has been added to each statement for determining if the port is open?

SerialPort_ReOpen_Flag = True

if so it would be better to use the built-in SerialPort.IsOpen Property to check when the port is open. After removing that flag you can remove the getter and setter methods and just use the Auto-implemented syntax for properties like _BaudRate. You can review this type of setup in the Microsoft Docs

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Thank you for your input. Common_Methods and Search_Com_Lock_Object are used in functions later on within the class. I didn't show the entire class because it is 1,220 lines of code and it contains some proprietary information. \$\endgroup\$
    – Pslice
    Jun 17, 2022 at 22:46
  • \$\begingroup\$ holy moly, I've worked with a range of hardware devices and I've never seen a VB.NET serial port wrapper need more than 300 lines of code. \$\endgroup\$ Jun 20, 2022 at 16:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.