Skip to content

Commit a7836a3

Browse files
committed
ganglion: better handling broken pipe
1 parent 7680f93 commit a7836a3

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

open_bci_ganglion.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,19 @@ def connect(self):
109109
print("Turn on notifications")
110110
# nead up-to-date bluepy, cf https://github.com/IanHarvey/bluepy/issues/53
111111
self.desc_notify = self.char_read.getDescriptors(forUUID=0x2902)[0]
112-
self.desc_notify.write(b"\x01")
112+
try:
113+
self.desc_notify.write(b"\x01")
114+
except Exception as e:
115+
print("Something went wrong while trying to enable notification: " + str(e))
113116

114117
print("Connection established")
115118

116119
def init_steaming(self):
117120
""" Tell the board to record like crazy. """
118-
self.char_write.write(b'b')
121+
try:
122+
self.ser_write(b'b')
123+
except Exception as e:
124+
print("Something went wrong while asking the board to start streaming: " + str(e))
119125
self.streaming = True
120126
self.packets_dropped = 0
121127
self.time_last_packet = timeit.default_timer()
@@ -235,17 +241,27 @@ def start_streaming(self, callback, lapse=-1):
235241
def stop(self):
236242
print("Stopping streaming...")
237243
self.streaming = False
238-
self.char_write.write(b's')
244+
# connection might be already down here
245+
try:
246+
self.ser_write(b's')
247+
except Exception as e:
248+
print("Something went wrong while asking the board to stop streaming: " + str(e))
239249
if self.log:
240250
logging.warning('sent <s>: stopped streaming')
241251

242252
def disconnect(self):
243253
if(self.streaming == True):
244254
self.stop()
245255
print("Closing BLE..")
246-
self.char_discon.write(b' ')
256+
try:
257+
self.char_discon.write(b' ')
258+
except Exception as e:
259+
print("Something went wrong while asking the board to disconnect: " + str(e))
247260
# should not try to read/write anything after that, will crash
248-
self.gang.disconnect()
261+
try:
262+
self.gang.disconnect()
263+
except Exception as e:
264+
print("Something went wrong while shutting down BLE link: " + str(e))
249265
logging.warning('BLE closed')
250266

251267

0 commit comments

Comments
 (0)