From a884cd12bc1758f8372717b230d9ae57d15ff64e Mon Sep 17 00:00:00 2001 From: Joacim Breiler Date: Wed, 7 Feb 2024 06:56:41 +0100 Subject: [PATCH 1/2] Attempt connection to GRBL for ten seconds instead of three --- .../universalgcodesender/GrblUtils.java | 104 ++++++++---------- 1 file changed, 47 insertions(+), 57 deletions(-) diff --git a/ugs-core/src/com/willwinder/universalgcodesender/GrblUtils.java b/ugs-core/src/com/willwinder/universalgcodesender/GrblUtils.java index 278511c339..d6986d190e 100644 --- a/ugs-core/src/com/willwinder/universalgcodesender/GrblUtils.java +++ b/ugs-core/src/com/willwinder/universalgcodesender/GrblUtils.java @@ -51,6 +51,9 @@ This file is part of Universal Gcode Sender (UGS). */ public class GrblUtils { + private GrblUtils() { + } + // Note: The Grbl RX buffer is not consumed by real-time commands public static final int GRBL_RX_BUFFER_SIZE= 128; @@ -99,8 +102,8 @@ public static Boolean isGrblVersionString(final String response) { /** * Parses the version double out of the version response string. */ - final static String VERSION_DOUBLE_REGEX = "[0-9]*\\.[0-9]*"; - final static Pattern VERSION_DOUBLE_PATTERN = Pattern.compile(VERSION_DOUBLE_REGEX); + static final String VERSION_DOUBLE_REGEX = "[0-9]*\\.[0-9]*"; + static final Pattern VERSION_DOUBLE_PATTERN = Pattern.compile(VERSION_DOUBLE_REGEX); public static double getVersionDouble(final String response) { double retValue = -1; @@ -113,8 +116,8 @@ public static double getVersionDouble(final String response) { return retValue; } - final static String VERSION_LETTER_REGEX = "(?<=[0-9]\\.[0-9])[a-zA-Z]"; - final static Pattern VERSION_LETTER_PATTERN = Pattern.compile(VERSION_LETTER_REGEX); + static final String VERSION_LETTER_REGEX = "(?<=[0-9]\\.[0-9])[a-zA-Z]"; + static final Pattern VERSION_LETTER_PATTERN = Pattern.compile(VERSION_LETTER_REGEX); public static Character getVersionLetter(final String response) { Character retValue = null; @@ -127,7 +130,7 @@ public static Character getVersionLetter(final String response) { return retValue; } - static protected String getHomingCommand(final double version, final Character letter) { + protected static String getHomingCommand(final double version, final Character letter) { if ((version >= 0.8 && (letter != null) && (letter >= 'c')) || version >= 0.9) { return GrblUtils.GCODE_PERFORM_HOMING_CYCLE_V8C; @@ -196,7 +199,7 @@ else if (grblVersion >= 0.8) { } - static protected String getKillAlarmLockCommand(final double version, final Character letter) { + protected static String getKillAlarmLockCommand(final double version, final Character letter) { if ((version >= 0.8 && (letter != null) && letter >= 'c') || version >= 0.9) { return GrblUtils.GRBL_KILL_ALARM_LOCK_COMMAND; @@ -206,7 +209,7 @@ static protected String getKillAlarmLockCommand(final double version, final Char } } - static protected String getToggleCheckModeCommand(final double version, final Character letter) { + protected static String getToggleCheckModeCommand(final double version, final Character letter) { if ((version >= 0.8 && (letter != null) && letter >= 'c') || version >= 0.9) { return GrblUtils.GRBL_TOGGLE_CHECK_MODE_COMMAND; @@ -216,7 +219,7 @@ static protected String getToggleCheckModeCommand(final double version, final Ch } } - static protected String getViewParserStateCommand(final double version, final Character letter) { + protected static String getViewParserStateCommand(final double version, final Character letter) { if ((version >= 0.8 && (letter != null) && letter >= 'c') || version >= 0.9) { return GrblUtils.GRBL_VIEW_PARSER_STATE_COMMAND; @@ -229,7 +232,7 @@ static protected String getViewParserStateCommand(final double version, final Ch /** * Determines version of GRBL position capability. */ - static protected Capabilities getGrblStatusCapabilities(final double version, final Character letter) { + protected static Capabilities getGrblStatusCapabilities(final double version, final Character letter) { Capabilities ret = new Capabilities(); ret.addCapability(CapabilitiesConstants.JOGGING); ret.addCapability(CapabilitiesConstants.CHECK_MODE); @@ -266,7 +269,7 @@ static protected Capabilities getGrblStatusCapabilities(final double version, fi return ret; } - static protected Position parseProbePosition(final String response, final Units units) { + protected static Position parseProbePosition(final String response, final Units units) { // Don't parse failed probe response. if (response.endsWith(":0]")) { return null; @@ -286,7 +289,8 @@ public static boolean isGrblStatusString(final String response) { private static final String PROBE_REGEX = "\\[PRB:.*]"; private static final Pattern PROBE_PATTERN = Pattern.compile(PROBE_REGEX); - static protected boolean isGrblProbeMessage(final String response) { + + protected static boolean isGrblProbeMessage(final String response) { return PROBE_PATTERN.matcher(response).find(); } @@ -304,7 +308,7 @@ public static boolean isGrblFeedbackMessageV1(final String response) { return response.startsWith("[GC:"); } - static protected String parseFeedbackMessage(final String response, Capabilities c) { + protected static String parseFeedbackMessage(final String response, Capabilities c) { if (c.hasCapability(GrblCapabilitiesConstants.V1_FORMAT)) { return parseFeedbackMessageV1(response); } else { @@ -318,7 +322,8 @@ public static String parseFeedbackMessageV1(final String response) { private static final String SETTING_REGEX = "\\$\\d+=.+"; private static final Pattern SETTING_PATTERN = Pattern.compile(SETTING_REGEX); - static protected boolean isGrblSettingMessage(final String response) { + + protected static boolean isGrblSettingMessage(final String response) { return SETTING_PATTERN.matcher(response).find(); } @@ -333,7 +338,7 @@ static protected boolean isGrblSettingMessage(final String response) { * @param reportingUnits units * @return the parsed controller status */ - static protected ControllerStatus getStatusFromStatusString( + protected static ControllerStatus getStatusFromStatusString( ControllerStatus lastStatus, final String status, final Capabilities version, Units reportingUnits) { // Legacy status. @@ -534,8 +539,8 @@ public static double parseFeedSpeed(String part) { /** * Parse state out of position string. */ - final static String STATUS_STATE_REGEX = "(?<=<)[a-zA-z]*(?=[,>])"; - final static Pattern STATUS_STATE_PATTERN = Pattern.compile(STATUS_STATE_REGEX); + static final String STATUS_STATE_REGEX = "(?<=<)[a-zA-z]*(?=[,>])"; + static final Pattern STATUS_STATE_PATTERN = Pattern.compile(STATUS_STATE_REGEX); protected static String getStateFromStatusString(final String status) { String retValue = null; Matcher matcher = STATUS_STATE_PATTERN.matcher(status); @@ -545,8 +550,8 @@ protected static String getStateFromStatusString(final String status) { return retValue; } - private final static String STATUS_VERSION_1_REGEX = "^<[a-zA-Z]+[|]+.*>$"; - private final static Pattern STATUS_VERSION_1_PATTERN = Pattern.compile(STATUS_VERSION_1_REGEX); + private static final String STATUS_VERSION_1_REGEX = "^<[a-zA-Z]+[|]+.*>$"; + private static final Pattern STATUS_VERSION_1_PATTERN = Pattern.compile(STATUS_VERSION_1_REGEX); public static boolean isGrblStatusStringV1(String response) { return STATUS_VERSION_1_PATTERN.matcher(response).matches(); @@ -573,11 +578,12 @@ public static ControllerState getControllerStateFromStateString(String stateStri static Pattern machinePattern = Pattern.compile("(?<=MPos:)(-?\\d*\\.?\\d*),(-?\\d*\\.?\\d*),(-?\\d*\\.?\\d*)(?:,(-?\\d*\\.?\\d+))?(?:,(-?\\d*\\.?\\d+))?(?:,(-?\\d*\\.?\\d+))?"); static Pattern workPattern = Pattern.compile("(?<=WPos:)(-?\\d*\\.?\\d*),(-?\\d*\\.?\\d*),(-?\\d*\\.?\\d*)(?:,(-?\\d*\\.?\\d+))?(?:,(-?\\d*\\.?\\d+))?(?:,(-?\\d*\\.?\\d+))?"); static Pattern wcoPattern = Pattern.compile("(?<=WCO:)(-?\\d*\\.?\\d*),(-?\\d*\\.?\\d*),(-?\\d*\\.?\\d*)(?:,(-?\\d*\\.?\\d+))?(?:,(-?\\d*\\.?\\d+))?(?:,(-?\\d*\\.?\\d+))?"); - static protected Position getMachinePositionFromStatusString(final String status, Units reportingUnits) { + + protected static Position getMachinePositionFromStatusString(final String status, Units reportingUnits) { return GrblUtils.getPositionFromStatusString(status, machinePattern, reportingUnits); } - static protected Position getWorkPositionFromStatusString(final String status, Units reportingUnits) { + protected static Position getWorkPositionFromStatusString(final String status, Units reportingUnits) { return GrblUtils.getPositionFromStatusString(status, workPattern, reportingUnits); } @@ -609,43 +615,27 @@ public static Position getPositionFromStatusString(final String status, final Pa /** * Map version enum to GRBL real time command byte. */ - static public Byte getOverrideForEnum(final Overrides command, final Capabilities version) { + public static Byte getOverrideForEnum(final Overrides command, final Capabilities version) { if (version != null && version.hasOverrides()) { - switch (command) { + return switch (command) { //CMD_DEBUG_REPORT, // 0x85 // Only when DEBUG enabled, sends debug report in '{}' braces. - case CMD_FEED_OVR_RESET: - return (byte)0x90; // Restores feed override value to 100%. - case CMD_FEED_OVR_COARSE_PLUS: - return (byte)0x91; - case CMD_FEED_OVR_COARSE_MINUS: - return (byte)0x92; - case CMD_FEED_OVR_FINE_PLUS : - return (byte)0x93; - case CMD_FEED_OVR_FINE_MINUS : - return (byte)0x94; - case CMD_RAPID_OVR_RESET: - return (byte)0x95; - case CMD_RAPID_OVR_MEDIUM: - return (byte)0x96; - case CMD_RAPID_OVR_LOW: - return (byte)0x97; - case CMD_SPINDLE_OVR_RESET: - return (byte)0x99; // Restores spindle override value to 100%. - case CMD_SPINDLE_OVR_COARSE_PLUS: - return (byte)0x9A; - case CMD_SPINDLE_OVR_COARSE_MINUS: - return (byte)0x9B; - case CMD_SPINDLE_OVR_FINE_PLUS: - return (byte)0x9C; - case CMD_SPINDLE_OVR_FINE_MINUS: - return (byte)0x9D; - case CMD_TOGGLE_SPINDLE: - return (byte)0x9E; - case CMD_TOGGLE_FLOOD_COOLANT: - return (byte)0xA0; - case CMD_TOGGLE_MIST_COOLANT: - return (byte)0xA1; - } + case CMD_FEED_OVR_RESET -> (byte) 0x90; // Restores feed override value to 100%. + case CMD_FEED_OVR_COARSE_PLUS -> (byte) 0x91; + case CMD_FEED_OVR_COARSE_MINUS -> (byte) 0x92; + case CMD_FEED_OVR_FINE_PLUS -> (byte) 0x93; + case CMD_FEED_OVR_FINE_MINUS -> (byte) 0x94; + case CMD_RAPID_OVR_RESET -> (byte) 0x95; + case CMD_RAPID_OVR_MEDIUM -> (byte) 0x96; + case CMD_RAPID_OVR_LOW -> (byte) 0x97; + case CMD_SPINDLE_OVR_RESET -> (byte) 0x99; // Restores spindle override value to 100%. + case CMD_SPINDLE_OVR_COARSE_PLUS -> (byte) 0x9A; + case CMD_SPINDLE_OVR_COARSE_MINUS -> (byte) 0x9B; + case CMD_SPINDLE_OVR_FINE_PLUS -> (byte) 0x9C; + case CMD_SPINDLE_OVR_FINE_MINUS -> (byte) 0x9D; + case CMD_TOGGLE_SPINDLE -> (byte) 0x9E; + case CMD_TOGGLE_FLOOD_COOLANT -> (byte) 0xA0; + case CMD_TOGGLE_MIST_COOLANT -> (byte) 0xA1; + }; } return null; } @@ -703,11 +693,11 @@ public static boolean isControllerResponsive(GrblController controller) throws E } private static GetStatusCommand queryForStatusReport(GrblController controller) throws InterruptedException { - return sendAndWaitForCompletionWithRetry(GetStatusCommand::new, controller, 1000, 3, (executionNumber) -> { + return sendAndWaitForCompletionWithRetry(GetStatusCommand::new, controller, 1000, 10, executionNumber -> { if (executionNumber == 1) { controller.getMessageService().dispatchMessage(MessageType.INFO, "*** Fetching device status\n"); } else { - controller.getMessageService().dispatchMessage(MessageType.INFO, "*** Fetching device status (" + executionNumber + " of 3)...\n"); + controller.getMessageService().dispatchMessage(MessageType.INFO, "*** Fetching device status (" + executionNumber + " of 10)...\n"); } }); } From a75cbb2971294866fab05e46f4bba0b3229d5109 Mon Sep 17 00:00:00 2001 From: Joacim Breiler Date: Wed, 7 Feb 2024 07:02:44 +0100 Subject: [PATCH 2/2] Attempt connection to GRBL for ten seconds instead of three --- .../universalgcodesender/GrblControllerInitializerTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ugs-core/test/com/willwinder/universalgcodesender/GrblControllerInitializerTest.java b/ugs-core/test/com/willwinder/universalgcodesender/GrblControllerInitializerTest.java index 8af8c3f962..cbf2772651 100644 --- a/ugs-core/test/com/willwinder/universalgcodesender/GrblControllerInitializerTest.java +++ b/ugs-core/test/com/willwinder/universalgcodesender/GrblControllerInitializerTest.java @@ -101,7 +101,7 @@ public void initializeShouldThrowErrorWhenNoStatusResponseFromController() throw RuntimeException exception = assertThrows(RuntimeException.class, instance::initialize); assertEquals("Could not query the device status", exception.getMessage()); - verify(controller, times(3)).sendCommandImmediately(any(GetStatusCommand.class)); + verify(controller, times(10)).sendCommandImmediately(any(GetStatusCommand.class)); verify(controller, times(0)).issueSoftReset(); }