@@ -3773,21 +3773,34 @@ bool GCodes::HandleMcode(GCodeBuffer& gb, const StringRef& reply) THROWS(GCodeEx
37733773 const size_t chan = gb.GetLimitedUIValue (' P' , NumSerialChannels);
37743774 bool hostMode = false ;
37753775 bool seen = false ;
3776- #if SUPPORT_USB_DRIVE
3777- if (chan == 0 && gb.TryGetBValue (' H' , hostMode, seen)) // switch modes first
3776+
3777+ // Check if a USB mode change is requested through the 'H' parameter on channel 0.
3778+ // If not on channel 0, short-circuit evaluation means TryGetBValue is not performed
3779+ // and hostMode == false, result == GCodeResult:ok is unchanged.
3780+ if (chan == 0 && gb.TryGetBValue (' H' , hostMode, seen))
37783781 {
3782+ #if SUPPORT_USB_DRIVE
37793783 if (!platform.SetUsbHostMode (hostMode, reply))
37803784 {
3781- reply.printf (" Unable to set to %s mode" , hostMode ? " host" : " device" );
3785+ reply.printf (" Unable to set to USB %s mode" , hostMode ? " host" : " device" );
37823786 result = GCodeResult::error;
37833787 }
3784- }
37853788#else
3786- reply.printf (" USB host mode unsupported" );
3787- result = GCodeResult::error;
3789+ // No support for changing to host mode; changing to device mode is ignored.
3790+ if (hostMode)
3791+ {
3792+ reply.printf (" USB host mode unsupported" );
3793+ result = GCodeResult::error;
3794+ }
37883795#endif
3789- if (result == GCodeResult::ok && !hostMode) // switched to device mode with no error, handle other device mode args
3796+ }
3797+
3798+ // Handle the other parameters if specified along with the change to USB device mode,
3799+ // no USB mode change requested, or communication parameter configuration on other channels.
3800+ if (result == GCodeResult::ok && !hostMode)
37903801 {
3802+ seen = false ;
3803+
37913804 GCodeBuffer * const gbp = (chan == 0 ) ? UsbGCode () : (chan == 1 ) ? AuxGCode () : Aux2GCode ();
37923805 if (gb.Seen (' B' ))
37933806 {
0 commit comments