OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Python ptyprocess does not read last two lines

  • Thread starter Thread starter sonus89
  • Start date Start date
S

sonus89

Guest
Code:
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.

Code:
    @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:

Code:
[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:

Code:
[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.
<pre><code>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.
</code></pre>
<pre><code> @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)
</code></pre>
<p>This is the response of the method:</p>
<pre><code>[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][
</code></pre>
<p>This should be the correct response:</p>
<pre><code>[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.
</code></pre>
<p>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.</p>
 

Latest posts

Top