def findAvailableMicroflySerialPorts(self): portsFound = [] comPorts = list_ports.comports() for comPort in comPorts: try: self.debugFunc("Trying: " + comPort[0]) serialConnection = serial.Serial(comPort[0], 115200, timeout=0.2) self.debugFunc("Connected to " + comPort[0]) except Exception, e: self.debugFunc("Could not open port, ignoring") else: currentProtocolState = mfcore.protocol_create_state_malloc(2048, lambda: None, lambda data:serialConnection.write(chr(data)), lambda: None); mfcore.protocol_usb_init(currentProtocolState); mfcore.protocol_usb_send_get_device_name(currentProtocolState); continueOn = 0 timeout = 3 while(continueOn == 0 and timeout > 0): self.yieldFunc() timeout -= 1 dataRead = serialConnection.read() dataRead = bytearray(dataRead) for data in dataRead: timeout = 3 mfcore.protocol_receive_packet_byte(currentProtocolState, data); if(mfcore.protocol_receive_is_packet_available(currentProtocolState) == 1): if(mfcore.protocol_usb_receive_is_get_settings_response_device_name(currentProtocolState) == 1): name = mfcore.protocol_usb_receive_get_settings_response_device_name_get_name(currentProtocolState) portsFound.append(comPort[0] + " (" + name + ")"); continueOn = 1
def connect(self, port_name): try: self.debugFunc("Connecting to " + port_name) self.activeSerialConnection = serial.Serial(port_name, 921600) self.activeProtocolState = mfcore.protocol_create_state_malloc(2048, lambda: None, lambda data:self.sendByteIfConnected(data), lambda: None); mfcore.protocol_usb_init(self.activeProtocolState); except Exception, e: self.debugFunc("Could not open port, throwing error") return 0