--- a/src/eric7/MicroPython/MicroPythonDevices.py Thu Feb 09 09:55:57 2023 +0100 +++ b/src/eric7/MicroPython/MicroPythonDevices.py Sat Feb 11 16:59:50 2023 +0100 @@ -9,6 +9,7 @@ """ import contextlib +import copy import importlib import logging import os @@ -19,6 +20,7 @@ from eric7 import Preferences from eric7.EricGui import EricPixmapCache +from eric7.EricWidgets import EricMessageBox from eric7.EricWidgets.EricApplication import ericApp SupportedBoards = { @@ -356,6 +358,7 @@ port.portName(), vid, pid, + port.serialNumber(), ) ) supported = True @@ -370,6 +373,7 @@ port.portName(), vid, pid, + port.serialNumber(), ) ) supported = True @@ -433,7 +437,7 @@ return EricPixmapCache.getPixmap(iconName) -def getDevice(deviceType, microPythonWidget, vid, pid, boardName=""): +def getDevice(deviceType, microPythonWidget, vid, pid, boardName="", serialNumber=""): """ Public method to instantiate a specific MicroPython device interface. @@ -447,6 +451,8 @@ @type int @param boardName name of the board (defaults to "") @type str (optional) + @param serialNumber serial number of the board (defaults to "") + @type str (optional) @return instantiated device interface @rtype MicroPythonDevice """ @@ -463,7 +469,9 @@ with contextlib.suppress(KeyError): mod = importlib.import_module(deviceMapping[deviceType], __package__) if mod: - return mod.createDevice(microPythonWidget, deviceType, vid, pid, boardName) + return mod.createDevice( + microPythonWidget, deviceType, vid, pid, boardName, serialNumber + ) # nothing specific requested or specific one failed or is not supported yet return MicroPythonDevice(microPythonWidget, deviceType) @@ -489,6 +497,23 @@ self._deviceType = deviceType self.microPython = microPythonWidget + self._deviceData = {} # dictionary with essential device data + + def setConnected(self, connected): + """ + Public method to set the connection state. + + Note: This method can be overwritten to perform actions upon connect + or disconnect of the device. + + @param connected connection state + @type bool + """ + self._deviceData = {} + + if connected: + with contextlib.suppress(OSError): + self._deviceData = self.microPython.commandsInterface().getDeviceData() def getDeviceType(self): """ @@ -499,6 +524,36 @@ """ return self._deviceType + def getDeviceData(self): + """ + Public method to get a copy of the determined device data. + + @return dictionary containing the essential device data + @rtype dict + """ + return copy.deepcopy(self._deviceData) + + def checkDeviceData(self): + """ + Public method to check the validity of the device data determined during + connecting the device. + + @return flag indicating valid device data + @rtype bool + """ + if bool(self._deviceData): + return True + else: + EricMessageBox.critical( + None, + self.tr("Show MicroPython Versions"), + self.tr( + """<p>The device data is not available. Try to connect to the""" + """ device again. Aborting...</p>""" + ).format(self.getDeviceType()), + ) + return False + def setButtons(self): """ Public method to enable the supported action buttons.