yozik04 / nextion

Nextion serial client
GNU Lesser General Public License v3.0
25 stars 10 forks source link

No response from async event handler #30

Closed SHexplorer closed 8 months ago

SHexplorer commented 8 months ago

Hi, im using the exact same script like here and also get no response. But when run

import serial
    s = serial.Serial('/dev/ttyS1', 115200)
    while(True):
        res = s.read()
        print(res)

in the main function instead of the loop I'm getting this response:

b'\x01'
b'\x03'
b'\xff'
b'\xff'
b'\xff'

Switching the event_handler to sync (without await) won't work either. Any idea why the event handler don't work?

yozik04 commented 8 months ago

It is wrong to make queries to the device in the event handler. This won't work.

SHexplorer commented 8 months ago

Thats my program with the direct serial read (I'm changing the last lines out):

import asyncio
import logging

from nextion import Nextion, EventType

class App:
    def __init__(self):
        self.client = Nextion('/dev/ttyS1', 115200, 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)
    def event_handler(self, type_, data):
        print('EVENT!', 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))

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

        await self.client.command("page 1")

        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()

    #import serial
    #s = serial.Serial('/dev/ttyS1', 115200)
    #while(True):
    #    res = s.read()
    #    print(res)
yozik04 commented 8 months ago

What is the output?

SHexplorer commented 8 months ago

What is the output?

mks@mkspi:~/touchscreen-connector$ /home/mks/touchscreen-connector/pythonvenv/bin/python /home/mks/touchscreen-connector/main.py
2023-10-25 00:04:59,693 - INFO - Connecting: /dev/ttyS1, baud: 115200
2023-10-25 00:04:59,696 - INFO - Connected to serial
2023-10-25 00:04:59,697 - DEBUG - sent: b'DRAKJHSUYDGBNCJHGJKSHBDN'
2023-10-25 00:04:59,703 - DEBUG - received: b'1a'
2023-10-25 00:04:59,744 - DEBUG - sent: b'connect'
2023-10-25 00:04:59,754 - DEBUG - received: b'636f6d6f6b20322c313037332d302c544a4334383237583234335f303131432c3132352c31303230312c333842323334303134303942354133432c31363737373231362d30'
2023-10-25 00:04:59,755 - INFO - Address: 1073-0
2023-10-25 00:04:59,756 - INFO - Detected model: TJC4827X243_011C
2023-10-25 00:04:59,756 - INFO - Firmware version: 125
2023-10-25 00:04:59,756 - INFO - Serial number: 38B23401409B5A3C
2023-10-25 00:04:59,757 - INFO - Flash size: 16777216-0
2023-10-25 00:04:59,758 - DEBUG - sent: b'bkcmd=3'
2023-10-25 00:04:59,761 - DEBUG - received: b'01'
2023-10-25 00:04:59,762 - DEBUG - sent: b'get sleep'
2023-10-25 00:04:59,766 - DEBUG - received: b'7100000000'
2023-10-25 00:04:59,767 - INFO - Successfully connected to the device
2023-10-25 00:04:59,768 - DEBUG - sent: b'page 1'
2023-10-25 00:04:59,772 - DEBUG - received: b'01'
finished

And with the serial read for example:

mks@mkspi:~/touchscreen-connector$ /home/mks/touchscreen-connector/pythonvenv/bin/python /home/mks/touchscreen-connector/main.py
b'e'
b'\x01'
b'\x03'
b'\xff'
b'\xff'
b'\xff'

Python 3.11 nextion 1.8.1 pyserial-asyncio 0.6

yozik04 commented 8 months ago

And what is the problem? I see that connection and parameter read are successful.

SHexplorer commented 8 months ago

Shouldn't the event_handler tell me there was a touch event? At least that the library received a message? I pressed a button and get no response whatsoever. But when I'm reading directly from the serial port, it says

b'e'
b'\x01'
b'\x03'
b'\xff'
b'\xff'
b'\xff'

What means probably "touch event, page 1, id 3"

yozik04 commented 8 months ago

I do not see that anything was received in the log. It is either script ends before receiving an event or you forgot to set Send Component ID for required element.

yozik04 commented 8 months ago
b'e' - TOUCH event
b'\x01' - page 1
b'\x03' - component 3
b'\xff'
b'\xff'
b'\xff'

Hmm where is one more byte with touch_event

SHexplorer commented 8 months ago

I have to mention that I don't have access to the nextion source file (only the .tft file) so I can't say for sure, if they use the "send component id" method - or something special.

yozik04 commented 8 months ago

Spec tells this:

The first byte of returned data Meaning Format
0X65 Touch event return data 0X65+Page ID+Component ID+TouchEvent+End
-- -- Return this data when the touch event created by the user is pressed.
-- -- Definition of TouchEvent: Press Event 0x01, Release Event 0X00)
-- -- Instance: 0X65 0X00 0X02 0X01 0XFF 0XFF 0XFF'
-- -- Meaning: Page 0, Button 2, Press

Source: https://wiki.iteadstudio.com/Nextion_Instruction_Set

SHexplorer commented 8 months ago

Okay, I checked it with a new HMI/tft program and there it functions correctly. Your are right, there is one byte missing. Looks like they not used the send id component function in my tft file. So I have to write my own library acording to the events. Thanks you anyway. I will mark this as resolved.