Black Lives Matter - Action and Equality. ... Adafruit is open and shipping.
0

Serial input type conversion for a Nextion display
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Serial input type conversion for a Nextion display

by rdevries on Mon Jun 22, 2020 9:16 pm

Hello,
I am trying to read a Nextion display on a second serial port using a M4 Express.
When I use the Nextion simulator and a button is pressed I get
65 00 10 01 FF FF FF
When the M4 prints the received data to a terminal I get
b'e\x00\x10\x01\xff\xff\xff' (understanding that \xx represents hex characters)

If I try to convert that data to a string I get
e ╔ÿÿÿ when I use
Code: Select all | TOGGLE FULL SIZE
data_string = ''.join([chr(b) for b in data])


What is the best way and how to work with this received data?
Working with a string is a bit difficult since I can't work with none printable characters.

Thank you,

rdevries
 
Posts: 20
Joined: Wed Mar 02, 2011 12:04 am
Location: On. Canada

Re: Serial input type conversion for a Nextion display

by siddacious on Mon Jun 22, 2020 9:39 pm

How you want to handle the data will depend on the encoding scheme being used. You will have to consult the documentation for the display to determine that.

For experimentation, I would reproduce the same bytes in a bytearray and play with that in a CP or CPython REPL:
Code: Select all | TOGGLE FULL SIZE
>>> ba = bytearray([0x65, 0x00, 0x10, 0x01, 0xFF, 0xFF, 0xFF])
>>> ba
bytearray(b'e\x00\x10\x01\xff\xff\xff')
>>> [chr(b) for b in ba]
['e', '\x00', '\x10', '\x01', 'ÿ', 'ÿ', 'ÿ']
>>> ba.decode('latin_1')
'e\x00\x10\x01ÿÿÿ'
>>> ba.decode('iso8859_2')
'e\x00\x10\x01˙˙˙'
>>> struct.unpack_from(">bHH",ba)
(101, 16, 511)
>>> struct.unpack_from("<bHH",ba)
(101, 4096, 65281)
>>> struct.unpack_from("<HbH",ba)
(101, 16, 65281)
>>> struct.unpack_from("<hBh",ba)
(101, 16, -255)


There are a basically infinite number of ways of interpreting a group of bytes so you'll need a hint about where to get started.

For text alone there is a multitude of encoding schemes:

https://docs.python.org/3.1/library/cod ... -encodings

Similarly pack/unpack provides a range of endianness and data types to choose from:
https://docs.python.org/3/library/struc ... characters

siddacious
 
Posts: 271
Joined: Fri Apr 21, 2017 3:09 pm

Re: Serial input type conversion for a Nextion display

by rdevries on Mon Jun 22, 2020 10:41 pm

The Nextion display just sends plain ASCII data.
From their instructions
[*]ie: decimal: 255 or hex: 0xFF or ansichar: ÿ or binary: 11111111
[*]ie byte ndt[3] = {255,255,255}; write(ndt,3); or print(“\xFF\xFF\xFF”); or print(“ÿÿÿ”)
[*]All instructions and parameters are in ASCII
[*]All instructions are in lowercase letters

So when a button is pressed they send
e\x00\x10\x01ÿÿÿ (sorry I presented it as hex 65 00 10 01 FF FF FF which they don't send)

For your example, I entered it in the REPL
>>> ba = bytearray([0x65, 0x00, 0x10, 0x01, 0xFF, 0xFF, 0xFF])
>>> ba
bytearray(b'e\x00\x10\x01\xff\xff\xff')

I tried to enter
>>> ba.decode('latin_1')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'bytearray' object has no attribute 'decode'

So if the received data is kept in a bytearray that might be the easiest way to keep it, then I can just compare the different positions.

0x65 0x00 0x01 0x01 0xFF 0xFF 0xFF (data: Page 0, Component 1, Pressed, termination 0xFF 0xFF 0xFF)

ba[0] - 0x65 (101) is a Touch Event (7 bytes) - Returned when Touch occurs and component’s
ba[1] - 0x00 is page number,
ba[2] - 0x01 is component ID,
ba[3] - 0x01 is event (0x01 Press and 0x00 Release)
ba[4] - 0xff
ba[5] - 0xff
ba[6] - 0xff

Thank you very much for your help.

rdevries
 
Posts: 20
Joined: Wed Mar 02, 2011 12:04 am
Location: On. Canada

Please be positive and constructive with your questions and comments.