Python Forum
Problem with pexpect.exception.TimeOUT
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with pexpect.exception.TimeOUT
#1
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}))')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Reading and storing a line of output from pexpect child eagerissac 1 4,147 Feb-20-2024, 05:51 AM
Last Post: ayoshittu
  Use pexpect to send user input alisha17 0 1,828 May-10-2022, 02:44 AM
Last Post: alisha17
  FTp timeout except korenron 2 3,504 Feb-01-2022, 06:51 AM
Last Post: korenron
  Not able to read the text using pexpect/expect Bipinjohnson 7 3,940 Jan-10-2022, 11:21 AM
Last Post: Bipinjohnson
  Sudden Problem with pexpect gw1500se 3 2,326 Nov-19-2021, 11:21 PM
Last Post: bowlofred
  How to use pexpect in python? tiho_bg 1 1,489 Oct-30-2021, 02:50 PM
Last Post: Yoriz
  Pexpect timesout before executing whole output eagerissac 0 1,455 Jun-23-2021, 03:30 AM
Last Post: eagerissac
  pexpect startup help korenron 2 3,433 Apr-27-2021, 07:23 AM
Last Post: korenron
  Request help on pexpect rsurathu 4 5,721 Jul-19-2020, 03:35 PM
Last Post: snippsat
  SIGWINCH ignored when trying to start a screen with the pexpect module StatTark 0 1,793 Jul-06-2020, 10:12 AM
Last Post: StatTark

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020