News:

;) This forum is the property of Proton software developers

Main Menu

Sending JSON by Serout Command

Started by Cranes, Jul 24, 2022, 02:11 AM

Previous topic - Next topic

Cranes

Hi Guys,

I recently bought a touch screen LCD from Stoneitech which accepts JSON code. Previously, I used their displays that accepted Hex codes, which I simply transmitted to the LCD using the Serout command.

But their new LCD's now accept JSON codes, the following is an example from their manual:

Sample Code: ST>{"cmd_code":"set_text","type":"label","widget":"label","text":"Hello World"}>ET
      where: ST> is the frame header
             >ET is the frame tail

When sending the above command via a serial usb board to the PC using a serial terminal program, everything works fine. I can see the data on the LCD.

But when using the MCU (I'm using a PIC18F47K40), and using the Serout command, there is no response on the LCD. For example:

Serout PORTC.0, baud, [ST>{"cmd_code":"set_text","type":"label","widget":"label","text":"Hello World"}>ET]
     
I've also tried different combinations like adding apostrophe before the ST> and after the <ET, but still won't work.

Thanks. Any help will be much appreciated.

Cheers,

Allan



shantanu@india

What terminal program did you use in PC... did it add CR+LF at the end? PC serial emulators often do.
Best way to debug is to use a usb/serial converter and visualize your PIC output in the PC.
Regards
Shantanu

Cranes

Hi Shantanu,

Thanks for your quick reply.

I'm using the sscom32E terminal program which came with the LCD. Then I'm using a USB-TTL serial converter to connect the LCD to the PC.

In this setup, I can send the json commands and get the correct readings on the LCD.

But when using the MCU and sending the same command using Serout, nothing happens on the LCD.

Is the command format I sent earlier correct? Or, does it need to be changed?

Thanks again.

Cheers,

Allan


Cranes

When I use the following command:

     Serout PORTC.0, baud, [ST>{"cmd_code":"set_text","type":"label","widget":"label","text":"Hello
     World"}>ET]

An error message, "Variable ST Not Found" comes up.

Then I added apostrophes to the "ST>{" and "}<ET" as follows:

     Serout PORTC.0, baud, ["ST>{","cmd_code":"set_text","type":"label","widget":"label","text":"Hello
     World","}>ET"]

Then the error message, "Value Expected!" comes up.

I also tried enclosing the ":" with apostrophes but still nothing happens.

Thanks.


shantanu@india

Serout PORTC.0, baud, ["ST>{cmd_code:set_text,type:label,widget:label,text:Hello
     World}>ET"]
Try the above... passing the entire json body as a single string
Regards
Shantanu

shantanu@india

If that doesn't work then add the individual quote symbols inside the json placeholders keeping the external quotes intact
Regards
Shantanu

Cranes

Hi Shantanu,

Thanks for your reply.

Test #1:
Serout PORTC.0, baud, ["ST>{cmd_code:set_text,type:label,widget:label,text:Hello World}>ET"]
Result: No response from LCD.

Test #2:
Serout PORTC.0, baud, ["ST>{","cmd_code",":","set_text","type",":","label","widget",":",
                      "label","text",":","Hello World","}>ET"]
Result: No response from LCD.

Test #3:
Serout PORTC.0, baud, ["ST>{"cmd_code",":","set_text","type",":","label","widget",":",
                      "label","text",":","Hello World"}>ET"]
Result: Error - comma missing or command terminated incorrectly.


Giuseppe

Have you tried adding commas on too "ST>"........

Cranes

Hi Giuseppe,

Yes, I tried adding commas too, just like the code on Test #2 that I sent previously. Still didn't work...

Thanks.

shantanu@india

I don't think ST and ET are plain ASCII strings.These are STX(0x02) and ETX(0x03)... Start of text and End of text.
Have you tried?
Regards
Shantanu

Cranes

Hi Shantanu,

I just tried the STX(0x02) and ETX(0x03),but still didn't work.

I went back to the serial terminal using the USB to TTL serial converter board. This time I converted the Json code ST<{....} >ET to Hex.

Then I sent the Hex codes to the LCD via the serial converter board and it worked ok.

But when sending the same Hex codes from the MCU using Serout command, it doesn't work.

Thanks.

Frizie

Is your baudrate OK?
Also attention that it is not inverted.
Ohm sweet Ohm | www.picbasic.nl

Cranes

Hi Frizie,

Yes baud rate of the mcu and lcd are the same at 38400.

I did try both inverted and non-inverted settings.

Still no luck...

Gabi

I would try like this for starters:

$define STR_HEAD_CMD    "ST<{"
$define STR_END            "}>ET"

Dim cmd_code[12]    As Byte Heap    = 34,"cmd_code",34,58,0     ;   "cmd_code":
Dim set_text[12]    As Byte Heap    = 34,"set_text",34,44,0     ;   "set_text",
Dim type[8]         As Byte Heap    = 34,"type",34,58,0         ;   "type":
Dim _label[9]       As Byte Heap    = 34,"label",34,44,0        ;   "label",
Dim widget[10]      As Byte Heap    = 34,"widget",34,58,0       ;   "widget":
Dim text[8]         As Byte Heap    = 34,"text",34,58,0         ;   "text":
Dim helloworld[14]  As Byte Heap    = 34,"Hello World",34,0     ;   "Hello World"


SENDIT:
   HRSOutLn STR_HEAD_CMD , Str cmd_code , Str set_text, Str type, Str _label, Str widget, Str _label, Str text, Str helloworld, STR_END
 
GL & 73
YO4WM

shantanu@india

Quote from: Cranes on Jul 24, 2022, 08:47 AMHi Shantanu,

I just tried the STX(0x02) and ETX(0x03),but still didn't work.

I went back to the serial terminal using the USB to TTL serial converter board. This time I converted the Json code ST<{....} >ET to Hex.

Then I sent the Hex codes to the LCD via the serial converter board and it worked ok.

But when sending the same Hex codes from the MCU using Serout command, it doesn't work.

Thanks.
Use the USB to TTL converter to send data from the PC to the PIC and record the received data in a interrupt driven buffer. Compare that with what you are sending from the PIC
Regards
Shantanu

Frizie

In some cases it may be necessary to give the communication port a high level at the start.
So give the communication port a high level before running SEROUT (or al low level if this is already the case).
Ohm sweet Ohm | www.picbasic.nl

Cranes

Hi Gabi,

You're code worked well, thank you so much! Now, I can continue working on our project.

Thank you too, Shantanu, Giuseppe and Frizie for your inputs and assistance.

Cheers Guys!

John Lawton

Gabi,

for completion, I see you use HRsoutLn which terminates the sent string with CR and/or LF but depending on this declare (manual page 216):

Declare Hserial1_Terminator = CRLF or LFCR or CR

Is there a default value if this is not specified, maybe it doesn't matter in this case as long as CR is sent?


top204

#18
The default for the serial "Ln" commands is a single Carriage Return (13)

A nice piece of coding gabi. However, you would be better placing the String data in flash memory to save RAM and speed up the program's operation for strings that are fixed. For example:

$define STR_HEAD_CMD "ST<{"
$define STR_END      "}>ET"
'
' Create string data in flash memory to save RAM
'
    Dim cmd_code   As Flash8 = 34, "cmd_code", 34, 58, 0    ' "cmd_code"
    Dim set_text   As Flash8 = 34, "set_text", 34, 44, 0    ' "set_text"
    Dim type       As Flash8 = 34, "type", 34, 58, 0        ' "type"
    Dim _label     As Flash8 = 34, "label", 34, 44, 0       ' "label"
    Dim widget     As Flash8 = 34, "widget", 34, 58, 0      ' "widget"
    Dim text       As Flash8 = 34, "text", 34, 58, 0        ' "text"
    Dim helloworld As Flash8 = 34, "Hello World", 34, 0     ' "Hello World"

SendIt:
   HRSOutLn STR_HEAD_CMD, cmd_code, set_text, type, _label, widget, _label, text, helloworld, STR_END

In the assembler code, you can see where it is storing the string data:

;---------------------------------------------
; USER. FLASH MEMORY TABLE DATA
cmd_code
    db 34,99,109,100
    db 95,99,111,100
    db 101,34,58,0
set_text
    db 34,115,101,116
    db 95,116,101,120
    db 116,34,44,0
type
    db 34,116,121,112
    db 101,34,58,0
_label
    db 34,108,97,98
    db 101,108,34,44
    db 0
widget
    db 34,119,105,100
    db 103,101,116,34
    db 58,0
text
    db 34,116,101,120
    db 116,34,58,0
helloworld
    db 34,72,101,108
    db 108,111,32,87
    db 111,114,108,100
    db 34,0

The Str function is now rather redundant with 18F and enhanced 14-bit core devices, because the compiler supports actual flash memory and RAM strings because of their better handling of RAM and flash memory.


Gabi

#19
Hello John

HRSOutLn was used just for the demo sending the jason string. I could not tell ref: a real hw device, I am not using such,

Though checking their library now, it seems that one needs to send the full jason string followed by a carriage return and a new line.
Just check my output hex and it terminates with a CR LF.

#define stone_Transmit(BUF, ...); Serial.println(("%s",BUF));
GL & 73
YO4WM