Skip to content
Snippets Groups Projects
Commit 9ad6395a authored by Leonardo Martinez Hornak's avatar Leonardo Martinez Hornak
Browse files

Simple bidir communication working

parent b2a3d086
No related branches found
No related tags found
No related merge requests found
#Class that uses bluetooth to communicate with HC-06
#https://github.com/pybluez/pybluez
#https://github.com/pybluez/pybluez/blob/master/examples/simple/rfcomm-server.py
import bluetooth
class Hardware_interface:
def __init__(self):
#def __init__(self):
def __del__(self):
#Destructor used to close the socket
self.client_socket.close()
self.server_socket.close()
print("All done.")
#Print device list to check names used by the system. Used for debugging purposes
def print_device_list(self):
nearby_devices = bluetooth.discover_devices(lookup_names=True)
print("Found {} devices.".format(len(nearby_devices)))
for addr, name in nearby_devices:
print(" {} - {}".format(addr, name))
#Print device list to check names used by the system. Used for debugging purposes
#def print_device_list(self):
print(" {} - {}".format(addr, name))
def start_server(self):
self.server_socket = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
self.server_socket.bind(("", bluetooth.PORT_ANY))
self.server_socket.listen(1)
port = self.server_socket.getsockname()[1]
uuid = "94f39d29-7d6d-437d-973b-fba39e49d4ee"
bluetooth.advertise_service(self.server_socket, "SampleServer", service_id=uuid,
service_classes=[uuid, bluetooth.SERIAL_PORT_CLASS],
profiles=[bluetooth.SERIAL_PORT_PROFILE],
# protocols=[bluetooth.OBEX_UUID]
)
print("Waiting for connection on RFCOMM channel", port)
self.client_socket, self.client_info = self.server_socket.accept()
print("Accepted connection from", self.client_info)
#def send_data(self):
def send_data(self):
self.client_socket.send("Envio desde la PC")
#def get_data(self):
\ No newline at end of file
def get_data(self):
try:
while True:
data = self.client_socket.recv(1024)
print(data)
if not data:
break
print("Received", data)
except OSError:
pass
\ No newline at end of file
......@@ -166,10 +166,16 @@ if __name__ == '__main__':
#Bluetooth object for communication with MSP432
btInterface = Hardware_interface()
btInterface.print_device_list()
btInterface.start_server()
btInterface.send_data()
btInterface.get_data()
#Thread used to separate the use interface with the front end. It is used to not slow down the GUI when processing information
t = Thread(target=refreshUI, args=(guiRef, model, guiQueue,))
t.daemon = True
t.start()
app.mainloop()
#Uncomment to show the GUI
#app.mainloop()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment