Author Topic: Need Visual Basic ULTRA Simple Terminal Emulator  (Read 897 times)

0 Members and 1 Guest are viewing this topic.

Offline falcomTopic starter

  • Contributor
  • Posts: 27
  • Country: us
Need Visual Basic ULTRA Simple Terminal Emulator
« on: March 19, 2023, 06:38:57 pm »
I hope you can help an old guy out. I need to write a simple terminal program that will send an receive to an external device via DB9 pins 2,3, 5. No flow control, no RTS CTS or anything else needed.

 I want to have a simple window that will display sent and received characters.

 I have very basic VB6 skills. I am hoping that with a Visual Basic example  I can get started and modify it to what I need from there. All of the examples on Github are needlessly complex for me. I dont need Telenet , just send /receive. I am talking 1980 technology.

 A VB
 Thanks
 Dave
KE4MLV

 PS
 As an extra bonus the finished .exe would be able to run on DOS (not necessary)
 
 
 

Offline adinsen

  • Regular Contributor
  • *
  • Posts: 61
  • Country: dk
Re: Need Visual Basic ULTRA Simple Terminal Emulator
« Reply #1 on: March 19, 2023, 06:45:37 pm »
PuTTY can do serial and is a reliable piece of software
 

Offline falcomTopic starter

  • Contributor
  • Posts: 27
  • Country: us
Re: Need Visual Basic ULTRA Simple Terminal Emulator
« Reply #2 on: March 19, 2023, 06:49:59 pm »
I have several terminal programs but I need to modify one to do what I need.
 

Offline golden_labels

  • Super Contributor
  • ***
  • Posts: 1286
  • Country: pl
Re: Need Visual Basic ULTRA Simple Terminal Emulator
« Reply #3 on: March 19, 2023, 07:32:57 pm »
Serial communication in VB6 was being done using the mscomm control. It seems Microsoft Common Controls is still offered. But be aware that VB6 itself is long after EOL, inadequate for current Windows versions, and components like mscomm may be so incompatible with anything in 2023 they will simply crash or do nothing, or require doing bad things™.

On top of that for simple query-answer behavior you will require one textbox for entering stuff, configured to react to the enter key for sending, and another textbox for displaying output. Do not forget to set the output textbox read-only and put cursor at the end after each update. Dirty and ugly, but I can’t think of anything simpler to do.
People imagine AI as T1000. What we got so far is glorified T9.
 

Offline Bicurico

  • Super Contributor
  • ***
  • Posts: 1745
  • Country: pt
    • VMA's Satellite Blog
Re: Need Visual Basic ULTRA Simple Terminal Emulator
« Reply #4 on: March 19, 2023, 09:22:46 pm »
VB6 is obsolete and yes, it used the MSCOMM control.

You can, however, easily use Visual Basic .Net and even get it for free included in Visual Studio 20xx Comunity Edition.

Here, you create a for example a Form1, a Button1 and a SerialPort1.

To send "PING" and hopefully receive a "PONG", you would code:

Code: [Select]
Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        SerialPort1.BaudRate = "115200"
        SerialPort1.DataBits = "8"
        SerialPort1.Handshake = "N"
        SerialPort1.StopBits = "1"
        SerialPort1.PortName = "COM1"
        SerialPort1.Open()
        SerialPort1.Write("PING")
        Dim received As String
        received = SerialPort1.ReadLine
        SerialPort1.Close()
    End Sub
End Class

Note that the tricky part of RS232 communication is knowing when to start listening and knowing when the transmission ended.

Often, some kind of protocol is used, where you know that you received everything: either you know it, because the reply to a given command has so many Bytes, or the transmission is ended with a special character (normally LF and/or CR). Alternatively, you could use a protocol where the first Byte tells you how many Bytes will follow.

My example above is the most simple one: it assumes that after sending a string, you will get a string ("line") back, which terminates with LFCR.

This has a big problem: if your device decides to not return the expected answer, your VB .Net app will wait forever for the incoming reply and basically get stuck. To handle this, you can use a TRY/CATCH exception and a TIME OUT. If the reply does not arrive in so many milliseconds, an error is produced and handled by TRY/CATCH.

This won't work well if you are expecting n Bytes but only received n-x Bytes. In this case your exception will kick in, but the device might be stuck trying to send the remaining Bytes.

As you see, implementing RS232 communication can be very easy, but when shit hits the fan, you start having all sorts of problems. For example, the SerialPort control is an asynchronous process. If you are filling for instance an array with the incoming Bytes, you cannot just use that array in another SUB, as it might be still used by the SerialPort... This is why I prefer to send/receive data in the same subroutine, instead of setting up a sub that handles the event of incoming Bytes.

Here is the mandatory example produced by ChatGPT, which I do recommend for your purpose (ChatGPT - not necessarily the example)! It is much better than browsing for examples on Google, because you can request exactly what you need.

Code: [Select]
Imports System.IO.Ports

Public Class Form1
    Private serialPort As New SerialPort()

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' Set up serial port properties
        serialPort.PortName = "COM1" ' Change to your COM port number
        serialPort.BaudRate = 9600 ' Change to your baud rate
        serialPort.DataBits = 8
        serialPort.Parity = Parity.None
        serialPort.StopBits = StopBits.One
        serialPort.Handshake = Handshake.None

        ' Open the serial port
        Try
            serialPort.Open()
        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message)
        End Try
    End Sub

    Private Sub btnSend_Click(sender As Object, e As EventArgs) Handles btnSend.Click
        Dim dataToSend As String = txtSend.Text
        If serialPort.IsOpen Then
            ' Send the string to the serial port
            serialPort.Write(dataToSend)

            ' Clear the send textbox
            txtSend.Text = ""
        Else
            MessageBox.Show("Serial port is not open")
        End If
    End Sub

    Private Sub serialPort_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles serialPort.DataReceived
        ' Read the data from the serial port
        Dim dataReceived As String = serialPort.ReadExisting()

        ' Append the received data to the receive textbox
        txtReceive.AppendText(dataReceived)
    End Sub

    Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
        ' Close the serial port
        If serialPort.IsOpen Then
            serialPort.Close()
        End If
    End Sub
End Class

Kind regards,
Vitor
« Last Edit: March 19, 2023, 09:25:41 pm by Bicurico »
 

Offline Bicurico

  • Super Contributor
  • ***
  • Posts: 1745
  • Country: pt
    • VMA's Satellite Blog
Re: Need Visual Basic ULTRA Simple Terminal Emulator
« Reply #5 on: March 19, 2023, 09:28:36 pm »
Here is a ChatGP example for the same in VB6:

Code: [Select]
Private WithEvents MSComm1 As MSComm

Private Sub Form_Load()
    Set MSComm1 = New MSComm
    With MSComm1
        .CommPort = 1 ' Change to your COM port number
        .Settings = "9600,N,8,1"
        .InputLen = 0
        .RThreshold = 1
        .SThreshold = 0
        .DTREnable = True
        .RTSEnable = True
        .Handshaking = comNone
        .NullDiscard = True
        .PortOpen = True
    End With
End Sub

Private Sub MSComm1_OnComm()
    Dim dataReceived As String
    If MSComm1.CommEvent = comEvReceive Then
        dataReceived = MSComm1.Input
        ' Append the received data to a textbox or do other processing
        Text1.Text = Text1.Text & dataReceived
    End If
End Sub

Private Sub Command1_Click()
    Dim dataToSend As String
    dataToSend = Text2.Text
    If MSComm1.PortOpen Then
        ' Send the string to the serial port
        MSComm1.Output = dataToSend
        ' Clear the send textbox
        Text2.Text = ""
    Else
        MsgBox "Serial port is not open"
    End If
End Sub

Private Sub Form_Unload(Cancel As Integer)
    MSComm1.PortOpen = False
    Set MSComm1 = Nothing
End Sub


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf