Python Forum
some help with reading line from CMD on PI
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
some help with reading line from CMD on PI
#1
Hello,
I'm a beginner , and up until now I did some simple code.

now I want to go up a level :-)

I want to run an application on my PI that scan bluetooth
I found this code
import pexpect
child = pexpect.spawn("bluetoothctl")
child.send("scan on\n")
bdaddrs = []

try:
    print("Scan Begin")
    while True:
        
        child.expect("Device (([0-9A-Fa-f]{2}:){5}([0-9A-Fa-f]{2})) ",timeout=None)
        bdaddr = child.match.group(1)
        if bdaddr not in bdaddrs:
           bdaddrs.append(bdaddr)
           print(bdaddr)
        
except KeyboardInterrupt:
    child.close()
    results.close()


it's working but I want to improve it and to read the RSSI and the name also ? how do I do this ?

also want to understand something
this line
child.expect.....
mean that the code is expecting the following (whats in the bracket , right?)
what does this line mean
child.match.group(1)
mean?


and finnaly - if I want to code to run the scan for 10 seconds only once every 1 min
what I need to add?

Thanks ,
Reply
#2
(May-20-2020, 02:47 PM)korenron Wrote: it's working but I want to improve it and to read the RSSI and the name also ? how do I do this ?

Not sure how that would be done, but presumably you just need to match another line. The code above is using pexpect which helps in talking to other apps. Right now it's using a regular expression to match something that starts with "Device" and has some other things after. You'd have to create similar lines for what you want to match.
Quote:also want to understand something
this line
child.expect.....
mean that the code is expecting the following (whats in the bracket , right?)

Yes. Check the documentation for the Module to read the specifics. But this is looking at the data to see if it finds a match.

Quote:what does this line mean
child.match.group(1)
mean?

This is part of how regular expressions tend to work. If you have an expression with parentheses, the stuff inside them is "captured" and can be returned as groups. Your regular expression has 3 sets of parenthesis. Group 1 is whatever is in the first one (the one with the leftmost opening parenthesis).

Quote:and finnaly - if I want to code to run the scan for 10 seconds only once every 1 min
what I need to add?

Looks right now like it's just scanning all the time. Might be easier to just scan forever and batch up the printout. Is there some benefit you'd get doing that over just scanning constantly?
Reply
#3
great
you help me understand
and now I can see the rssi also

the point of the app is to "see" who is around me .
I can make the scan work all the time , and save once evey 10 seconds and send it using udp to my server?
how do I do this?
I guess I need to create a list and save on it what I want
but how do I send once evey 10 seconds the list?
Thanks ,
Reply
#4
I would probably use a loop that:
  • put any information collected into a set() (to remove duplicates)
  • checked if it's been more than 10s since the last transmission. If so:
    * send the data
    * zero out the set
    * set the last transmission time to "now"
Reply
#5
this is what I'm tryting now

but I'm having problems
can you take a look , please?

import pexpect
import datetime
import time
child = pexpect.spawn("bluetoothctl")
child.send("scan on\n")
SendToServer = []

try:
    print("Scan Begin")
    while True:
        now = datetime.datetime.now()
        future = (now+ 10) #####--->this is not wroking##### 
        child.expect("Device (([0-9A-Fa-f]{2}:){5}([0-9A-Fa-f]{2})) RSSI: (-[0-9]{2})",timeout=None)
        bdaddr = child.match.group(1)
        bdrssi = child.match.group(4)
        DeviceInfo = ((bdaddr + b'!' + bdrssi))
        print(DeviceInfo)
        SendToServer.append(DeviceInfo)
        if now>future:#### --->this is also not working also obvious :-) #### 
            print("it's been 10 seconds" + now)
            ##cdoe for sending to server, need to write 
            print ("Old size is - " + len(SendToServer))
            SendToServer.clear()
            print ("Ne size need to be 0" + len(SendToServer))
            break    
                
except KeyboardInterrupt:
    child.close()


and also what do you mean be set()?

Thanks,
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
  Reading in of line not working? garynewport 2 786 Sep-19-2023, 02:22 PM
Last Post: snippsat
  [Solved] Reading every nth line into a column from txt file Laplace12 7 5,144 Jun-29-2021, 09:17 AM
Last Post: Laplace12
  Ignore first few letters of a line when reading file. ShakeyPakey 16 6,187 May-30-2020, 02:17 PM
Last Post: BitPythoner
  EOFError: EOF when reading a line - Runtime Error RavCOder 6 9,556 Sep-27-2019, 12:22 PM
Last Post: RavCOder
  reading data from command line mcmxl22 2 1,956 Feb-17-2019, 09:01 PM
Last Post: Axel_Erfurt
  reading a line of a CSV Skaperen 2 2,176 Feb-10-2019, 08:10 PM
Last Post: Skaperen
  First line of File gets deleted when reading file lrxM 2 4,274 Dec-24-2016, 10:56 AM
Last Post: lrxM

Forum Jump:

User Panel Messages

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