[Solved] Help with sending Hex to serial port

Post your Gambas programming questions here.
Post Reply
AndyGable
Posts: 359
Joined: Wednesday 2nd December 2020 12:11am
Location: Northampton, England
Contact:

[Solved] Help with sending Hex to serial port

Post 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?
Last edited by AndyGable on Saturday 20th February 2021 4:10pm, edited 1 time in total.
vuott
Posts: 262
Joined: Wednesday 5th April 2017 6:07pm
Location: European Union

Re: Help with sending Hex to serial port

Post 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
Europaeus sum !

Amare memorentes atque deflentes ad mortem silenter labimur.
AndyGable
Posts: 359
Joined: Wednesday 2nd December 2020 12:11am
Location: Northampton, England
Contact:

Re: Help with sending Hex to serial port

Post 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
vuott
Posts: 262
Joined: Wednesday 5th April 2017 6:07pm
Location: European Union

Re: Help with sending Hex to serial port

Post 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.
Europaeus sum !

Amare memorentes atque deflentes ad mortem silenter labimur.
AndyGable
Posts: 359
Joined: Wednesday 2nd December 2020 12:11am
Location: Northampton, England
Contact:

Re: Help with sending Hex to serial port

Post 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?
vuott
Posts: 262
Joined: Wednesday 5th April 2017 6:07pm
Location: European Union

Re: Help with sending Hex to serial port

Post 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)

......
Europaeus sum !

Amare memorentes atque deflentes ad mortem silenter labimur.
User avatar
PJBlack
Posts: 184
Joined: Tuesday 9th June 2020 10:26pm
Location: Florstadt, Hessen, Germany

Re: Help with sending Hex to serial port

Post 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

Last edited by PJBlack on Saturday 20th February 2021 3:42pm, edited 1 time in total.
vuott
Posts: 262
Joined: Wednesday 5th April 2017 6:07pm
Location: European Union

Re: Help with sending Hex to serial port

Post 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).
Europaeus sum !

Amare memorentes atque deflentes ad mortem silenter labimur.
User avatar
PJBlack
Posts: 184
Joined: Tuesday 9th June 2020 10:26pm
Location: Florstadt, Hessen, Germany

Re: Help with sending Hex to serial port

Post 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)
Post Reply