⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rs232.vb

📁 Corrado Cavalli为VB.NET写的串口通讯类。通过调用API的方法与串口通讯。我的有关串口通讯的程序都是用的这个类。
💻 VB
字号:
'Keith Langer 2/5/02
'Modified to handle overlapped IO and provide asynchronous read/write
Imports System
Imports System.Runtime.InteropServices
Imports Microsoft.VisualBasic
Imports System.Threading

Public Class CMediaRs232
        Implements IDisposable
		'// Class Members		
		Private mhRS As Int32 = -1		 '// Handle to Com Port									
		Private miPort As Integer = 1		 '//  Default is COM1	
		Private miTimeout As Int32 = 70		 '// Timeout in ms
		Private miBaudRate As Int32 = 9600
		Private meParity As DataParity = 0
		Private meStopBit As DataStopBit = 0
		Private miDataBit As Int32 = 8
		Private miBufferSize As Int32 = 512		 '// Buffers size default to 512 bytes
		Private mabtRxBuf As Byte()		 '//  Receive buffer	
		'----------------------------------------------------------------------------------------
        'KL 2/4/02 - Listener and Writer threads for async
        Private mobjThreadListen As Thread
        Private mobjThreadWrite As Thread
        Private mbytWriteBuffer() As Byte
        Public Event DataReceived(ByVal sender As CMediaRs232, ByVal Data() As Byte)
        Public Event DataSent(ByVal sender As CMediaRs232)
        Private blnIsSending As Boolean
#Region "Enums"
        '// Parity Data
        Public Enum DataParity
                Parity_None = 0
                Pariti_Odd
                Parity_Even
                Parity_Mark
        End Enum
        '// StopBit Data
        Public Enum DataStopBit
                StopBit_1 = 1
                StopBit_2
        End Enum
        Private Enum PurgeBuffers
                RXAbort = &H2
                RXClear = &H8
                TxAbort = &H1
                TxClear = &H4
        End Enum

        Private Enum Lines
                SetRts = 3
                ClearRts = 4
                SetDtr = 5
                ClearDtr = 6
        End Enum
#End Region
#Region "Structures"
        <StructLayout(LayoutKind.Sequential, Pack:=1)> Private Structure DCB
                Private DCBlength As Int32
                Private BaudRate As Int32
                Private Bits1 As Int32
                Private wReserved As Int16
                Private XonLim As Int16
                Private XoffLim As Int16
                Private ByteSize As Byte
                Private Parity As Byte
                Private StopBits As Byte
                Private XonChar As Byte
                Private XoffChar As Byte
                Private ErrorChar As Byte
                Private EofChar As Byte
                Private EvtChar As Byte
                Private wReserved2 As Int16
        End Structure
        <StructLayout(LayoutKind.Sequential, Pack:=1)> Private Structure COMMTIMEOUTS
                Public ReadIntervalTimeout As Int32
                Public ReadTotalTimeoutMultiplier As Int32
                Public ReadTotalTimeoutConstant As Int32
                Public WriteTotalTimeoutMultiplier As Int32
                Public WriteTotalTimeoutConstant As Int32
        End Structure

        <StructLayout(LayoutKind.Sequential, Pack:=1)> Private Structure COMMCONFIG
                Private dwSize As Int32
                Private wVersion As Int16
                Private wReserved As Int16
                Private dcbx As DCB
                Private dwProviderSubType As Int32
                Private dwProviderOffset As Int32
                Private dwProviderSize As Int32
                Private wcProviderData As Byte
        End Structure

        '<StructLayout(LayoutKind.Sequential, Pack:=1)> Public Structure OVERLAPPED
        '		Private Internal As Int32
        '		Private InternalHigh As Int32
        '		Private Offset As Int32
        '		Private OffsetHigh As Int32
        '		Private hEvent As Int32
  '      End Structure

'KL 2/4/02 - need public variables?
        <StructLayout(LayoutKind.Sequential, Pack:=1)> Private Structure OVERLAPPED
            Public Internal As Int32
            Public InternalHigh As Int32
            Public offset As Int32
            Public OffsetHigh As Int32
            Public hEvent As Int32
        End Structure
#End Region
#Region "Constants"
        Private Const PURGE_RXABORT As Integer = &H2
        Private Const PURGE_RXCLEAR As Integer = &H8
        Private Const PURGE_TXABORT As Integer = &H1
        Private Const PURGE_TXCLEAR As Integer = &H4
        Private Const GENERIC_READ As Integer = &H80000000
        Private Const GENERIC_WRITE As Integer = &H40000000
        Private Const OPEN_EXISTING As Integer = 3
        Private Const INVALID_HANDLE_VALUE As Integer = -1
        Private Const IO_BUFFER_SIZE As Integer = 1024

#End Region

#Region "Win32API"
        'KL 2/4/02
        Const FILE_FLAG_OVERLAPPED As Integer = &H40000000
        Const INFINITE = -1&
        Const ERROR_IO_PENDING = 997
        Const WAIT_OBJECT_0 = 0
        Const EV_RXCHAR As Integer = &H1
        Const WAIT_TIMEOUT = &H102&


        '// Win32 API
        <DllImport("kernel32.dll")> Private Shared Function SetCommState(ByVal hCommDev As Int32, ByRef lpDCB As DCB) As Int32
        End Function
        <DllImport("kernel32.dll")> Private Shared Function GetCommState(ByVal hCommDev As Int32, ByRef lpDCB As DCB) As Int32
        End Function
        <DllImport("kernel32.dll", CharSet:=CharSet.Auto)> Private Shared Function BuildCommDCB(ByVal lpDef As String, ByRef lpDCB As DCB) As Int32
        End Function
        <DllImport("kernel32.dll")> Private Shared Function SetupComm(ByVal hFile As Int32, ByVal dwInQueue As Int32, ByVal dwOutQueue As Int32) As Int32
        End Function
        <DllImport("kernel32.dll")> Private Shared Function SetCommTimeouts(ByVal hFile As Int32, ByRef lpCommTimeouts As COMMTIMEOUTS) As Int32
        End Function
        <DllImport("kernel32.dll")> Private Shared Function GetCommTimeouts(ByVal hFile As Int32, ByRef lpCommTimeouts As COMMTIMEOUTS) As Int32
        End Function
        <DllImport("kernel32.dll")> Private Shared Function ClearCommError(ByVal hFile As Int32, ByVal lpErrors As Int32, ByVal l As Int32) As Int32
        End Function
        <DllImport("kernel32.dll")> Private Shared Function PurgeComm(ByVal hFile As Int32, ByVal dwFlags As Int32) As Int32
        End Function
        <DllImport("kernel32.dll")> Private Shared Function EscapeCommFunction(ByVal hFile As Integer, ByVal ifunc As Long) As Boolean
        End Function
        <DllImport("kernel32.dll")> Private Shared Function WaitCommEvent(ByVal hFile As Integer, ByRef Mask As Integer, ByRef lpOverlap As OVERLAPPED) As Int32
        End Function
        '<DllImport("kernel32.dll")> Private Shared Function WriteFile(ByVal hFile As Integer, ByVal Buffer As Byte(), ByVal nNumberOfBytesToWrite As Integer, ByRef lpNumberOfBytesWritten As Integer, ByVal lpOverlapped As Integer) As Integer
'KL 2/4/02
        <DllImport("kernel32.dll")> Private Shared Function WriteFile(ByVal hFile As Integer, ByVal Buffer As Byte(), ByVal nNumberOfBytesToWrite As Integer, ByRef lpNumberOfBytesWritten As Integer, ByRef lpOverlapped As OVERLAPPED) As Integer
        End Function
        <DllImport("kernel32.dll")> Private Shared Function ReadFile(ByVal hFile As Integer, ByVal Buffer As Byte(), ByVal nNumberOfBytesToRead As Integer, ByRef lpNumberOfBytesRead As Integer, ByRef lpOverlapped As OVERLAPPED) As Integer
        End Function
        <DllImport("kernel32.dll")> Private Shared Function CreateFile(<MarshalAs(UnmanagedType.LPStr)> ByVal lpFileName As String, _
        ByVal dwDesiredAccess As Integer, ByVal dwShareMode As Integer, _
        ByVal lpSecurityAttributes As Integer, ByVal dwCreationDisposition As Integer, _
        ByVal dwFlagsAndAttributes As Integer, ByVal hTemplateFile As Integer) As Integer
        End Function
        <DllImport("kernel32.dll")> Private Shared Function CloseHandle(ByVal hObject As Integer) As Integer
        End Function
        <DllImport("kernel32.dll")> Private Shared Function FormatMessage(ByVal dwFlags As Integer, _
        ByVal lpSource As Integer, ByVal dwMessageId As Integer, _
        ByVal dwLanguageId As Integer, _
        <MarshalAs(UnmanagedType.LPStr)> ByVal lpBuffer As String, _
        ByVal nSize As Integer, ByVal Arguments As Integer) As Integer
        End Function

    'KL 2/4/02
        <DllImport("kernel32.dll")> Private Shared Function SetCommMask(ByVal hFile As Int32, ByVal lpEvtMask As Int32) As Int32
        End Function
        Private Declare Function CreateEvent Lib "kernel32" Alias "CreateEventA" (ByVal lpEventAttributes As Int32, ByVal bManualReset As Int32, ByVal bInitialState As Int32, ByVal lpName As String) As Int32
        Private Declare Function GetLastError Lib "kernel32" Alias "GetLastError" () As Int32
        Private Declare Function WaitForSingleObject Lib "kernel32" Alias "WaitForSingleObject" (ByVal hHandle As Int32, ByVal dwMilliseconds As Int32) As Int32
        Private Declare Function GetOverlappedResult Lib "kernel32" Alias "GetOverlappedResult" (ByVal hFile As Int32, ByRef lpOverlapped As OVERLAPPED, ByRef lpNumberOfBytesTransferred As Int32, ByVal bWait As Int32) As Int32

#End Region

        Public Property Port() As Integer
                '===================================================
                '												

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -