All pastes #358144 Raw Edit

NoX

public text v1 · immutable
·published 2007-02-16 02:24 UTC
rendered paste body
Imports DBComm
Public Enum ParityOptions
    EvenParity
    MarkParity
    NoParity
    OddParity
    SpaceParity
End Enum
Public Enum stopBitOptions
    oneptfiveStopBit
    oneStopBit
    twoStopBit
End Enum
Public Interface ISerial
    Sub Open(ByVal portName As String, _
             ByVal baudRate As Integer, _
             ByVal dataBits As Byte, _
             ByVal parity As parityOptions, _
             ByVal stopBits As stopBitOptions)

    Sub Open(ByVal port As Integer, _
             ByVal baudRate As Integer, _
             ByVal dataBits As Byte, _
             ByVal parity As parityOptions, _
             ByVal stopBits As stopBitOptions)

    Sub SendMessage(ByVal message As Byte())

    Event DataAvailable(ByVal sender As Object, ByVal e As EventArgs)

    Sub Close()

    Function ReadMessage( _
        ByVal byteCount As Integer) As Byte()
End Interface
Public Class Form2
    Implements ISerial
    Private WithEvents DBCOMM As New RS232
    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Open(1, "19200", 1, ParityOptions.EvenParity, stopBitOptions.oneStopBit)
    End Sub
    Public Overloads Sub Open(ByVal port As Integer, ByVal baudRate As Integer, _
    ByVal dataBits As Byte, ByVal parity As ParityOptions, ByVal stopBits As stopBitOptions) Implements ISerial.Open
        DBCOMM.CommPort = port
        Dim settings As String

        Dim parityChar As Char
        Select Case parity
            Case ParityOptions.EvenParity
                parityChar = "E"c
            Case ParityOptions.MarkParity
                parityChar = "M"c
            Case ParityOptions.NoParity
                parityChar = "N"c
            Case ParityOptions.OddParity
                parityChar = "O"c
            Case ParityOptions.SpaceParity
                parityChar = "S"c
        End Select
        Dim stopBitsNumber As String
        Select Case stopBits
            Case stopBitOptions.oneptfiveStopBit
                stopBitsNumber = "1.5"
            Case stopBitOptions.oneStopBit
                stopBitsNumber = "1"
            Case stopBitOptions.twoStopBit
                stopBitsNumber = "2"
        End Select
        'the MSCOMM Library expects its settings as
        'a string such as "9600,N,8,1"
        settings = String.Format("{0},{1},{2},{3}", _
            baudRate, parityChar, _
            dataBits, stopBitsNumber)

        DBCOMM.Settings = settings
        DBCOMM.InputLength = 5
        DBCOMM.InBufferSize = 5
        DBCOMM.InputMode = RS232.InputModes.Binary
        'DBCOMM.SendThreshold = 5
        DBCOMM.ReceiveThreshold = 5
        'DBCOMM.RThreshold = 5

        DBCOMM.PortOpen = True
    End Sub


    Public Sub SendMessage(ByVal message() As Byte) Implements ISerial.SendMessage
        DBCOMM.Output(message) ' = message
    End Sub
    Public Sub Close1() Implements ISerial.Close
    End Sub
    Public Event DataAvailable(ByVal sender As Object, ByVal e As System.EventArgs) Implements ISerial.DataAvailable
    Public Sub Open1(ByVal portName As String, ByVal baudRate As Integer, ByVal dataBits As Byte, ByVal parity As ParityOptions, ByVal stopBits As stopBitOptions) Implements ISerial.Open
    End Sub
    Public Function ReadMessage(ByVal byteCount As Integer) As Byte() Implements ISerial.ReadMessage
        Return Nothing
    End Function
End Class