Search in sources :

Example 11 with GcodeCommand

use of com.willwinder.universalgcodesender.types.GcodeCommand in project Universal-G-Code-Sender by winder.

the class GrblController method resetCoordinateToZero.

@Override
public void resetCoordinateToZero(final char coord) throws Exception {
    if (this.isCommOpen()) {
        String gcode = GrblUtils.getResetCoordToZeroCommand(coord, this.grblVersion, this.grblVersionLetter);
        if (!"".equals(gcode)) {
            GcodeCommand command = createCommand(gcode);
            this.sendCommandImmediately(command);
            return;
        }
    }
    // Throw exception
    super.resetCoordinatesToZero();
}
Also used : GcodeCommand(com.willwinder.universalgcodesender.types.GcodeCommand)

Example 12 with GcodeCommand

use of com.willwinder.universalgcodesender.types.GcodeCommand in project Universal-G-Code-Sender by winder.

the class GrblFirmwareSettingsSerialCommunicator method updateSettingOnController.

/**
 * Sends a command to update a setting on the controller. The method will block until we get a response
 * from the controller or if a timeout is triggered if the setting took too long to update.
 *
 * @param setting the setting to update
 * @return the updated setting or an empty optional if it couldn't be updated.
 * @throws FirmwareSettingsException will be thrown if the controller isn't ready to receive setting updates or if
 *                                   the update took to long and caused a timeout
 */
public Optional<FirmwareSetting> updateSettingOnController(FirmwareSetting setting) throws FirmwareSettingsException {
    if (isUpdatingSettings()) {
        throw new FirmwareSettingsException("The settings are being updated in another thread.");
    }
    if (!canSendToController()) {
        throw new FirmwareSettingsException("The controller is not ready to receive commands.");
    }
    boolean previousSingleStepMode = controller.getSingleStepMode();
    boolean previousStatusUpdatesEnabled = controller.getStatusUpdatesEnabled();
    controller.setStatusUpdatesEnabled(false);
    controller.setSingleStepMode(true);
    try {
        try {
            updatedSetting = null;
            newSetting = setting;
            GcodeCommand command = controller.createCommand(setting.getKey() + "=" + setting.getValue());
            controller.sendCommandImmediately(command);
        } catch (Exception e) {
            throw new FirmwareSettingsException("Couldn't send update setting command to the controller: " + setting.getKey() + "=" + setting.getValue() + ".", e);
        }
        waitUntilUpdateFinished();
    } finally {
        controller.setSingleStepMode(previousSingleStepMode);
        controller.setStatusUpdatesEnabled(previousStatusUpdatesEnabled);
    }
    // Reset internal states
    Optional<FirmwareSetting> result = Optional.ofNullable(updatedSetting);
    updatedSetting = null;
    newSetting = null;
    return result;
}
Also used : GcodeCommand(com.willwinder.universalgcodesender.types.GcodeCommand) TimeoutException(java.util.concurrent.TimeoutException)

Example 13 with GcodeCommand

use of com.willwinder.universalgcodesender.types.GcodeCommand in project Universal-G-Code-Sender by winder.

the class GUIBackend method sendGcodeCommand.

@Override
public void sendGcodeCommand(boolean restoreParserState, String commandText) throws Exception {
    if (this.isConnected()) {
        GcodeCommand command = controller.createCommand(commandText);
        command.setTemporaryParserModalChange(restoreParserState);
        sendGcodeCommand(command);
        if (restoreParserState && this.isConnected()) {
            controller.restoreParserModalState();
        }
    } else {
        throw new Exception(Localization.getString("controller.log.notconnected"));
    }
}
Also used : GcodeCommand(com.willwinder.universalgcodesender.types.GcodeCommand)

Example 14 with GcodeCommand

use of com.willwinder.universalgcodesender.types.GcodeCommand in project Universal-G-Code-Sender by winder.

the class GcodeViewParse method toObjFromReader.

/**
 * Almost the same as toObjRedux, convert gcode to a LineSegment collection.
 * I've tried refactoring this, but the function is so small that merging
 * toObjFromReader and toObjRedux adds more complexity than having these two
 * methods.
 *
 * @param gcode commands to visualize.
 * @param arcSegmentLength length of line segments when expanding an arc.
 */
public List<LineSegment> toObjFromReader(GcodeStreamReader reader, double arcSegmentLength) throws IOException, GcodeParserException {
    lines.clear();
    GcodeParser gp = getParser(arcSegmentLength);
    // Save the state
    Position start = new Position();
    while (reader.getNumRowsRemaining() > 0) {
        GcodeCommand commandObject = reader.getNextCommand();
        List<String> commands = gp.preprocessCommand(commandObject.getCommandString(), gp.getCurrentState());
        for (String command : commands) {
            List<GcodeMeta> points = gp.addCommand(command, commandObject.getCommandNumber());
            for (GcodeMeta meta : points) {
                if (meta.point != null) {
                    addLinesFromPointSegment(start, meta.point, arcSegmentLength, lines);
                    start.set(meta.point.point());
                }
            }
        }
    }
    return lines;
}
Also used : GcodeMeta(com.willwinder.universalgcodesender.gcode.GcodeParser.GcodeMeta) Position(com.willwinder.universalgcodesender.model.Position) GcodeParser(com.willwinder.universalgcodesender.gcode.GcodeParser) GcodeCommand(com.willwinder.universalgcodesender.types.GcodeCommand)

Example 15 with GcodeCommand

use of com.willwinder.universalgcodesender.types.GcodeCommand in project Universal-G-Code-Sender by winder.

the class GrblControllerTest method testBeginStreaming.

/**
 * Test of beginStreaming method, of class GrblController.
 */
@Test
public void testBeginStreaming() throws Exception {
    System.out.println("beginStreaming");
    GrblController instance = new GrblController(mgc);
    instance.openCommPort("blah", 1234);
    instance.rawResponseHandler("Grbl 0.8c");
    // $G and $$ get queued on startup
    assertEquals(2, mgc.numQueueStringForCommCalls);
    // Test 1. No commands to stream.
    boolean caughtException = false;
    try {
        instance.beginStreaming();
    } catch (Exception e) {
        caughtException = true;
        assertEquals("There are no commands queued for streaming.", e.getMessage());
    }
    assertTrue(caughtException);
    // Test 2. Command already streaming.
    instance.queueCommand(instance.createCommand("G0X1"));
    caughtException = false;
    try {
        // Trigger the error.
        instance.beginStreaming();
    } catch (Exception ex) {
        caughtException = true;
        assertEquals("Cannot stream while there are active commands (controller).", ex.getMessage());
    }
    assertFalse(caughtException);
    assertEquals(3, mgc.numQueueStringForCommCalls);
    // Wrap up test 2.
    // Whitespace removed.
    GcodeCommand command = new GcodeCommand("G0X1");
    command.setSent(true);
    command.setResponse("ok");
    instance.commandSent(command);
    instance.commandComplete(command.getCommandString());
    // Test 3. Stream some commands and make sure they get sent.
    for (int i = 0; i < 30; i++) {
        instance.queueCommand(instance.createCommand("G0X" + i));
    }
    try {
        // Trigger the error.
        instance.beginStreaming();
    } catch (Exception ex) {
        caughtException = true;
        assertEquals("Cannot stream while there are active commands (controller).", ex.getMessage());
    }
    assertFalse(caughtException);
    assertEquals(30, instance.rowsRemaining());
    assertEquals(33, mgc.numQueueStringForCommCalls);
    // Wrap up test 3.
    for (int i = 0; i < 30; i++) {
        command.setCommand("G0X" + i);
        instance.commandSent(command);
        instance.commandComplete(command.getCommandString());
    }
}
Also used : GcodeCommand(com.willwinder.universalgcodesender.types.GcodeCommand) IOException(java.io.IOException) Test(org.junit.Test) GcodeStreamTest(com.willwinder.universalgcodesender.utils.GcodeStreamTest)

Aggregations

GcodeCommand (com.willwinder.universalgcodesender.types.GcodeCommand)51 Test (org.junit.Test)26 GcodeStreamTest (com.willwinder.universalgcodesender.utils.GcodeStreamTest)19 IOException (java.io.IOException)11 Connection (com.willwinder.universalgcodesender.connection.Connection)4 Position (com.willwinder.universalgcodesender.model.Position)4 ControllerListener (com.willwinder.universalgcodesender.listeners.ControllerListener)3 GcodeStreamReader (com.willwinder.universalgcodesender.utils.GcodeStreamReader)3 File (java.io.File)3 LinkedList (java.util.LinkedList)3 GcodeParser (com.willwinder.universalgcodesender.gcode.GcodeParser)2 EasyMock.anyString (org.easymock.EasyMock.anyString)2 UnexpectedCommand (com.willwinder.universalgcodesender.AbstractController.UnexpectedCommand)1 GcodeMeta (com.willwinder.universalgcodesender.gcode.GcodeParser.GcodeMeta)1 ArcExpander (com.willwinder.universalgcodesender.gcode.processors.ArcExpander)1 CommentProcessor (com.willwinder.universalgcodesender.gcode.processors.CommentProcessor)1 LineSplitter (com.willwinder.universalgcodesender.gcode.processors.LineSplitter)1 M30Processor (com.willwinder.universalgcodesender.gcode.processors.M30Processor)1 MeshLeveler (com.willwinder.universalgcodesender.gcode.processors.MeshLeveler)1 Translator (com.willwinder.universalgcodesender.gcode.processors.Translator)1