📄 crs232.vb
字号:
Throw New CIOChannelException("Unable to set COM state " & sErrTxt)
End If
'// Setup Buffers (Rx,Tx)
iRc = SetupComm(m_hcom, miBufferSize, miBufferSize)
'// Set Timeouts
pSetTimeout()
'//Enables events if required
'mbEnableEvents = True
If mbEnableEvents Then Me.EnableEvents()
Else
'// Raise Initialization problems
Dim sErrTxt As String = New Win32Exception().Message
Throw New CIOChannelException("不能打开 COM" + miPort.ToString + ControlChars.CrLf + sErrTxt)
End If
Catch Ex As Exception
'// Generica error
'Throw New CIOChannelException(Ex.Message, Ex)
MsgBox(Ex.Message)
End Try
Else
'// Port not defined, cannot open
Throw New ApplicationException("请选定 串口号 并设置串口配置参数")
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)
'===================================================
'
' Description: Opens comunication port (Overloaded method)
' Created : 21/09/2001 - 11:33:40
'
' *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()
'===================================================
'
' Description: Close comunication channel
' Created : 21/09/2001 - 11:38:00
'
' *Parameters Info*
'
' Notes :
'===================================================
If m_hcom.ToInt32 <> 0 And m_hcom.ToInt32 <> INVALID_HANDLE_VALUE Then
If mbEnableEvents = True Then
Me.DisableEvents()
End If
Dim ret As Boolean = CloseHandle(m_hcom)
If Not ret Then Throw New Win32Exception
m_hcom = New IntPtr(0)
End If
End Sub
ReadOnly Property IsOpen() As Boolean
'===================================================
'
' Description: Returns Port Status
' Created : 21/09/2001 - 11:38:51
'
' *Parameters Info*
'
' Notes :
'===================================================
Get
Return CBool(m_hcom.ToInt32 <> 0 And m_hcom.ToInt32 <> INVALID_HANDLE_VALUE)
End Get
End Property
Public Overloads Sub Write(ByVal Buffer As Byte())
'===================================================
'
' Description: Read Bytes from Port
' Created : 2005-8-13 12:17
'
' *Parameters Info*
' Bytes2Read : Bytes to read from port
' Returns : Number of readed chars
' writer :韩俊峰
' company:重庆联康科技有限公司 :
'===================================================
Dim iRc, iBytesWritten As Integer, hOvl As GCHandle
'-----------------------------------------------------------------
muOvlW = New OVERLAPPED
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
'// Creates Event
Try
hOvl = GCHandle.Alloc(muOvlW, GCHandleType.Pinned)
muOvlW.hEvent = CreateEvent(Nothing, 1, 0, Nothing)
If muOvlW.hEvent.ToInt32 = 0 Then Throw New ApplicationException("Error creating event for overlapped writing")
'// Clears IO buffers and sends data
iRc = WriteFile(m_hcom, Buffer, Buffer.Length, 0, muOvlW)
If iRc = 0 Then
If Marshal.GetLastWin32Error <> ERROR_IO_PENDING Then
Throw New ApplicationException("Write command error")
Else
'// Check Tx results
If GetOverlappedResult(m_hcom, muOvlW, iBytesWritten, 1) = 0 Then
Throw New ApplicationException("Write pending error")
Else
'// All bytes sent?
If iBytesWritten <> Buffer.Length Then Throw New ApplicationException("Write Error - Bytes Written " & iBytesWritten.ToString & " of " & Buffer.Length.ToString)
End If
End If
End If
Finally
'//Closes handle
CloseHandle(muOvlW.hEvent)
If (hOvl.IsAllocated = True) Then hOvl.Free()
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
'
' *Parameters Info*
'
' Notes : 24/05/2002 Fixed problem with ASCII Encoding
'===================================================
Dim oEncoder As New System.Text.ASCIIEncoding
Dim oEnc As Encoding = oEncoder.GetEncoding(1252)
'-------------------------------------------------------------
Dim aByte() As Byte = oEnc.GetBytes(Buffer)
Me.Write(aByte)
End Sub
Public Function Read(ByVal Bytes2Read As Integer) As Integer
'===================================================
'
' Description: Read Bytes from Port
' Created : 2005-8-13 12:17
'
' *Parameters Info*
' Bytes2Read : Bytes to read from port
' Returns : Number of readed chars
' writer :韩俊峰
' company:重庆联康科技有限公司
'===================================================
Dim iReadChars, iRc As Integer, bReading As Boolean, hOvl As GCHandle
'--------------------------------------------------------------
'// If Bytes2Read not specified uses Buffersize
If Bytes2Read = 0 Then Bytes2Read = miBufferSize
muOvlR = New OVERLAPPED
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
'// Get bytes from port
Try
hOvl = GCHandle.Alloc(muOvlR, GCHandleType.Pinned)
muOvlR.hEvent = CreateEvent(Nothing, 1, 0, Nothing)
If muOvlR.hEvent.ToInt32 = 0 Then Throw New ApplicationException("Error creating event for overlapped reading")
'// Clears IO buffers and reads data
ReDim mabtRxBuf(Bytes2Read - 1)
iRc = ReadFile(m_hcom, mabtRxBuf, Bytes2Read, iReadChars, muOvlR)
If iRc = 0 Then
If Marshal.GetLastWin32Error() <> ERROR_IO_PENDING Then
Throw New ApplicationException("Read pending error")
Else
'// Wait for characters
iRc = WaitForSingleObject(muOvlR.hEvent, miTimeout)
Select Case iRc
Case WAIT_OBJECT_0
'// Some data received...
If GetOverlappedResult(m_hcom, muOvlR, iReadChars, 0) = 0 Then
Throw New ApplicationException("Read pending error.")
Else
Return iReadChars
End If
Case WAIT_TIMEOUT
Throw New IOTimeoutException("Read Timeout.")
Case Else
Throw New ApplicationException("General read error.")
End Select
End If
Else
Return (iReadChars)
End If
Finally
'//Closes handle
CloseHandle(muOvlR.hEvent)
If (hOvl.IsAllocated) Then hOvl.Free()
End Try
End If
End Function
Public Sub ClearInputBuffer()
'===================================================
'
' Description: Clears Input buffer
' Created : 21/09/2001 - 11:45:34
'
' *Parameters Info*
'
' Notes : Gets all character until end of buffer
'===================================================
If m_hcom.ToInt32 <> 0 And m_hcom.ToInt32 <> INVALID_HANDLE_VALUE Then
PurgeComm(m_hcom, PURGE_RXCLEAR)
End If
End Sub
Public Function CheckLineStatus(ByVal Line As ModemStatusBits) As Boolean
'===================================================
'
' Description : Check status of a Modem Line
' Created : 28/02/2002 - 10:25:17
'
' *Parameters Info*
'
' Notes :
'===================================================
Return Convert.ToBoolean(ModemStatus And Line)
End Function
Public Shared Function IsPortAvailable(ByVal portNumber As Int32) As Boolean
'===================================================
' ?003 www.codeworks.it All rights reserved
'
' Description : Returns true if a specific port number is supported by the system
' Created : 14/09/03 - 17:00:57
' Author : Corrado Cavalli
'
' *Parameters Info*
' portNumber : port number to check
'
' Notes :
'===================================================
If portNumber <= 0 Then
Return False
Else
Dim cfg As COMMCONFIG
Dim cfgsize As Int32 = Marshal.SizeOf(cfg)
cfg.dwSize = cfgsize
Dim ret As Boolean = GetDefaultCommConfig("COM" + portNumber.ToString, cfg, cfgsize)
Return ret
End If
End Function
Public Sub SetBreak()
'===================================================
' ?003 www.codeworks.it All rights reserved
'
' Description : Set COM in break modem
' Created : 12/10/03 - 10:00:57
' Author : Corrado Cavalli
'
' *Parameters Info*
'
'
' Notes :
'===================================================
If m_hcom.ToInt32 <> 0 And m_hcom.ToInt32 <> INVALID_HANDLE_VALUE Then
If SetCommBreak(m_hcom) = False Then Throw New Win32Exception
End If
End Sub
Public Sub ClearBreak()
'===================================================
' ?003 www.codeworks.it All rights reserved
'
' Description : Clear COM break mode
' Created : 12/10/03 - 10:02:57
' Author : Corrado Cavalli
'
' *Parameters Info*
'
'
' Notes :
'===================================================
If m_hcom.ToInt32 <> 0 And m_hcom.ToInt32 <> INVALID_HANDLE_VALUE Then
If ClearCommBreak(m_hcom) = False Then Throw New Win32Exception
End If
End Sub
#End Region
#Region "串口通信 执行事务 超时设置"
Private Sub pSetTimeout()
'===================================================
'
' Description: Set comunication timeouts
' Created : 21/09/2001 - 11:46:40
'
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -