Skip to content
PerlinWarp edited this page Sep 6, 2021 · 5 revisions

Serial Exceptions

 raise SerialException("could not open port {!r}: {!r}".format(self.portstr, ctypes.WinError()))
serial.serialutil.SerialException: could not open port 'COM3': PermissionError(13, 'Access is denied.', None, 5)

This is a Windows only problem where a serial port can only be accessed from one thread. A Myo object made can only be accessed inside the thread it was made in.

For example:

# ------------ Myo Setup ---------------
q = multiprocessing.Queue()

def worker(q):
	m = Myo(mode=emg_mode.PREPROCESSED)
	m.connect()

	def add_to_queue(emg, movement):
		q.put(emg)

	"""worker function"""
	while carryOn:
		m.run()
	print("Worker Stopped")

# -------- Main Program Loop -----------
p = multiprocessing.Process(target=worker, args=(q,))
p.start()
# Vibrate to know we connected okay
m.vibrate(1)

Will error on Windows, as m is being accessed in the main program loop but was made in the worker thread. To fix it we need to move m.vibrate(1) into the worker thread:

# ------------ Myo Setup ---------------
q = multiprocessing.Queue()

def worker(q):
	m = Myo(mode=emg_mode.PREPROCESSED)
	m.connect()

	def add_to_queue(emg, movement):
		q.put(emg)

	m.add_emg_handler(add_to_queue)

	def print_battery(bat):
		print("Battery level:", bat)

	m.add_battery_handler(print_battery)

	# Orange logo and bar LEDs
	m.set_leds([128, 0, 0], [128, 0, 0])
	# Vibrate to know we connected okay
	m.vibrate(1)

	"""worker function"""
	while carryOn:
		m.run()
	print("Worker Stopped")

# -------- Main Program Loop -----------
p = multiprocessing.Process(target=worker, args=(q,))
p.start()

The Myo sends data all the time, so there needs to be a thread listening to it all the time. The thread should just get data and put it in a queue so another thread can deal with it, so there shouldn't be much need to access m from multiple threads.

When making a game you might want to get the Myo to vibrate when you score, this can be solved by sending a message to the worker thread. See the multiprocessing docs for help on doing this.