I was trying to establish a serial communication between my python script and Nextion display. I want to create a custom firmware uploader, so I could use it instead of Nextion Editor.
Unfortunately I can't establish a connection to the screen(RS232 to Serial is connected properly and detected, I use default 9600 baud rate). After sending
DRAKJHSUYDGBNCJHGJKSHBDNÿÿÿ
connectÿÿÿ
ÿÿconnectÿÿÿ
It should respond properly... I found a document from Nextion that explains the Upload Protocol: https://nextion.tech/2017/12/08/nextion-hmi-upload-protocol-v1-1/
And here is my script, that produces from time to time
b'$\xff\xff\xff'
import serial
from time import sleep
ser = serial.Serial('/dev/tty.usbserial-A94RJPXT',9600,timeout=0)
ser.write(str.encode(" DRAKJHSUYDGBNCJHGJKSHBDNÿÿÿ"))
ser.write(str.encode("\n"))
ser.write(str.encode(" connectÿÿÿ"))
ser.write(str.encode("\n"))
ser.write(str.encode(" ÿÿconnectÿÿÿ"))
ser.write(str.encode("\n"))
while True:
print(ser.readline())
sleep(0.1)
WORKING CODE:
import serial
from time import sleep
ser = serial.Serial('/dev/tty.usbserial-A94RJPXT',9600,timeout=0)
ser.write("DRAKJHSUYDGBNCJHGJKSHBDNÿÿÿ".encode('iso-8859-1'))
ser.write("connectÿÿÿ".encode('iso-8859-1')) # could try some other encodings
ser.write("ÿÿconnectÿÿÿ".encode('iso-8859-1'))
while True:
data=ser.readline().decode('iso-8859-1')
if data !="":
print(data)
sleep(0.1)
If you look at this GitHub commit, you'll see that they do:
this->sendCommand("DRAKJHSUYDGBNCJHGJKSHBDNÿÿÿ");
this->sendCommand("connectÿÿÿ");
this->sendCommand("ÿÿconnectÿÿÿ");
That suggests that you do not need the spaces or newlines. If that doesn't work, you should also consider different encodings (and make your current encodings explicit):
ser.write("DRAKJHSUYDGBNCJHGJKSHBDNÿÿÿ".encode('utf-8'))
ser.write("connectÿÿÿ".encode('utf-8')) # could try some other encodings
ser.write("ÿÿconnectÿÿÿ".encode('utf-8'))