yozik04/nextion

Different character encodings

Closed this issue · 9 comments

Well, it's me again, your nightmare!

This time I would like to discuss with you about the possibility to add support for multiple character encodings.

Serial connection to Nextion only supports ASCII, and so all UTF-8 characters that use 2 bytes for encoding are "lost in translation" (like é, ç, ä, etc.).
From the Nextion Editor it is possible to set a different encoding for the screen, like the ISO-8859-x Single Byte Character Set (SBCS) encoding (this PDF was enlightening to grasp the issue).

From the Python endpoint, it is necessary that the encoding is set on the same type of the screen.
In protocol.py, in the write function, if you change data = data.encode() to, for example, data = data.encode(iso-8859-1'), "special" characters are properly displayed on the screen (of course it has to be set on the same encoding from the Nextion Editor, as previously specified).

I tried to modify your library so that it is possible to specify a different encoding, in a fashion similar to what you did for the timeout with the IO_TIMEOUT global variable in client.py.
The result probably isn't in line with your coding style (or coding best practices), but I'll soon propose it as a commit to protocol.py and client.py for you to review.

Check my version =) I do not want protocol.py to be aware of encoding. It should always operate in bytes.

If you confirm my version is working I will make a release.

I'm always open to better solutions! ;)
I just tried to offload you of some work, I don't think that is nice to come here and simply ask you to add new functions.
I just tried your code and it seems to work perfectly!
Strangely though, I get this error on startup, that doesn't seem to affect normal functionality:

Wrong reply b'\x1a' to connect attempt

I was getting the same (or something similar) in my first attempts to modify your code to support different encodings.

You investigated and offered a solution. That is already good help.
Regarding wrong reply. Can you plese give me longer log with debug

Thanks for your kind words :D

Here is a more detailed log, hope it helps:

2020-04-23 12:47:29,683 - INFO - Connecting: /dev/ttyAMA0, baud: 115200
2020-04-23 12:47:29,685 - INFO - Connected to serial
2020-04-23 12:47:29,686 - DEBUG - sent: b'DRAKJHSUYDGBNCJHGJKSHBDN'
2020-04-23 12:47:29,690 - DEBUG - received: b'1a'
2020-04-23 12:47:29,728 - DEBUG - sent: b'connect'
2020-04-23 12:47:29,729 - WARNING - Wrong reply b'\x1a' to connect attempt
2020-04-23 12:47:29,730 - DEBUG - sent: b'\xff\xffconnect'
2020-04-23 12:47:29,737 - DEBUG - received: b'636f6d6f6b20312c33303631342d302c4e58343833324b3033355f303131522c3134322c36313639392c444236393043313544333232333932442c3333353534343332'
2020-04-23 12:47:29,738 - INFO - Address: 30614-0
2020-04-23 12:47:29,740 - INFO - Detected model: NX4832K035_011R
2020-04-23 12:47:29,740 - INFO - Firmware version: 142
2020-04-23 12:47:29,740 - INFO - Serial number: DB690C15D322392D
2020-04-23 12:47:29,741 - DEBUG - Flash size: 33554432
2020-04-23 12:47:29,741 - DEBUG - sent: b'bkcmd=3'
2020-04-23 12:47:29,743 - DEBUG - received: b'01'
2020-04-23 12:47:29,744 - DEBUG - sent: b'get sleep'
2020-04-23 12:47:29,746 - DEBUG - received: b'7100000000'
2020-04-23 12:47:29,747 - DEBUG - received: b'01'
2020-04-23 12:47:29,749 - INFO - Successfully connected to the device
2020-04-23 12:47:29,750 - DEBUG - sent: b'get dp'
2020-04-23 12:47:29,751 - DEBUG - received: b'7107000000'
2020-04-23 12:47:29,753 - DEBUG - received: b'01'
2020-04-23 12:47:29,754 - DEBUG - sent: b'get rtc3'
2020-04-23 12:47:29,756 - DEBUG - received: b'710c000000'

Let me know if I can do anything else

Most probably is solved now.

Yes, I can confirm you that it isn't giving me that wrong reply anymore: you can make a release ;)

BTW, I noticed that you implemented a new function to flash the firmware: I tried it but I encountered some errors, is it supposed to be ready for prime time? Can we discuss it here, or it is better to open a new issue?

New issue. I did it according to spec but not tested it yet.

Uploading is working now.

use command line

python -m nextion.console_scripts.upload_firmware -h