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

📄 crs232.vb

📁 广西百色247台电视发射机监控源代码.已经过实践
💻 VB
📖 第 1 页 / 共 4 页
字号:
                        Throw New ApplicationException("Write command error")
                    Else
                        '// Check Tx results
                        If GetOverlappedResult(mhRS, 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			:		21/09/2001 - 11:41:17
        '
        '												*Parameters Info*
        '		Bytes2Read		:		Bytes to read from port
        '		Returns					:		Number of readed chars
        '
        '		Notes				:
        '===================================================
        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 mhRS.ToInt32 <= 0 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(mhRS, 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
                        Sleep(500)
                        iRc = WaitForSingleObject(muOvlR.hEvent, miTimeout)
                        Select Case iRc
                            Case WAIT_OBJECT_0
                                '// Some data received...
                                'Sleep(2000)
                                If GetOverlappedResult(mhRS, muOvlR, iReadChars, 0) = 0 Then
                                    Throw New ApplicationException("Read pending error.")
                                Else
                                    ReDim Preserve mabtRxBuf(iReadChars - 1)
                                    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
    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)
        End Get
    End Property
    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 mhRS.ToInt32 > 0 Then
            PurgeComm(mhRS, PURGE_RXCLEAR)
        End If
    End Sub
    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 mhRS.ToInt32 > 0 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
        '===================================================
        '
        '		Description:		Set/Resets DTR Line		
        '		Created			:		21/09/2001 - 11:45:34
        '
        '												*Parameters Info*
        '
        '		Notes				:
        '===================================================
        Set(ByVal Value As Boolean)
            If mhRS.ToInt32 > 0 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
        '===================================================
        '
        '	Description	:	Gets Modem status
        '	Created			:	28/02/2002 - 8:58:04
        '
        '						*Parameters Info*
        '
        '	Notes				:
        '===================================================
        Get
            If mhRS.ToInt32 <= 0 Then
                Throw New ApplicationException("Please initialize and open port before using this method")
            Else
                '// Retrieve modem status
                Dim lpModemStatus As Int32
                If Not GetCommModemStatus(mhRS, lpModemStatus) Then
                    Throw New ApplicationException("Unable to get modem status")
                Else
                    Return CType(lpModemStatus, ModemStatusBits)
                End If
            End If
        End Get
    End Property
    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 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 Sub EnableEvents()
        '===================================================
        '
        '	Description	:	Enables monitoring of incoming events
        '	Created			:	15/07/2003 - 12:00:56
        '
        '						*Parameters Info*
        '
        '	Notes				:
        '===================================================
        If mhRS.ToInt32 <= 0 Then
            Throw New ApplicationException("Please initialize and open port before using this method")
        Else
            If moEvents Is Nothing Then
                mbEnableEvents = True
                moEvents = New Thread(AddressOf pEventsWatcher)
                moEvents.IsBackground = True
                moEvents.Start()
            End If
        End If
    End Sub
    Public Sub DisableEvents()
        '===================================================
        '
        '	Description	:	Disables monitoring of incoming events
        '	Created			:	15/07/2003 - 12:00:56
        '
        '						*Parameters Info*
        '
        '	Notes				:
        '===================================================
        If mbEnableEvents = True Then
            SyncLock Me
                mbEnableEvents = False     '// This should kill the thread
            End SyncLock
            '// Let WaitCommEvent exit...
            If muOvlE.hEvent.ToInt32 <> 0 Then SetEvent(muOvlE.hEvent)
            moEvents = Nothing
        End If
    End Sub
    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 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

⌨️ 快捷键说明

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