📄 crs232.vb
字号:
' Description : Comunication parity
' Created : 21/09/2001 - 11:27:15
'
' *Parameters Info*
'
' Notes :
'===================================================
Get
Return meParity
End Get
Set(ByVal Value As DataParity)
meParity = Value
End Set
End Property
Public Property StopBit() As DataStopBit
'===================================================
'
' Description: Comunication StopBit
' Created : 21/09/2001 - 11:27:37
'
' *Parameters Info*
'
' Notes :
'===================================================
Get
Return meStopBit
End Get
Set(ByVal Value As DataStopBit)
meStopBit = Value
End Set
End Property
Public Property BaudRate() As Integer
'===================================================
'
' Description: Comunication BaudRate
' Created : 21/09/2001 - 11:28:00
'
' *Parameters Info*
'
' Notes :
'===================================================
Get
Return miBaudRate
End Get
Set(ByVal Value As Integer)
miBaudRate = Value
End Set
End Property
Public Property DataBit() As Integer
'===================================================
'
' Description : Comunication DataBit
' Created : 21/09/2001 - 11:28:20
'
' *Parameters Info*
'
' Notes :
'===================================================
Get
Return miDataBit
End Get
Set(ByVal Value As Integer)
miDataBit = Value
End Set
End Property
Public Property BufferSize() As Integer
'===================================================
'
' Description : Receive Buffer size
' Created : 21/09/2001 - 11:33:05
'
' *Parameters Info*
'
' Notes :
'===================================================
Get
Return miBufferSize
End Get
Set(ByVal Value As Integer)
miBufferSize = Value
End Set
End Property
Overridable ReadOnly Property InputStream() As Byte()
'===================================================
'
' Description: Returns received data as Byte()
' Created : 21/09/2001 - 11:45:06
'
' *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
'
' *Parameters Info*
'
' Notes :
'===================================================
Get
Dim oEncoder As New System.Text.ASCIIEncoding
Dim oEnc As Encoding = oEncoder.GetEncoding(1252)
'-------------------------------------------------------------
If Not Me.InputStream Is Nothing Then Return oEnc.GetString(Me.InputStream, 0, Me.InputStream.Length)
End Get
End Property
Public WriteOnly Property Rts() As Boolean
'===================================================
'
' Description: Set/Resets RTS Line
' Created : 21/09/2001 - 11:45:34
'
' *Parameters Info*
'
' Notes :
'===================================================
Set(ByVal Value As Boolean)
If m_hcom.ToInt32 <> 0 And m_hcom.ToInt32 <> INVALID_HANDLE_VALUE Then
If Value Then
EscapeCommFunction(m_hcom, Lines.SetRts)
Else
EscapeCommFunction(m_hcom, Lines.ClearRts)
End If
End If
End Set
End Property
Public WriteOnly Property Dtr() As Boolean
'===================================================
'
' Description: Set/Resets DTR Line
' Created : 21/09/2001 - 11:45:34
'
' *Parameters Info*
'
' Notes :
'===================================================
Set(ByVal Value As Boolean)
If m_hcom.ToInt32 <> 0 And m_hcom.ToInt32 <> INVALID_HANDLE_VALUE Then
If Value Then
EscapeCommFunction(m_hcom, Lines.SetDtr)
Else
EscapeCommFunction(m_hcom, Lines.ClearDtr)
End If
End If
End Set
End Property
Public ReadOnly Property ModemStatus() As ModemStatusBits
'===================================================
'
' Description : Gets Modem status
' Created : 28/02/2002 - 8:58:04
'
' *Parameters Info*
'
' Notes :
'===================================================
Get
If m_hcom.ToInt32 = 0 Or m_hcom.ToInt32 = INVALID_HANDLE_VALUE Then
Throw New ApplicationException("Please initialize and open port before using this method")
Else
'// Retrieve modem status
Dim lpModemStatus As Int32
If Not GetCommModemStatus(m_hcom, lpModemStatus) Then
Throw New ApplicationException("Unable to get modem status")
Else
Return CType(lpModemStatus, ModemStatusBits)
End If
End If
End Get
End Property
Public Property UseXonXoff() As Boolean
'===================================================
'
' Description : Set XON/XOFF mode
' Created : 26/05/2003 - 21:16:18
'
' *Parameters Info*
'
' Notes :
'===================================================
Get
Return mbUseXonXoff
End Get
Set(ByVal Value As Boolean)
mbUseXonXoff = Value
End Set
End Property
Public Property RxBufferThreshold() As Int32
'===================================================
' ?003 www.codeworks.it All rights reserved
'
' Description : Numer of characters into input buffer
' Created : 16/07/03 - 9:00:57
' Author : Corrado Cavalli
'
' *Parameters Info*
'
' Notes :
'===================================================
Get
Return miBufThreshold
End Get
Set(ByVal Value As Int32)
miBufThreshold = Value
End Set
End Property
Public ReadOnly Property InBufferCount() As Int32
'===================================================
' ?003 www.codeworks.it All rights reserved
'
' Description : Returns the number of bytes inside Rx buffer
' Created : 20/04/05 - 10:02:57
' Author : Corrado Cavalli/Jean-Pierre ZANIER
'
'
'===================================================
Get
Dim comStat As COMSTAT
Dim lpErrCode As Int32
Dim iRc As Int32
comStat.cbInQue = 0
If m_hcom.ToInt32 = 0 And m_hcom.ToInt32 = INVALID_HANDLE_VALUE Then
iRc = ClearCommError(m_hcom, lpErrCode, comStat)
Return comStat.cbInQue
End If
Return 0
End Get
End Property
#End Region
#Region "类 public 共有成员函数声明和实现 "
Public Overloads Sub Open()
'===================================================
'
' Description : Initializes and Opens comunication port
' Created : 2005-8-13 14:09
'
' *Parameters Info*
'
' writer :韩俊峰
' company:重庆联康科技有限公司
'===================================================
'// Get Dcb block,Update with current data
Dim uDcb As DCB, iRc As Int32
'// Set working mode
meMode = Mode.Overlapped
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
m_hcom = CreateFile("COM" & miPort.ToString + ":", GENERIC_READ Or GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0)
If (m_hcom.ToInt32 <> INVALID_HANDLE_VALUE And m_hcom.ToInt32 <> 0) Then
'// Clear all comunication errors
Dim lpErrCode As Int32
iRc = ClearCommError(m_hcom, lpErrCode, New COMSTAT)
'// Clears I/O buffers
iRc = PurgeComm(m_hcom, PurgeBuffers.RXClear Or PurgeBuffers.TxClear)
'// Gets COM Settings
iRc = GetCommState(m_hcom, uDcb)
'// Updates COM Settings
'Dim sParity As String = "NOEM"
'sParity = sParity.Substring(meParity, 1)
'// Set DCB State
'Dim sDCBState As String = String.Format("baud={0} parity={1} data={2} stop={3}", miBaudRate, sParity, miDataBit, CInt(meStopBit))
'iRc = BuildCommDCB(sDCBState, uDcb)
uDcb.Parity = CByte(meParity)
uDcb.BaudRate = miBaudRate
uDcb.ByteSize = CByte(miDataBit)
uDcb.StopBits = CByte(meStopBit)
'// Set Xon/Xoff State
If mbUseXonXoff Then
uDcb.Bits1 = 768
Else
uDcb.Bits1 = 0
End If
iRc = SetCommState(m_hcom, uDcb)
If iRc = 0 Then
Dim sErrTxt As String = New Win32Exception().Message
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -