Python Forum

Full Version: Problem with pexpect.exception.TimeOUT
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello ,
I'm trying to write a simple python code that scan bluetooth devices
once evey 10 seconds

but I get this error after 30 seconds - when he find deviced and when he doesn't

why ?

this is the code
import threading
import pexpect
import datetime
import time
from datetime import datetime, timedelta


child = pexpect.spawn("bluetoothctl")

bdaddrs = []

def Scan():
    #TS = datetime.now().strftime('%d-%m-%y %H-%M-%S')
    #after_20_sec = TS + timedelta(seconds = 20)
    CurrentTime=datetime.now()
    EndTime=CurrentTime+timedelta(seconds=10) 
   # delta = (StartDay - EndDay).seconds
    #threading.Timer(20,Scan).start()
    #now = datetime.now()
    #timestamp = datetime.timestamp(now)
    print ('Scan Start at     ' , str(CurrentTime))
    print ('Scan will stop at ' , str(EndTime))
    #print ('delta is ' , str(delta))
    child.send("scan on\n")
    while (CurrentTime < EndTime):
        try:
            
            child.expect("Device (([0-9A-Fa-f]{2}:){5}([0-9A-Fa-f]{2}))")       # child.expect("[NEW]", timeout=None)
            bdaddr = child.match.group(1)      
            if bdaddr not in bdaddrs:
                bdaddrs.append(bdaddr)
            #results.write(bdaddr+"\n")
                print (str(datetime.now()), "----->" , bdaddr) 
        except KeyboardInterrupt:
           child.close()
        CurrentTime=datetime.now()
    EndTS = datetime.now().strftime('%d-%m-%y %H-%M-%S')
    print ('end time of scanning is ' ,str(EndTS))
   # else:
       # child.send("scan off\n")
        
    
while True:
    print('this is before the code running')
    Scan()
    print('this is after the code running')
    child.send("scan off\n")
    print(bdaddrs)
    print('wait 10 seconds')
    time.sleep(10)
    bdaddrs.clear( 
fell free to change \ modify - I want to know what I did wrong

this is the output :
this is before the code running
Scan Start at      2021-04-12 18:23:12.390305
Scan will stop at  2021-04-12 18:23:22.390305
2021-04-12 18:23:20.371978 -----> b'FF:77:13:12:AA:AC'
Traceback (most recent call last):
  File "Documents/Scan_V2.py", line 45, in <module>
    Scan()
  File "Documents/Scan_V2.py", line 28, in Scan
    child.expect("Device (([0-9A-Fa-f]{2}:){5}([0-9A-Fa-f]{2}))")       # child.expect("[NEW]", timeout=None)
  File "/home/pi/.local/lib/python3.7/site-packages/pexpect/spawnbase.py", line 344, in expect
    timeout, searchwindowsize, async_)
  File "/home/pi/.local/lib/python3.7/site-packages/pexpect/spawnbase.py", line 372, in expect_list
    return exp.expect_loop(timeout)
  File "/home/pi/.local/lib/python3.7/site-packages/pexpect/expect.py", line 181, in expect_loop
    return self.timeout(e)
  File "/home/pi/.local/lib/python3.7/site-packages/pexpect/expect.py", line 144, in timeout
    raise exc
pexpect.exceptions.TIMEOUT: Timeout exceeded.
<pexpect.pty_spawn.spawn object at 0xb6788c50>
command: /usr/bin/bluetoothctl
args: ['/usr/bin/bluetoothctl']
buffer (last 100 chars): b'm# \r\x1b[K  ff 12 01 6f ff 65 2e 63 6f 11 69 6c                 \r\n\x1b[0;94m[bluetooth]\x1b[0m# '
before (last 100 chars): b'm# \r\x1b[K  ff 12 01 6f ff 65 2e 63 6f 11 69 6c                 \r\n\x1b[0;94m[bluetooth]\x1b[0m# '
after: <class 'pexpect.exceptions.TIMEOUT'>
match: None
match_index: None
exitstatus: None
flag_eof: False
pid: 13680
child_fd: 5
closed: False
timeout: 30
delimiter: <class 'pexpect.exceptions.EOF'>
logfile: None
logfile_read: None
logfile_send: None
maxread: 2000
ignorecase: False
searchwindowsize: None
delaybeforesend: 0.05
delayafterclose: 0.1
delayafterterminate: 0.1
searcher: searcher_re:
    0: re.compile(b'Device (([0-9A-Fa-f]{2}:){5}([0-9A-Fa-f]{2}))')