Andrea Covelli
Andrea Covelli

Reputation: 23

python3 sending serial data to Nextion Display

I'm trying to control a Nextion Display via a Python3 script.

Using Windows Terminal I'm able to control it.

For example to change the text of a control I send the following:

t0.txt="test" followed by 3 time 0xFF via "send hex"

My Python3 script using PySerial is the following:

import serial
s = serial.Serial(port='COM5',baudrate=9600)
escape='\xff'.encode('iso-8859-1')
test=b't0.txt="test"'
s.write(test)
s.write(escape)
s.write(escape)
s.write(escape)
s.close()

but it is not working.

Any ideas?

Thank you so much

Upvotes: 1

Views: 5035

Answers (3)

Marek Poliaček
Marek Poliaček

Reputation: 61

Read bytes from nextion EEPROM on raspberry pi3 on gpiopins.

#import time
import serial

ser = serial.Serial(
    port='/dev/ttyS0',
    baudrate =9600,           
    parity=serial.PARITY_NONE,
    stopbits=serial.STOPBITS_ONE,
    bytesize=serial.EIGHTBITS,
    timeout=1)
commandToSend = b'rept 0,32\xff\xff\xff' #read 32 bytes of hex data from EEPROM to UART, start address is 0
while True:
    ser.write(commandToSend)
    #time.sleep(0.5)
    x=ser.readline()    
    print (x)

Upvotes: 1

Achmad Chaidir
Achmad Chaidir

Reputation: 1

import time
import serial

ser = serial.Serial(
  port='/dev/ttyAMA0',
  baudrate = 9600,
  parity=serial.PARITY_NONE,
  stopbits=serial.STOPBITS_ONE,
  bytesize=serial.EIGHTBITS,
  timeout=1
)

while 1:
  EndCom = "\xff\xff\xff"
  write('t0.txt="Hello World"'+EndCom)

Upvotes: 0

Ullas
Ullas

Reputation: 1

You may try the following code below:

import serial
import time
import struct

ser = serial.Serial("/dev/ttyAMA0")
print ser
time.sleep(1)
i=1
k=struct.pack('B', 0xff)
while True:
    ser.write(b"n0.val=")
    ser.write(str(i))
    ser.write(k)
    ser.write(k)
    ser.write(k)
    print " NEXT"
    time.sleep(1)
    i=i+1

Upvotes: 0

Related Questions