Need Visual Basic ULTRA Simple Terminal Emulator  - Page 1
Forum section: electronics / general

author: falcom
author_rank: Contributor
timestamp: March 19, 2023, 06:38:57 pm
content: 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 VBThanksDaveKE4MLVPSAs an extra bonus the finished .exe would be able to run on DOS (not necessary)

author: adinsen
author_rank: Regular Contributor
timestamp: March 19, 2023, 06:45:37 pm
content: PuTTYcan do serial and is a reliable piece of software

author: falcom
author_rank: Contributor
timestamp: March 19, 2023, 06:49:59 pm
content: I have several terminal programs but I need to modify one to do what I need.

author: golden_labels
author_rank: Super Contributor
timestamp: March 19, 2023, 07:32:57 pm
content: Serial communication in VB6 was being done using the mscomm control. It seemsMicrosoft Common Controlsis 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 doingbad 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.

author: Bicurico
author_rank: Super Contributor
timestamp: March 19, 2023, 09:22:46 pm
content: 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 Form1Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.ClickSerialPort1.BaudRate = "115200"SerialPort1.DataBits = "8"SerialPort1.Handshake = "N"SerialPort1.StopBits = "1"SerialPort1.PortName = "COM1"SerialPort1.Open()SerialPort1.Write("PING")Dim received As Stringreceived = SerialPort1.ReadLineSerialPort1.Close()End SubEnd ClassNote 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.PortsPublic Class Form1Private serialPort As New SerialPort()Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load' Set up serial port propertiesserialPort.PortName = "COM1" ' Change to your COM port numberserialPort.BaudRate = 9600 ' Change to your baud rateserialPort.DataBits = 8serialPort.Parity = Parity.NoneserialPort.StopBits = StopBits.OneserialPort.Handshake = Handshake.None' Open the serial portTryserialPort.Open()Catch ex As ExceptionMessageBox.Show("Error: " & ex.Message)End TryEnd SubPrivate Sub btnSend_Click(sender As Object, e As EventArgs) Handles btnSend.ClickDim dataToSend As String = txtSend.TextIf serialPort.IsOpen Then' Send the string to the serial portserialPort.Write(dataToSend)' Clear the send textboxtxtSend.Text = ""ElseMessageBox.Show("Serial port is not open")End IfEnd SubPrivate Sub serialPort_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles serialPort.DataReceived' Read the data from the serial portDim dataReceived As String = serialPort.ReadExisting()' Append the received data to the receive textboxtxtReceive.AppendText(dataReceived)End SubPrivate Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing' Close the serial portIf serialPort.IsOpen ThenserialPort.Close()End IfEnd SubEnd ClassKind regards,Vitor

author: Bicurico
author_rank: Super Contributor
timestamp: March 19, 2023, 09:28:36 pm
content: Here is a ChatGP example for the same in VB6:Code:Private WithEvents MSComm1 As MSCommPrivate Sub Form_Load()Set MSComm1 = New MSCommWith 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 = TrueEnd WithEnd SubPrivate Sub MSComm1_OnComm()Dim dataReceived As StringIf MSComm1.CommEvent = comEvReceive ThendataReceived = MSComm1.Input' Append the received data to a textbox or do other processingText1.Text = Text1.Text & dataReceivedEnd IfEnd SubPrivate Sub Command1_Click()Dim dataToSend As StringdataToSend = Text2.TextIf MSComm1.PortOpen Then' Send the string to the serial portMSComm1.Output = dataToSend' Clear the send textboxText2.Text = ""ElseMsgBox "Serial port is not open"End IfEnd SubPrivate Sub Form_Unload(Cancel As Integer)MSComm1.PortOpen = FalseSet MSComm1 = NothingEnd Sub