📄 comm.vb
字号:
ByRef lpOverlapped As OVERLAPPED) As Integer
End Function
#End Region
#Region "Properties"
Overridable ReadOnly Property InputStream() As Byte()
Get
Return mbytRxBuffer
End Get
End Property
Public Property Port() As Integer
Get
Return miPort
End Get
Set(ByVal Value As Integer)
miPort = Value
End Set
End Property
Public Property DataBit() As Integer
Get
Return miDataBit
End Get
Set(ByVal Value As Integer)
miDataBit = Value
End Set
End Property
Public Property StopBit() As DataStopBit
Get
Return meStopBit
End Get
Set(ByVal Value As DataStopBit)
meStopBit = Value
End Set
End Property
Public Property BufferSize() As Integer
Get
Return miBufferSize
End Get
Set(ByVal Value As Integer)
miBufferSize = Value
End Set
End Property
Public Property BaudRate() As Integer
Get
Return mintBaudRate
End Get
Set(ByVal Value As Integer)
mintBaudRate = Value
End Set
End Property
Public ReadOnly Property CommID() As Integer
Get
Return mintHandle
End Get
End Property
Public Property CommPort() As Integer
Get
Return mintPort
End Get
Set(ByVal Value As Integer)
mintPort = Value
If ((mintPort < 1) Or (mintPort > 16)) Then
Throw New Exception("Invalid port number")
Else
' try to open the port to see if it's available.
mintHandle = CreateFile("COM" + mintPort.ToString() + ":", GENERIC_READ Or GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0)
If (mintHandle = INVALID_HANDLE_VALUE) Then
Throw New Exception("Port is already in use.")
Else
mintHandle = Close(mintHandle)
End If
End If
End Set
End Property
Public Property DataBits() As Integer
Get
Dim retval As Integer = 0
Select Case mbytDataBits
Case DATABITS_5
retval = 5
Case DATABITS_6
retval = 6
Case DATABITS_7
retval = 7
Case DATABITS_8
retval = 8
End Select
Return retval
End Get
Set(ByVal Value As Integer)
Select Case Value
Case 5
mbytDataBits = DATABITS_5
Case 6
mbytDataBits = DATABITS_6
Case 7
mbytDataBits = DATABITS_7
Case 8
mbytDataBits = DATABITS_8
Case Else
Throw New Exception("Invalid number of data bits.")
End Select
End Set
End Property
Public Property InputLen() As Integer
Get
Return mintRXBufferSize
End Get
Set(ByVal Value As Integer)
mintRXBufferSize = Value
End Set
End Property
Public Property OutputLen() As Integer
Get
Return mintTXBufferSize
End Get
Set(ByVal Value As Integer)
mintTXBufferSize = Value
End Set
End Property
Public Property Parity() As String
Get
Dim retval As String = ""
Select Case mbytParity
Case EVENPARITY
retval = "E"
Case MARKPARITY
retval = "M"
Case NOPARITY
retval = "N"
Case ODDPARITY
retval = "O"
Case SPACEPARITY
retval = "S"
End Select
Return retval
End Get
Set(ByVal Value As String)
If (Value.Length > 1) Then
Value = Value.Substring(0, 1)
Select Case Value
Case "E"
mbytParity = EVENPARITY
Case "M"
mbytParity = MARKPARITY
Case "N"
mbytParity = NOPARITY
Case "O"
mbytParity = ODDPARITY
Case "S"
mbytParity = SPACEPARITY
Case Else
Throw New Exception("Invalid parity attribute.")
End Select
End If
End Set
End Property
Public Property StopBits() As Integer
Get
Dim retval As Integer = 0
Select Case mbytStopBits
Case STOPBITS_10
retval = 1
Case STOPBITS_15
retval = 2
Case STOPBITS_20
retval = 3
End Select
Return retval
End Get
Set(ByVal Value As Integer)
Select Case Value
Case 1
mbytStopBits = STOPBITS_10
Case 2
mbytStopBits = STOPBITS_15
Case 3
mbytStopBits = STOPBITS_20
Case Else
Throw New Exception("Invalid number of stop bits.")
End Select
End Set
End Property
Public Property Timeout() As Integer
Get
Return mintTimeout
End Get
Set(ByVal Value As Integer)
mintTimeout = Value
End Set
End Property
#End Region
#Region "通讯子程序"
' This function returns an integer specifying the number of bytes
' read from the Comm Port. It accepts a parameter specifying the number
' of desired bytes to read.
Public Function Read(ByVal mhRS As Integer, ByVal Bytes2Read As Integer) As Integer
Dim iReadChars, iRc As Integer
' If Bytes2Read not specified uses Buffersize
If Bytes2Read = 0 Then Bytes2Read = 512
If mhRS = -1 Then
Throw New ApplicationException( _
"Please initialize and open port before using this method")
Else
' Get bytes from port
Try
' Purge buffers
'PurgeComm(mhRS, PURGE_RXCLEAR Or PURGE_TXCLEAR)
' Creates an event for overlapped operations
' Non overlapped mode
ReDim mbytRxBuffer(Bytes2Read - 1)
iRc = ReadFile(mhRS, mbytRxBuffer, Bytes2Read, iReadChars, Nothing)
If iRc = 0 Then
' Read Error
Throw New ApplicationException( _
"ReadFile error " & iRc.ToString)
Else
' Handles timeout or returns input chars
If iReadChars < Bytes2Read Then
Else
' ComData = mbytRxBuffer
Return (iReadChars)
End If
End If
Catch ex As Exception
End Try
End If
End Function
Public Function Input(ByVal mintHandle As Integer, ByVal BytesToRead As Integer) As Integer
Dim result As Integer
Dim intResult As Integer
Dim lpNumberOfBytesRead As Integer = 0
Try
If (BytesToRead = 0) Then BytesToRead = mintRXBufferSize
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -