2014-11-11 2 views
2

UPDATE: Я решил мое решение с помощью os.system:Hcitool lescan не будет печатать в режиме реального времени файл

sensortag=0 
while sensortag != "B4:99:4C:64:33:E0": 
    #call the command and write to scan.txt file and then fill the process. 
    #loop to find if the MAC address given is available 
    os.system("hcitool lescan> scan.txt & pkill --signal SIGINT hcitool") 
    scan = open("scan.txt","r") 
    readscan = scan.read() 
    if "B4:99:4C:64:33:E0" in readscan: 
     print "SensorTag found." 
     sensortag = "B4:99:4C:64:33:E0" 

У меня есть две программы, по существу то же самое, но с двумя разными командами, на малиновый PI, работающий на Raspbian.

То, что я пытаюсь сделать, это записать оба командных вывода в файл, поэтому я могу их обработать позже.

Я озадачен тем, почему первая программа не будет работать, но вторая будет.

Первая программа имеет "Судо таймаут 5 hcitool lescan" команды, которая не работает.

import os 
import subprocess 

#r+ because the file is already there, w without the file 
myfile = open("scan.txt", "r+") 

#Reset Bluetooth interface, hci0 
os.system("sudo hciconfig hci0 down") 
os.system("sudo hciconfig hci0 up") 

#Scan for bluetooth devices 
dev = subprocess.Popen(["sudo timeout 5 hcitool lescan"], stdout=subprocess.PIPE, shell=True) 
(device, err) = dev.communicate() 

#Print bluetooth devices 
print device 

#Write the hcitool lescan output to a file 
myfile.write(device) 

#Close the file 
myfile.close() 

Вот Вторая программа у меня есть, которая работает штраф напечатав "SUDO hciconfig":

import os 
import subprocess 

#r+ because the file is already there, w without the file 
myfile = open("test.txt", "r+") 

#Reset Bluetooth interface, hci0 
os.system("sudo hciconfig hci0 down") 
os.system("sudo hciconfig hci0 up") 

#Make sure device is up 
interface = subprocess.Popen(["sudo hciconfig"], stdout=subprocess.PIPE, shell=True) 
(int, err) = interface.communicate() 

#Print hciconfig to make sure it's up 
print int 

#Write the hciconfig output to a file 
myfile.write(int) 

#Close the file 
myfile.close() 

ответ

1

Я решил мое решение, используя os.system и убийство прямо сканирования :

sensortag=0 
while sensortag != "B4:99:4C:64:33:E0": 
    #call the command and write to scan.txt file and then fill the process. 
    #loop to find if the MAC address given is available 
    os.system("hcitool lescan> scan.txt & pkill --signal SIGINT hcitool") 
    scan = open("scan.txt","r") 
    readscan = scan.read() 
    if "B4:99:4C:64:33:E0" in readscan: 
     print "SensorTag found." 
     sensortag = "B4:99:4C:64:33:E0" 
1

Проведя несколько хоу rs, работая над этим, я придумал это решение. Проблема с hcitool lescan является то, что она не возвращается, пока он не получит SIGINT, поэтому мы посылаем его один с Python:

bashCommand = "hcitool lescan" 
    process = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE) 
    time.sleep(3) 
    os.kill(process.pid, signal.SIGINT) 
    output = process.communicate()[0] 

Это для меня возвращал строку, содержащую все MAC-адресов, найденных после 3 второй поиск.

0

Используя только терминал, после команды сделал работу:

hcitool lescan > scan.txt & sleep 2 && pkill --signal SIGINT hcito 

Я просто оставить его здесь, может быть, это поможет кому-то.