Skip to content

Commit

Permalink
Crash when attempting to connect to board via serial (#1781)
Browse files Browse the repository at this point in the history
  • Loading branch information
neilenns authored Jun 16, 2024
1 parent 102df37 commit 51be925
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 28 deletions.
61 changes: 35 additions & 26 deletions MobiFlight/MobiFlightModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public enum Command
InputMultiplexerChange, // 30
SetStepperSpeedAccel, // 31
SetCustomDevice, // 32
DebugPrint =0xFF // 255 for Debug Print from Firmware to log/terminal
DebugPrint = 0xFF // 255 for Debug Print from Firmware to log/terminal
};

public const string TYPE_COMPATIBLE = "Compatible";
Expand Down Expand Up @@ -90,16 +90,17 @@ public enum Command
public String Port { get { return _comPort; } }

private string _name;
public String Name {
public String Name
{
get
{
if (HasMfFirmware()) return _name;
return Board.Info.FriendlyName;
}
set
set
{
_name = value;
}
}
}

public string HardwareId { get; set; }
Expand Down Expand Up @@ -255,34 +256,42 @@ public void Connect()
return;
}

// Create Serial Port object
int baudRate = 115200;
//baudRate = 57600;
_transportLayer = new SerialTransport()
//_transportLayer = new SerialPortManager
try
{
//CurrentSerialSettings = { PortName = _comPort, BaudRate = 115200, DtrEnable = dtrEnable } // object initializer
CurrentSerialSettings = { PortName = _comPort, BaudRate = baudRate, DtrEnable = Board.Connection.DtrEnable } // object initializer
};
// Create Serial Port object
int baudRate = 115200;
//baudRate = 57600;
_transportLayer = new SerialTransport()
//_transportLayer = new SerialPortManager
{
//CurrentSerialSettings = { PortName = _comPort, BaudRate = 115200, DtrEnable = dtrEnable } // object initializer
CurrentSerialSettings = { PortName = _comPort, BaudRate = baudRate, DtrEnable = Board.Connection.DtrEnable } // object initializer
};

_cmdMessenger = new CmdMessenger(_transportLayer, BoardType.Bit16, ',', ';', '\\', Board.Connection.MessageSize);
Log.Instance.log($"Connecting to {_comPort} - Baud rate: {baudRate}, DtrEnable: {Board.Connection.DtrEnable}", LogSeverity.Debug);

// Attach the callbacks to the Command Messenger
AttachCommandCallBacks();
_cmdMessenger = new CmdMessenger(_transportLayer, BoardType.Bit16, ',', ';', '\\', Board.Connection.MessageSize);

// Start listening
var status = _cmdMessenger.Connect();
Log.Instance.log($"MobiflightModule.connect: Connected to {Name} at {_comPort} of type {Board.Info.MobiFlightType} (DTR=>{_transportLayer.CurrentSerialSettings.DtrEnable}).", LogSeverity.Info);
//this.Connected = status;
this.connected = true;
// Attach the callbacks to the Command Messenger
AttachCommandCallBacks();

// this sleep helps during initialization
// without this line modules did not connect properly
if (Board.Connection.ConnectionDelay > 0)
// Start listening
var status = _cmdMessenger.Connect();
Log.Instance.log($"MobiflightModule.connect: Connected to {Name} at {_comPort} of type {Board.Info.MobiFlightType} (DTR=>{_transportLayer.CurrentSerialSettings.DtrEnable}).", LogSeverity.Info);
//this.Connected = status;
this.connected = true;

// this sleep helps during initialization
// without this line modules did not connect properly
if (Board.Connection.ConnectionDelay > 0)
{
System.Threading.Thread.Sleep(Board.Connection.ConnectionDelay);
}
}
catch (Exception ex)
{
System.Threading.Thread.Sleep(Board.Connection.ConnectionDelay);
Log.Instance.log($"Error connecting to device: {ex.Message}", LogSeverity.Error);
}

// if (!this.Connected) return;
// ResetBoard();
// LoadConfig();
Expand Down Expand Up @@ -903,7 +912,7 @@ public IModuleInfo GetInfo()

// With the support of Custom Devices
// we also introduced CoreVersion
if(InfoCommand.Arguments.Length>4)
if (InfoCommand.Arguments.Length > 4)
{
CoreVersion = InfoCommand.ReadStringArg();
}
Expand Down
6 changes: 4 additions & 2 deletions MobiFlight/Monitors/SerialPortMonitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,11 @@ override protected void Scan()
var portNameMatch = Regex.Match(queryObj["Caption"].ToString(), portNameRegEx); // Find the COM port.
var portName = portNameMatch?.Value.Trim(new char[] { '(', ')' }); // Remove the surrounding ().

if (portName == null)
if (string.IsNullOrEmpty(portName))
{
//Log.Instance.log($"Device has no port information: {hardwareId}.", LogSeverity.Error);
// Issue 1778: If someone renames the device in Device Manager and removes the COM
// port from the device name then this will be blank. Log an error and continue.
Log.Instance.log($"Detected {queryObj["Caption"].ToString()} but it has no COM port in its name.", LogSeverity.Error);
continue;
}

Expand Down

0 comments on commit 51be925

Please sign in to comment.