Page 1 of 1

[Solved] Help with sending Hex to serial port

Posted: Friday 19th February 2021 3:33pm
by AndyGable
Hi all,

I have managed to get a small App to talk to a LCD Display and show "Hello World!" with no program and recently the LCD manufacturer
has sent over a list of commands that the screen supports.

I am hoping to send to the screen the Clear command (this is HEX 0C) how would I sent this to the screen?

I am using

Code: Select all

Write #SerialPort1, "Hello World!"
to send the string to the display.

So my question is how do I send the Hex command to the screen?

Re: Help with sending Hex to serial port

Posted: Saturday 20th February 2021 12:28am
by vuott
AndyGable wrote:
Friday 19th February 2021 3:33pm
I am hoping to send to the screen the Clear command (this is HEX 0C) how would I sent this to the screen?

So my question is how do I send the Hex command to the screen?
Since the hex value "0C" is and occupies one byte, you must specify it:
Write #SerialPort1, &h0C As Byte

Re: Help with sending Hex to serial port

Posted: Saturday 20th February 2021 2:30pm
by AndyGable
Would that work for the following Commands


Select output mode = 1B 3D n (n= 0x30, 0x31, 0x32)
Select display mode = 1F n (n= 0x01, 0x02, 0x03)
Initialize the display = 1B 40
Move Cursor Up 1F 0A
Move to the Right most = 1F 0D
Bottom most position = 1F 42
Move to given location = 1F 24 X Y (1<= X, 1<= y <2)
Turn cursor on or off = 1F 43 n (n= 0,1)

Thanks for the advice so far on this project

Re: Help with sending Hex to serial port

Posted: Saturday 20th February 2021 2:42pm
by vuott
AndyGable wrote:
Saturday 20th February 2021 2:30pm
Would that work for the following Commands
......
Thanks for the advice so far on this project
Sorry, I didn't understand if you solved it.

Re: Help with sending Hex to serial port

Posted: Saturday 20th February 2021 2:48pm
by AndyGable
vuott wrote:
Saturday 20th February 2021 2:42pm
AndyGable wrote:
Saturday 20th February 2021 2:30pm
Would that work for the following Commands
......
Thanks for the advice so far on this project
Sorry, I didn't understand if you solved it.
Sorry Vuott
I have the Clear command working but you said this was only a byte so do I send the command "1F 0D" (for example as it has more to it)

does it still work with the Write #SerialPort1, &h0C As Byte Code?

Re: Help with sending Hex to serial port

Posted: Saturday 20th February 2021 3:28pm
by vuott
AndyGable wrote:
Saturday 20th February 2021 2:48pm
but you said this was only a byte so do I send the command "1F 0D"
...obviously the problem is whether you have to send one byte at a time, or whether those two bytes represent a 16-bit value (in Gambas it is a "Short" data type).
In the first case you will have to repeat the "Write ... As Byte " function twice (explicitly or with a loop), or you could use an array of type Byte[], as in the following example:
Public Sub ........()

  Dim bb As Byte[] = [&1F, &0D]

  .........

  bb.Write(SerialPort1, 0, bb.Count)

......

Re: Help with sending Hex to serial port

Posted: Saturday 20th February 2021 3:34pm
by PJBlack
maybe like so ...
Private sptCom1 As SerialPort
Private _Init As Boolean = False

Public Function Com1Init()

    sptCom1 = New ServerSocket
    sptCom1.PortName = "/dev/ttyS0"
    sptCom1.Speed = 115200
    sptCom1.Parity = sptCom1.None
    sptCom1.DataBits = 8
    sptCom1.StopBits = 1
    sptCom1.FlowControl = sptCom1.None
    sptCom1.Open
    _Init = True

End

Public Sub ExtDisplayInit(OutputMode As Byte, DisplayMode As Byte)

    If Not _Init Then Com1Init()

    Dim strOutput As String = Chr(&H1B) & Chr(&H3D) & OutputMode
    Dim strDisplay As String = Chr(&H1F) & DisplayMode
    Dim strInit As String = Chr(&H1B) & Chr(&H40)

    sptCom1.Begin

    Write #sptCom1, strOutput & strDisplay & strInit

    sptCom1.Send

Catch
    Debug Error.Code, Error.Text

End

Public Sub ExtDisplayCursorUp()

    If Not _Init Then Com1Init()

    Dim strOutput As String = Chr(&H1F) & Chr(&H0A)

    sptCom1.Begin

    Write #sptCom1, strOutput

    sptCom1.Send

Catch
    Debug Error.Code, Error.Text

End


Re: Help with sending Hex to serial port

Posted: Saturday 20th February 2021 3:39pm
by vuott
PJBlack wrote:
Saturday 20th February 2021 3:34pm
maybe like so ...
......
    Dim strOutput As String = Chr(&H1B) & Chr(&H3D) & OutputMode
    Dim strDisplay As String = Chr(&H1F) & DisplayMode
    Dim strInit As String = Chr(&H1B) & Chr(&H40)

   ........

    Write #sptCom1, strOutput & strDisplay & strInit
Yes, perfect, if he prefers to use ASCII characters (as the character representation of a numeric value).
Theoretically, the substance does not change: they are always values that each occupy 8 bits (1 byte) of memory (in C is "char " datatype).

Re: Help with sending Hex to serial port

Posted: Saturday 20th February 2021 3:50pm
by PJBlack
vuott wrote:
Saturday 20th February 2021 3:39pm
Theoretically, the substance does not change: they are always values that each occupy 8 bits (1 byte) of memory.
yes ...

Write #stream, bValue as byte
Write #stream, bValue as byte
Write #stream, bValue as byte
Write #stream, bValue as byte
Write #stream, bValue as byte
Write #stream, bValue as byte

is the same like

sString = bValue & bValue & bValue & bValue & bValue & bValue

write #stream, sString

but second one is much clearer (to me) and you con put it in a sub like

public sub outputserial (value as string)
write #stream,value
end

and wherre you need you can

outputserial(bValue & bValue & bValue & bValue & bValue & bValue)
outputserial(bdifferentValue)

Re: Help with sending Hex to serial port

Posted: Saturday 20th February 2021 4:09pm
by AndyGable
To all,

Thank you for the advice on this I have managed to create a small list of commands like this

Code: Select all

 Public strInit As String = Chr(&H1B) & Chr(&H40)
and I can send them with in one send command now.

I can clear the screen print the text and return the cursor to the home position (never could get that one working on Windows)

Thank-you all for the help I can now start to work on getting the app actually doing Something