Skip to content

ptyprocess.read() does not read last two lines #78

@pneumus

Description

@pneumus
I can see what's happening in the real terminal with "OK" in the end of the response.
But the read method does not return the last two lines.
    @staticmethod
    def communicate(write_uuid, read_uuid, data):
        
        # Requires a writable and a readable GATT uuid and data to be written
        # The method returns the data retrieved from the readable GATT uuid
        
        hex_string = ''
        for char in data:
            hex_string += hex(ord(char)) + ' '

        timeout = .1
        read_process = ptyprocess.PtyProcessUnicode.spawn(['/bin/bash'])
        commands = ['bluetoothctl', 'menu gatt', f'select-attribute {read_uuid}', 'notify on']
        for command in commands:
            read_process.write(command + '\n')
            time.sleep(timeout)
            read_process.read(1)

        write_process = ptyprocess.PtyProcessUnicode.spawn(['/bin/bash'])
        commands = f'bluetoothctl <<EOF\nmenu gatt\nselect-attribute {write_uuid}\nwrite "{hex_string}"\nEOF\n'
        write_process.write(commands)
        time.sleep(timeout)
        write_process.read(1)
        time.sleep(timeout)

        hex_values = []
        pattern = re.compile(r'\b[0-9a-fA-F]{2}\b')
        lines = read_process.read(size=10000).splitlines()
        for line in lines:
            print(line)

This is the response of the method:

[CHG] Attribute /org/bluez/hci0/dev_CE_EE_A8_9B_17_AC/service000b/char000e Value:
[Device:/service000b/char000e]#                                              
  30 2e 30 2c 20 30 2e 30 2c 20 30 2e 30 2c 20 30  0.0, 0.0, 0.0, 0
[Device:/service000b/char000e]#                                              
  2e 30 2c 20 30 2e 30 2c 20 30 2e 30 2c 20 30 2e  .0, 0.0, 0.0, 0.
[Device:/service000b/char000e]#                                              
  30 2c 20 30 2e 30 2c 20 30 2e 30 2c 20 30 2e 30  0, 0.0, 0.0, 0.0
[Device:/service000b/char000e]#                                              
  2c 20 30 2e 30 2c 20 30 2e 30 2c 20 30 2e 30 2c  , 0.0, 0.0, 0.0,
[Device:/service000b/char000e]�[

This should be the correct response:

[Device:/service000b/char000e]# [CHG] Attribute /org/bluez/hci0/dev_CE_EE_A8_9B_17_AC/service000b/char000e Value:
[Device:/service000b/char000e]#   30 2e 30 2c 20 30 2e 30 2c 20 30 2e 30 2c 20 30  0.0, 0.0, 0.0, 0
[Device:/service000b/char000e]#   2e 30 2c 20 30 2e 30 2c 20 30 2e 30 2c 20 30 2e  .0, 0.0, 0.0, 0.
[Device:/service000b/char000e]#   30 2c 20 30 2e 30 2c 20 30 2e 30 2c 20 30 2e 30  0, 0.0, 0.0, 0.0
[Device:/service000b/char000e]#   2c 20 30 2e 30 2c 20 30 2e 30 2c 20 30 2e 30 2c  , 0.0, 0.0, 0.0,
[Device:/service000b/char000e]#   20 30 2e 30 2c 20 30 2e 30 2c 20 30 2e 30 2c 20   0.0, 0.0, 0.0, 
[Device:/service000b/char000e]#   30 2e 30 2c 20 30 2e 30 20 4f 4b 0a              0.0, 0.0 OK.   

As you can see we have 18 values and an OK at the end of the correct response.
While we only have 13 values without OK in the read() method's response.
Also: The read method returns an ESC character for some reason.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions