@@ -3793,21 +3793,34 @@ bool GCodes::HandleMcode(GCodeBuffer& gb, const StringRef& reply) THROWS(GCodeEx
37933793 const size_t chan = gb.GetLimitedUIValue (' P' , NumSerialChannels);
37943794 bool hostMode = false ;
37953795 bool seen = false ;
3796- #if SUPPORT_USB_DRIVE
3797- if (chan == 0 && gb.TryGetBValue (' H' , hostMode, seen)) // switch modes first
3796+
3797+ // Check if a USB mode change is requested through the 'H' parameter on channel 0.
3798+ // If not on channel 0, short-circuit evaluation means TryGetBValue is not performed
3799+ // and hostMode == false, result == GCodeResult:ok is unchanged.
3800+ if (chan == 0 && gb.TryGetBValue (' H' , hostMode, seen))
37983801 {
3802+ #if SUPPORT_USB_DRIVE
37993803 if (!platform.SetUsbHostMode (hostMode, reply))
38003804 {
3801- reply.printf (" Unable to set to %s mode" , hostMode ? " host" : " device" );
3805+ reply.printf (" Unable to set to USB %s mode" , hostMode ? " host" : " device" );
38023806 result = GCodeResult::error;
38033807 }
3804- }
38053808#else
3806- reply.printf (" USB host mode unsupported" );
3807- result = GCodeResult::error;
3809+ // No support for changing to host mode; changing to device mode is ignored.
3810+ if (hostMode)
3811+ {
3812+ reply.printf (" USB host mode unsupported" );
3813+ result = GCodeResult::error;
3814+ }
38083815#endif
3809- if (result == GCodeResult::ok && !hostMode) // switched to device mode with no error, handle other device mode args
3816+ }
3817+
3818+ // Handle the other parameters if specified along with the change to USB device mode,
3819+ // no USB mode change requested, or communication parameter configuration on other channels.
3820+ if (result == GCodeResult::ok && !hostMode)
38103821 {
3822+ seen = false ;
3823+
38113824 GCodeBuffer * const gbp = (chan == 0 ) ? UsbGCode () : (chan == 1 ) ? AuxGCode () : Aux2GCode ();
38123825 if (gb.Seen (' B' ))
38133826 {
0 commit comments