Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
pneumus opened this issue Jun 20, 2024 · 0 comments
Open

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

pneumus opened this issue Jun 20, 2024 · 0 comments

Comments

@pneumus
Copy link

pneumus commented Jun 20, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant