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();
}
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;
}
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"));
}
}
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;
}
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());
}
}
Aggregations