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

📄 crs232.vb

📁 WINCE 下的串口源码,VB.NET2003编写
💻 VB
字号:
Imports System.Runtime.InteropServices
'RxChar = &H1


Public Class CRs232
    Public Property ReadTimeOut() As Integer
        Get
            Return intReadTimeOut
        End Get
        Set(ByVal Value As Integer)
            intReadTimeOut = Value
            Timer1.Interval = Value
        End Set
    End Property


    Public Class dcb
        Friend DCBlength As UInt32
        Friend BaudRate As UInt32
        Friend fBinary As UInt32
        Friend fParity As UInt32
        Friend fOutxCtsFlow As UInt32
        Friend fOutxDsrFlow As UInt32
        Friend fDtrControl As UInt32
        Friend fDsrSensitivity As UInt32
        Friend fTXContinueOnXoff As UInt32
        Friend fOutX As UInt32
        Friend fInX As UInt32
        Friend fErrorChar As UInt32
        Friend fNull As UInt32
        Friend fRtsControl As UInt32
        Friend fAbortOnError As UInt32
        Friend fDummy2 As UInt32
        Friend wReserved As UInt32
        Friend XonLim As UInt32
        Friend XoffLim As UInt32
        Friend ByteSize As Byte
        Friend Parity As Byte
        Friend StopBits As Byte
        Friend XonChar As Char
        Friend XoffChar As Char
        Friend ErrorChar As Char
        Friend EofChar As Char
        Friend EvtChar As Char
        Friend wReserved1 As UInt16
    End Class


    <StructLayout(LayoutKind.Sequential)> Private Structure COMSTAT
        Dim fBitFields As Int32
        Dim cbInQue As Int32
        Dim cbOutQue As Int32
    End Structure

    <DllImport("coredll.dll")> _
    Private Shared Function CreateFile _
    (ByVal lpFileName As String, _
     ByVal dwDesiredAccess As Integer, _
     ByVal dwShareMode As Integer, _
     ByVal lpSecurityAttributes As Integer, _
     ByVal dwCreationDisposition As Integer, _
     ByVal dwFlagAndAttributes As Integer, _
     ByVal hTemplateFile As Integer) As Integer
    End Function

    ' <DllImport("coredll.dll")> _
    ' Private Shared Function ClearCommError _
    '(ByVal hFile As Integer, _
    ' ByVal lpErrors As Integer, _
    ' ByVal lpStat As COMSTAT) As Boolean
    ' End Function


    <DllImport("coredll.dll", SetlastError:=True)> Private Shared Function ClearCommError(ByVal hFile As Integer, ByRef lpErrors As Int32, ByRef lpComStat As COMSTAT) As Int32
    End Function

    <DllImport("coredll.dll")> _
    Private Shared Function GetCommState _
    (ByVal hFile As Integer, _
     ByVal mdcb As DCB) As Integer
    End Function

    <DllImport("coredll.dll")> _
    Private Shared Function SetCommState _
    (ByVal hFile As Integer, _
     ByVal mdcb As DCB) As Integer
    End Function

    <DllImport("coredll.dll")> _
    Private Shared Function ReadFile _
    (ByVal hFile As Integer, _
     ByVal Buffer() As Byte, _
     ByVal nNumberOfBytesToRead As Integer, _
     ByRef lpNumberOfBytesRead As Integer, _
     ByRef lpOverlapped As Integer) As Integer
    End Function

    <DllImport("coredll.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 Boolean
    End Function

    <DllImport("coredll.dll")> _
    Private Shared Function CloseHandle _
    (ByVal hObject As Integer) As Integer
    End Function


    <DllImport("coredll.dll")> _
    Private Shared Function SetupComm _
    (ByVal hObject As Integer, _
    ByVal dwInQueue As Integer, _
    ByVal dwOutQueue As Integer) As Integer
    End Function

    Private intReadTimeOut As Integer = 1500
    Dim inoutfileHandler As Integer
    Dim numReadWrite As Integer
    Dim pError As Integer

    Dim pdcb As dcb
    Dim pstat As COMSTAT = New COMSTAT


    Friend WithEvents Timer1 As System.Windows.Forms.Timer
    Dim stopRead As Boolean = False


    Public Sub openPort()
        inoutfileHandler = CreateFile _
        ("COM1" & ":", _
         &HC0000000, 0, 0, 3, 0, 0)

        'SetupComm(inoutfileHandler, 5120, 5120)
        pdcb = New dcb
        '设置波特率
        GetCommState(inoutfileHandler, pdcb)


        pdcb.BaudRate = UInt32.Parse("19200")
        pdcb.Parity = 0
        pdcb.StopBits = 0
        SetCommState(inoutfileHandler, pdcb)
        'MessageBox.Show(inoutfileHandler.ToString)


    End Sub

    Public Function GetReadBuffer() As Integer
        ClearCommError(inoutfileHandler, pError, pstat)
        Return pstat.cbInQue
    End Function

    Public Function read(ByVal nByte As Integer) As Byte()

        stopRead = False
        ' Timer1.Enabled = True

        Do While (Not stopRead)
            'ClearCommError(inoutfileHandler, pError, pstat)


            'If nByte <= pstat.cbInQue Then
            'Timer1.Enabled = False
            Dim inbuff(nByte - 1) As Byte
            ReadFile(inoutfileHandler, _
                     inbuff, _
                     inbuff.Length, _
                     numReadWrite, _
                     0)

            Return inbuff
            ' End If
            'Threading.Thread.Sleep(100)
            ' Application.DoEvents()

        Loop
        'Throw New ApplicationException("Read timeout.")
        'Return Nothing
    End Function

    Public Sub write(ByVal msg() As Byte)
        WriteFile(inoutfileHandler, _
                  msg, _
                  msg.Length, _
                  numReadWrite, _
                  0)
    End Sub


    Public Sub closePort()
        CloseHandle(inoutfileHandler)
    End Sub

    Public Sub clsPort()
        Dim intN As Integer = GetReadBuffer()
        If intN <> 0 Then
            read(intN)
        End If
    End Sub


    Public Sub New()
        Timer1 = New System.Windows.Forms.Timer
        Timer1.Enabled = False
        Timer1.Interval = intReadTimeOut
    End Sub

    Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        stopRead = True
        Timer1.Enabled = False

    End Sub
End Class

⌨️ 快捷键说明

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