Products > Programming

Need Visual Basic ULTRA Simple Terminal Emulator

(1/2) > >>

falcom:
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)
 
 

adinsen:
PuTTY can do serial and is a reliable piece of software

falcom:
I have several terminal programs but I need to modify one to do what I need.

golden_labels:
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.

Bicurico:
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: ---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

--- End code ---

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: ---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

--- End code ---

Kind regards,
Vitor

Navigation

[0] Message Index

[#] Next page

There was an error while thanking
Thanking...
Go to full version
Powered by SMFPacks Advanced Attachments Uploader Mod