lineterm.vb

来自「Base Class Library for RS232 Communicati」· VB 代码 · 共 67 行

VB
67
字号
Imports JH.CommBase

Public Class LineTerm
    Inherits CommLine

    Public Sub SendCommand(ByVal s As String)
        Send(s)
    End Sub

    Public Sub TransactCommand(ByVal s As String)
        Dim r As String
        r = Transact(s)
        Console.WriteLine("RESPONSE: " + r)
        Prompt()
    End Sub

    Public Sub Prompt()
        Console.WriteLine("Type string to send and press ENTER. Empty string to close com port.")
    End Sub

    Protected Overrides Function CommSettings() As CommBaseSettings
        Dim cs As New CommLineSettings()
        cs.SetStandard("COM1:", 19200, Handshake.none)
        cs.rxFilter = New ASCII() {ASCII.LF, ASCII.SOH}
        cs.rxTerminator = ASCII.CR
        cs.txTerminator = New ASCII() {ASCII.CR}
        Setup(cs)
        Return cs
    End Function

    Protected Overrides Sub OnRxLine(ByVal s As String)
        Console.WriteLine("RECEIVED: " + s)
        Prompt()
    End Sub

    Protected Overrides Sub OnTxDone()
        Console.WriteLine("TRANSMISSION COMPLETE")
        Prompt()
    End Sub

End Class
Module Module1

    Sub Main()
        Dim t As New LineTerm()
        Dim c As String
        Console.WriteLine("Press ENTER to open com port")
        Console.ReadLine()
        If t.Open() Then
            Console.WriteLine("COM PORT OPEN")
            t.Prompt()
            While True
                c = Console.ReadLine().Trim
                If c = "" Then Exit While
                t.SendCommand(c)
                't.TransactCommand(c)
            End While
            t.Close()
        End If
        Console.WriteLine("COM PORT CLOSED")
        Console.WriteLine("Press ENTER to close program.")
        Console.ReadLine()
    End Sub

End Module

⌨️ 快捷键说明

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