我有一个继承SerialPort
的类,这个类设置串口的属性,如下面的代码所示。
Public Class Ports
Inherits SerialPort
WithEvents port As New SerialPort()
Sub New()
With port
.BaudRate = 19200
.Parity = IO.Ports.Parity.None
.DataBits = 8
.StopBits = IO.Ports.StopBits.One
.Handshake = IO.Ports.Handshake.None
.RtsEnable = False
.ReceivedBytesThreshold = 1
.NewLine = vbCr
.ReadTimeout = 10000
End With
End Sub
End Class
这个问题是我实例化的时候,
Private WithEvents testPort As New Ports()
SerialPort
的属性不会转移,并设置默认值。
答案 0 :(得分:1)
问题是因为您没有为对象设置实例,而是在为该实例内的另一个变量执行此操作。正如@the_lotus指出的那样,删除变量或继承。我建议只删除不需要的变量WithEvents port As New SerialPort()
,然后使用实例...
Public Class Ports
Inherits SerialPort
Sub New()
With Me
.BaudRate = 19200
.Parity = IO.Ports.Parity.None
.DataBits = 8
.StopBits = IO.Ports.StopBits.One
.Handshake = IO.Ports.Handshake.None
.RtsEnable = False
.ReceivedBytesThreshold = 1
.NewLine = vbCr
.ReadTimeout = 10000
End With
End Sub
End Class