Question Details

No question body available.

Tags

vb.net serial-port hex

Answers (1)

Accepted Answer Available
Accepted Answer
July 11, 2026 Score: 3 Rep: 25,390 Quality: Expert Completeness: 80%

If you use Encoding.ASCII.GetBytes on a "£", you get &H3F ("?" - it'll use that for anything above &H7F). So, you can get the bytes of the text to be displayed and change all occurrences of &H3F to &HBC before sending the data as long as you don't need to use "?" as "?". You can use the SerialPort.Write(Byte[], Int32, Int32) method to avoid having the data regarded as characters in the byte range 0-127.

I suggest using constants for things like the display size - it will make it much easier to change to, say, a four-line display.

Something like this:

Sub SendToDisplay(displayText As String)
    Const DISPLAYWIDTH As Integer = 20
    Const DISPLAYHEIGHT As Integer = 2

Dim CRLF As Byte() = {13, 10}

Dim blankText = New String(" ", DISPLAYWIDTH) Dim blankBytes = Encoding.ASCII.GetBytes(blankText)

If displayText.Length < DISPLAYHEIGHT DISPLAYWIDTH Then displayText = displayText.PadRight(DISPLAYHEIGHT DISPLAYWIDTH) End If

Dim displayBytes = Encoding.ASCII.GetBytes(displayText) ' Replace pound sign with &HBC for what the display uses. For i = 0 To displayBytes.Length - 1 If displayBytes(i) = &H3F Then displayBytes(i) = &HBC End If

Next

Using ser As New IO.Ports.SerialPort("COM3", 9600, IO.Ports.Parity.None, 8, IO.Ports.StopBits.One) ser.Open()

'TODO: Check if display has a code to clear it. For i = 1 To DISPLAYHEIGHT ser.Write(blankBytes, 0, 20) ser.Write(CRLF, 0, 2) Next

For i = 0 To DISPLAYHEIGHT - 1 ser.Write(displayBytes, i * DISPLAYWIDTH, DISPLAYWIDTH) ser.Write(CRLF, 0, 2) Next

ser.Close()

End Using

End Sub