1

Hi i trying to send data to nextion 2.4 display from raspberry pi , i try to do change such as t0.txt="abc" but i dont know how i do with python

i try to this code block but is not work

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"t0.txt=")
    ser.write(str(i))
    ser.write(k)
    ser.write(k)
    ser.write(k)
    print " NEXT"
    time.sleep(1)
    i=i+1`

2 Answers2

0

You're missing the quotation marks around str(i). You're sending; t0.txt=1, t0.txt=2, etc. It needs to be; t0.txt="1".

Something like this should do the trick I guess:

ser.write(b"t0.txt=")
ser.write('"')
ser.write(str(i))
ser.write('"')
ser.write(k)
ser.write(k)
ser.write(k)
Hein Andre Grønnestad
  • 6,885
  • 2
  • 31
  • 43
0

In 2023 none of these worked for me,

this works to switch pages

import struct
import serial
port = serial.Serial(
    port='/dev/ttyS0',
    baudrate =9600,
    parity=serial.PARITY_NONE)

eof = struct.pack('B', 0xff)
port.write("page 1".encode())
port.write(eof)
port.write(eof)
port.write(eof)

And this to display text, it needs pip install nextion

import asyncio
import logging
import random

from nextion import Nextion, EventType

class App:
    def __init__(self):
        self.client = Nextion('/dev/ttyS0', 9600, self.event_handler)

    # Note: async event_handler can be used only in versions 1.8.0+ (versions 1.8.0+ supports both sync and async versions)
    async def event_handler(self, type_, data):
        if type_ == EventType.STARTUP:
            print('We have booted up!')
        elif type_ == EventType.TOUCH:
            print('A button (id: %d) was touched on page %d' % (data.component_id, data.page_id))

        logging.info('Event %s data: %s', type, str(data))

        print(await self.client.get('t0.txt'))

    async def run(self):
        await self.client.connect()

        # await client.sleep()
        # await client.wakeup()

        # await client.command('sendxy=0')

        print(await self.client.get('sleep'))
        print(await self.client.get('t0.txt'))

        await self.client.set('t0.txt', 'hello')

        print('finished')

if __name__ == '__main__':
    logging.basicConfig(
        format='%(asctime)s - %(levelname)s - %(message)s',
        level=logging.DEBUG,
        handlers=[
            logging.StreamHandler()
        ])
    loop = asyncio.get_event_loop()
    app = App()
    asyncio.ensure_future(app.run())
    loop.run_forever()
user2351509
  • 51
  • 12