Search in sources :

Example 16 with GcodeCommand

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

the class GrblControllerTest method testGetSendDuration.

/**
 * Test of getSendDuration method, of class GrblController.
 */
@Test
public void testGetSendDuration() throws Exception {
    System.out.println("getSendDuration");
    GrblController instance = new GrblController(mgc);
    instance.openCommPort("blah", 1234);
    instance.rawResponseHandler("Grbl 0.8c");
    // Test 1.
    // Result when not sending and nothing has been sent.
    long expResult = 0L;
    long result = instance.getSendDuration();
    assertEquals(expResult, result);
    // Test 2.
    // Result when stream has begun but not completed.
    instance.queueCommand(instance.createCommand("G0X1"));
    try {
        instance.beginStreaming();
    } catch (Exception ex) {
        fail("Unexpected exception from GrblController: " + ex.getMessage());
    }
    try {
        // Sleep for 2 seconds
        Thread.sleep(2000);
    } catch (InterruptedException ex) {
        fail("Unexpected exception from Thread.sleep: " + ex.getMessage());
    }
    // Send duration should be around 2 seconds.
    expResult = 2000L;
    result = instance.getSendDuration();
    // Assert that result is within 0.5 seconds of expected value.
    assert (expResult <= result);
    assert (result <= (expResult + 500));
    try {
        // Sleep for 2 seconds
        Thread.sleep(2000);
    } catch (InterruptedException ex) {
        fail("Unexpected exception from Thread.sleep: " + ex.getMessage());
    }
    // Test 3.
    // Wrap up the send and check the duration.
    // Whitespace removed.
    GcodeCommand command = new GcodeCommand("G0X1");
    command.setSent(true);
    command.setResponse("ok");
    try {
        instance.commandSent(command);
        instance.commandComplete(command.getCommandString());
    } catch (Exception ex) {
        ex.printStackTrace();
        fail("Unexpected exception from command complete: " + ex.getMessage());
    }
    expResult = 4000L;
    result = instance.getSendDuration();
    // Assert that result is within 0.5 seconds of expected value.
    assert (expResult <= result);
    assert (result <= (expResult + 500));
    // Make sure the duration is no longer increasing.
    try {
        // Sleep for 2 seconds
        Thread.sleep(2000);
    } catch (InterruptedException ex) {
        fail("Unexpected exception from Thread.sleep: " + ex.getMessage());
    }
    result = instance.getSendDuration();
    // Assert that result is within 0.5 seconds of expected value.
    assert (expResult <= result);
    assert (result <= (expResult + 500));
}
Also used : GcodeCommand(com.willwinder.universalgcodesender.types.GcodeCommand) IOException(java.io.IOException) Test(org.junit.Test) GcodeStreamTest(com.willwinder.universalgcodesender.utils.GcodeStreamTest)

Example 17 with GcodeCommand

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

the class GrblControllerTest method testSendCommandImmediately.

/**
 * Test of numQueueStringForCommCalls method, of class GrblController.
 */
@Test
public void testSendCommandImmediately() throws Exception {
    System.out.println("queueStringForComm");
    String str = "G0 X0 ";
    GrblController instance = new GrblController(mgc);
    instance.openCommPort("blah", 123);
    instance.rawResponseHandler("Grbl 0.8c");
    // $G and $$ get queued on startup
    assertEquals(2, mgc.numQueueStringForCommCalls);
    assertEquals(2, mgc.numStreamCommandsCalls);
    GcodeCommand command = instance.createCommand(str);
    instance.sendCommandImmediately(command);
    assertEquals(3, mgc.numQueueStringForCommCalls);
    assertEquals(3, mgc.numStreamCommandsCalls);
    assertEquals(str + "\n", mgc.queuedString);
}
Also used : GcodeCommand(com.willwinder.universalgcodesender.types.GcodeCommand) Test(org.junit.Test) GcodeStreamTest(com.willwinder.universalgcodesender.utils.GcodeStreamTest)

Example 18 with GcodeCommand

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

the class GrblControllerTest method errorInCheckModeSending.

@Test
public void errorInCheckModeSending() throws Exception {
    // Given
    AbstractCommunicator communicator = mock(AbstractCommunicator.class);
    ControllerListener controllerListener = mock(ControllerListener.class);
    GrblController gc = new GrblController(communicator);
    gc.addListener(controllerListener);
    // We will assume that we are using version Grbl 1.0 with streaming support
    gc.rawResponseHandler("Grbl 1.1f");
    gc.rawResponseHandler("<Check|MPos:0.000,0.000,0.000|FS:0,0|Pn:XYZ>");
    doReturn(true).when(communicator).isCommOpen();
    // When
    gc.queueCommand(new GcodeCommand("G0 X10"));
    gc.beginStreaming();
    gc.communicatorPausedOnError();
    gc.rawResponseHandler("error:1");
    // Then
    assertEquals(gc.getControlState(), COMM_CHECK);
    assertFalse(gc.isPaused());
    verify(communicator, times(1)).sendByteImmediately(GRBL_PAUSE_COMMAND);
    verify(controllerListener, times(1)).controlStateChange(COMM_SENDING_PAUSED);
}
Also used : ControllerListener(com.willwinder.universalgcodesender.listeners.ControllerListener) GcodeCommand(com.willwinder.universalgcodesender.types.GcodeCommand) Test(org.junit.Test) GcodeStreamTest(com.willwinder.universalgcodesender.utils.GcodeStreamTest)

Example 19 with GcodeCommand

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

the class BufferedCommunicatorTest method responseMessageOnErrorShouldDispatchPauseEvent.

@Test
public void responseMessageOnErrorShouldDispatchPauseEvent() {
    // Given
    Connection connection = mock(Connection.class);
    instance.setConnection(connection);
    SerialCommunicatorListener serialCommunicatorListener = mock(SerialCommunicatorListener.class);
    instance.addCommandEventListener(serialCommunicatorListener);
    asl.add(new GcodeCommand("G0"));
    asl.add(new GcodeCommand("G0"));
    instance.streamCommands();
    // When
    instance.responseMessage("error");
    // Then
    assertTrue(instance.isPaused());
    verify(serialCommunicatorListener, times(1)).communicatorPausedOnError();
}
Also used : SerialCommunicatorListener(com.willwinder.universalgcodesender.listeners.SerialCommunicatorListener) Connection(com.willwinder.universalgcodesender.connection.Connection) GcodeCommand(com.willwinder.universalgcodesender.types.GcodeCommand) Test(org.junit.Test) GcodeStreamTest(com.willwinder.universalgcodesender.utils.GcodeStreamTest)

Example 20 with GcodeCommand

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

the class BufferedCommunicatorTest method softResetShouldClearBuffersAndResumeOperation.

@Test
public void softResetShouldClearBuffersAndResumeOperation() {
    // Given
    Connection connection = mock(Connection.class);
    instance.setConnection(connection);
    asl.add(new GcodeCommand("G0"));
    cb.add("G0");
    instance.pauseSend();
    assertTrue(instance.isPaused());
    assertEquals(1, instance.numActiveCommands());
    assertEquals(1, instance.numBufferedCommands());
    // When
    instance.softReset();
    // Then
    assertFalse("The communicator should resume operation after a reset", instance.isPaused());
    assertEquals("There should be no active commands", 0, instance.numActiveCommands());
    assertEquals("There should be no buffered manual commands", 0, instance.numBufferedCommands());
}
Also used : Connection(com.willwinder.universalgcodesender.connection.Connection) GcodeCommand(com.willwinder.universalgcodesender.types.GcodeCommand) 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