📄 crs232.vb
字号:
Return miDataBit
End Get
Set(ByVal Value As Integer)
miDataBit = Value
End Set
End Property
Public Property BufferSize() As Integer
'===================================================
' ?001 Corrado Cavalli All rights reserved
'
' Description: Receive Buffer size
' Created : 21/09/2001 - 11:33:05
' Author : Corrado Cavalli
'
' *Parameters Info*
'
' Notes :
'===================================================
Get
Return miBufferSize
End Get
Set(ByVal Value As Integer)
miBufferSize = Value
End Set
End Property
Public Overloads Sub Open()
'===================================================
' ?001 Corrado Cavalli All rights reserved
'
' Description: Initializes and Opens comunication port
' Created : 21/09/2001 - 11:33:40
' Author : Corrado Cavalli
'
' *Parameters Info*
'
' Notes :
'===================================================
'// Get Dcb block,Update with current data
Dim uDcb As DCB, iRc As Int32
'// Set working mode
Dim iMode As Int32 = Convert.ToInt32(IIf(meMode = Mode.Overlapped, FILE_FLAG_OVERLAPPED, 0))
'// Initializes Com Port
If miPort > 0 Then
Try
'// Creates a COM Port stream handle
mhRS = CreateFile("COM" & miPort.ToString, _
GENERIC_READ Or GENERIC_WRITE, 0, 0, _
OPEN_EXISTING, iMode, 0)
If mhRS <> -1 Then
'// Clear all comunication errors
Dim lpErrCode As Int32
iRc = ClearCommError(mhRS, lpErrCode, 0&)
'// Clears I/O buffers
iRc = PurgeComm(mhRS, PurgeBuffers.RXClear Or PurgeBuffers.TxClear)
'// Gets COM Settings
iRc = GetCommState(mhRS, uDcb)
'// Updates COM Settings
Dim sParity As String = "noem"
sParity = sParity.Substring(meParity, 1)
Dim sDCBState As String = miBaudRate.ToString & "," & sParity & "," & _
miDataBit.ToString & "," & CInt(meStopBit).ToString
iRc = BuildCommDCB(sDCBState, uDcb)
iRc = SetCommState(mhRS, uDcb)
'// Setup Buffers (Rx,Tx)
iRc = SetupComm(mhRS, miBufferSize, miBufferSize)
'// Set Timeouts
pSetTimeout()
Else
'// Raise Initialization problems
Throw New CIOChannelException("Unable to open COM" & miPort.ToString)
End If
Catch Ex As Exception
'// Generica error
Throw New CIOChannelException(Ex.Message, Ex)
End Try
Else
'// Port not defined, cannot open
Throw New ApplicationException("COM Port not defined,use Port property to set it before invoking InitPort")
End If
End Sub
Public Overloads Sub Open(ByVal Port As Integer, ByVal BaudRate As Integer, ByVal DataBit As Integer, ByVal Parity As DataParity, ByVal StopBit As DataStopBit, ByVal BufferSize As Integer)
'===================================================
' ?001 Corrado Cavalli All rights reserved
'
' Description: Opens comunication port (Overloaded method)
' Created : 21/09/2001 - 11:33:40
' Author : Corrado Cavalli
'
' *Parameters Info*
'
' Notes :
'===================================================
Me.Port = Port
Me.BaudRate = BaudRate
Me.DataBit = DataBit
Me.Parity = Parity
Me.StopBit = StopBit
Me.BufferSize = BufferSize
Open()
End Sub
Public Sub Close()
'===================================================
' ?001 Corrado Cavalli All rights reserved
'
' Description: Close comunication channel
' Created : 21/09/2001 - 11:38:00
' Author : Corrado Cavalli
'
' *Parameters Info*
'
' Notes :
'===================================================
If mhRS <> -1 Then
CloseHandle(mhRS)
mhRS = -1
End If
End Sub
ReadOnly Property IsOpen() As Boolean
'===================================================
' ?001 Corrado Cavalli All rights reserved
'
' Description: Returns Port Status
' Created : 21/09/2001 - 11:38:51
' Author : Corrado Cavalli
'
' *Parameters Info*
'
' Notes :
'===================================================
Get
Return CBool(mhRS <> -1)
End Get
End Property
Public Overloads Sub Write(ByVal Buffer As Byte())
'===================================================
' ?001 Corrado Cavalli All rights reserved
'
' Description: Transmit a stream
' Created : 21/09/2001 - 11:39:51
' Author : Corrado Cavalli
'
' *Parameters Info*
' Buffer : Array of Byte() to write
' Notes :
'===================================================
Dim iBytesWritten, iRc As Integer
'---------------------------------
If mhRS = -1 Then
Throw New ApplicationException("Please initialize and open port before using this method")
Else
'// Transmit data to COM Port
Try
If meMode = Mode.Overlapped Then
'// Overlapped write
If pHandleOverlappedWrite(Buffer) Then
Throw New ApplicationException("Error in overllapped write")
End If
Else
'// Clears IO buffers
PurgeComm(mhRS, PURGE_RXCLEAR Or PURGE_TXCLEAR)
iRc = WriteFile(mhRS, Buffer, Buffer.Length, iBytesWritten, Nothing)
If iRc = 0 Then
Throw New ApplicationException("Write Error - Bytes Written " & iBytesWritten.ToString & " of " & Buffer.Length.ToString)
End If
End If
Catch Ex As Exception
Throw
End Try
End If
End Sub
Public Overloads Sub Write(ByVal Buffer As String)
'===================================================
'
'
' Description : Writes a string to RS232
' Created : 04/02/2002 - 8:46:42
' Author : Corrado Cavalli
'
' *Parameters Info*
'
' Notes :
'===================================================
Dim oEncoder As New System.Text.ASCIIEncoding()
'-------------------------------------------------------------
Dim aByte() As Byte = oEncoder.GetBytes(Buffer)
Me.Write(aByte)
End Sub
Public Function Read(ByVal Bytes2Read As Integer) As Integer
'===================================================
' ?001 Corrado Cavalli All rights reserved
'
' Description: Read Bytes from Port
' Created : 21/09/2001 - 11:41:17
' Author : Corrado Cavalli
'
' *Parameters Info*
' Bytes2Read : Bytes to read from port
' Returns : Number of readed chars
'
' Notes :
'===================================================
Dim iReadChars, iRc As Integer
'--------------------------------------
'// If Bytes2Read not specified uses Buffersize
If Bytes2Read = 0 Then Bytes2Read = miBufferSize
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
If meMode = Mode.Overlapped Then
pHandleOverlappedRead(Bytes2Read)
Else
'// Non overlapped mode
ReDim mabtRxBuf(Bytes2Read - 1)
iRc = ReadFile(mhRS, mabtRxBuf, Bytes2Read, iReadChars, Nothing)
If iRc = 0 Then
'// Read Error
Throw New ApplicationException("ReadFile error " & iRc.ToString)
Else
'// Handles timeout or returns input chars
ReDim Preserve mabtRxBuf(iReadChars - 1) 'HSC注
'这一句是我加的,要不然返回的数据的长度就是你指定的长度
'如果信息量不确定的话,很容易返回很多的零
'做这个改进就没有问题了
If iReadChars < Bytes2Read Then
Throw New IOTimeoutException("Timeout error")
'HSC注:我觉得这个地方没有处理好,如果读入的东西比预先的少
'那么就会出现这个错误,建议关闭。
Else
mbWaitOnRead = True
Return (iReadChars)
End If
End If
End If
Catch Ex As Exception
'// Others generic erroes
Throw New ApplicationException("Read Error: " & Ex.Message, Ex)
End Try
End If
End Function
Overridable ReadOnly Property InputStream() As Byte()
'===================================================
' ?001 Corrado Cavalli All rights reserved
'
' Description: Returns received data as Byte()
' Created : 21/09/2001 - 11:45:06
' Author : Corrado Cavalli
'
' *Parameters Info*
'
' Notes :
'===================================================
Get
Return mabtRxBuf
End Get
End Property
Overridable ReadOnly Property InputStreamString() As String
'===================================================
'
'
' Description : Return a string containing received data
' Created : 04/02/2002 - 8:49:55
' Author : Corrado Cavalli
'
' *Parameters Info*
'
' Notes :
'===================================================
Get
Dim oEncoder As New System.Text.ASCIIEncoding()
'-------------------------------------------------------------
Return oEncoder.GetString(Me.InputStream)
End Get
End Property
Public Sub ClearInputBuffer()
'===================================================
' ?001 Corrado Cavalli All rights reserved
'
' Description: Clears Input buffer
' Created : 21/09/2001 - 11:45:34
' Author : Corrado Cavalli
'
' *Parameters Info*
'
' Notes : Gets all character until end of buffer
'===================================================
If Not mhRS = -1 Then
PurgeComm(mhRS, PURGE_RXCLEAR)
End If
End Sub
Public WriteOnly Property Rts() As Boolean
'===================================================
' ?001 Corrado Cavalli All rights reserved
'
' Description: Set/Resets RTS Line
' Created : 21/09/2001 - 11:45:34
' Author : Corrado Cavalli
'
' *Parameters Info*
'
' Notes :
'===================================================
Set(ByVal Value As Boolean)
If Not mhRS = -1 Then
If Value Then
EscapeCommFunction(mhRS, Lines.SetRts)
Else
EscapeCommFunction(mhRS, Lines.ClearRts)
End If
End If
End Set
End Property
Public WriteOnly Property Dtr() As Boolean
'===================================================
' ?001 Corrado Cavalli All rights reserved
'
' Description: Set/Resets DTR Line
' Created : 21/09/2001 - 11:45:34
' Author : Corrado Cavalli
'
' *Parameters Info*
'
' Notes :
'===================================================
Set(ByVal Value As Boolean)
If Not mhRS = -1 Then
If Value Then
EscapeCommFunction(mhRS, Lines.SetDtr)
Else
EscapeCommFunction(mhRS, Lines.ClearDtr)
End If
End If
End Set
End Property
Public ReadOnly Property ModemStatus() As ModemStatusBits
'===================================================
' ?002 Corrado Cavalli All rights reserved
'
' Description : Gets Modem status
' Created : 28/02/2002 - 8:58:04
' Author : Corrado Cavalli
'
' *Parameters Info*
'
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -