[Solved] Help with sending Hex to serial port

Post

Posted
Rating:
#1 (In Topic #588)
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’
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

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?
Online now: No Back to the top

Post

Posted
Rating:
#2
Regular
vuott is in the usergroup ‘Regular’

AndyGable said

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:

Code (gambas)

  1. Write #SerialPort1, &h0C As Byte

Europaeus sum !

<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Online now: No Back to the top

Post

Posted
Rating:
#3
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’
 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
Online now: No Back to the top

Post

Posted
Rating:
#4
Regular
vuott is in the usergroup ‘Regular’

AndyGable said

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 !

<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Online now: No Back to the top

Post

Posted
Rating:
#5
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’

vuott said

AndyGable said

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?
Online now: No Back to the top

Post

Posted
Rating:
#6
Regular
vuott is in the usergroup ‘Regular’

AndyGable said

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:

Code (gambas)

  1. Public Sub ........()
  2.  
  3.   Dim bb As Byte[] = [&1F, &0D]
  4.  
  5.   .........
  6.  
  7.   bb.Write(SerialPort1, 0, bb.Count)
  8.  
  9. ......

Europaeus sum !

<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Online now: No Back to the top

Post

Posted
Rating:
#7
Avatar
Enthusiast
PJBlack is in the usergroup ‘Enthusiast’
maybe like so …

Code (gambas)

  1.  
  2. Public Function Com1Init()
  3.  
  4.     sptCom1 = New ServerSocket
  5.     sptCom1.PortName = "/dev/ttyS0"
  6.     sptCom1.Speed = 115200
  7.     sptCom1.Parity = sptCom1.None
  8.     sptCom1.DataBits = 8
  9.     sptCom1.StopBits = 1
  10.     sptCom1.FlowControl = sptCom1.None
  11.     sptCom1.Open
  12.     _Init = True
  13.  
  14.  
  15. Public Sub ExtDisplayInit(OutputMode As Byte, DisplayMode As Byte)
  16.  
  17.     If Not _Init Then Com1Init()
  18.  
  19.     Dim strOutput As String = Chr(&H1B) & Chr(&H3D) & OutputMode
  20.     Dim strDisplay As String = Chr(&H1F) & DisplayMode
  21.     Dim strInit As String = Chr(&H1B) & Chr(&H40)
  22.  
  23.     sptCom1.Begin
  24.  
  25.     Write #sptCom1, strOutput & strDisplay & strInit
  26.  
  27.     sptCom1.Send
  28.  
  29.     Debug Error.Code, Error.Text
  30.  
  31.  
  32. Public Sub ExtDisplayCursorUp()
  33.  
  34.     If Not _Init Then Com1Init()
  35.  
  36.     Dim strOutput As String = Chr(&H1F) & Chr(&H0A)
  37.  
  38.     sptCom1.Begin
  39.  
  40.     Write #sptCom1, strOutput
  41.  
  42.     sptCom1.Send
  43.  
  44.     Debug Error.Code, Error.Text
  45.  
  46.  
  47.  
Online now: No Back to the top

Post

Posted
Rating:
#8
Regular
vuott is in the usergroup ‘Regular’

PJBlack said

maybe like so …

Code (gambas)

  1. ......
  2.     Dim strOutput As String = Chr(&H1B) & Chr(&H3D) & OutputMode
  3.     Dim strDisplay As String = Chr(&H1F) & DisplayMode
  4.     Dim strInit As String = Chr(&H1B) & Chr(&H40)
  5.  
  6.    ........
  7.  
  8.     Write #sptCom1, strOutput & strDisplay & strInit
  9.  

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 !

<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Online now: No Back to the top

Post

Posted
Rating:
#9
Avatar
Enthusiast
PJBlack is in the usergroup ‘Enthusiast’

vuott said

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)
Online now: No Back to the top
1 guest and 0 members have just viewed this.