How to send Text and hex codes to serial VFD display [VB.NET]
-
01-07-2021 - |
Question
I want to program a vfd display to use with vb.net Currently, I'm only able to send text to the vfd display, however it's still not in correct format. I couldn't figure it out, so I will include the programming manual.
Things that I want to do:
- Since there are 2 lines, I want to show different data in those 2 lines.
- I should be able to clear the display.
- If the text is longer than the display (20 lines), it should start scrolling. Thats all for now, I hope someone could help me.
Here's the code that I used:
Dim sp As New SerialPort()
sp.PortName = "COM1"
sp.BaudRate = 9600
sp.Parity = Parity.None
sp.DataBits = 8
sp.StopBits = StopBits.One
sp.Open()
sp.WriteLine(TextBox1.Text)
sp.WriteLine(TextBox2.Text)
sp.Close()
sp.Dispose()
sp = Nothing
Solution
To send binary data, it's best to use the overload of the SerialPort.Write
method which takes a byte array. If you send characters or a string, you will have to deal with character encoding which annoying at best and will fail at worst. Sometimes when you encode a numeric value to a character and then decode it back to a numeric value, the decoded value won't match the original value. Therefore, the safest and easiest way is to send the byte values as a byte array. For instance:
Dim bytes() As Byte = { &H00, &H20, &HFF }
sp.Write(bytes, 0, bytes.Length)
Or, if you want to load each byte by hexadecimal value individually:
Dim bytes(3) As Byte
byte(0) = &H00
byte(1) = &H20
byte(2) = &HFF
sp.Write(bytes, 0, bytes.Length)
Or, if you just want to send a single byte by its hexadecimal value:
sp.Write(New Byte() { &H20 }, 0, 1)