Skip to content

Commit

Permalink
Attempt to warn user if permissions to device is missing. And prevent…
Browse files Browse the repository at this point in the history
… the user from starting a stream twice when the machine is waiting for G4 dwell command
  • Loading branch information
breiler committed Jul 4, 2024
1 parent 72f96f1 commit 264cd09
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ This file is part of Universal Gcode Sender (UGS).
import com.fazecast.jSerialComm.SerialPort;
import com.fazecast.jSerialComm.SerialPortDataListener;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.SystemUtils;

import java.io.File;
import java.util.Arrays;
import java.util.List;

Expand All @@ -48,6 +50,8 @@ public void setUri(String uri) {
String portName = StringUtils.substringBetween(uri, ConnectionDriver.JSERIALCOMM.getProtocol(), ":");
int baudRate = Integer.parseInt(StringUtils.substringAfterLast(uri, ":"));
initSerialPort(portName, baudRate);
} catch (ConnectionException e) {
throw e;
} catch (Exception e) {
throw new ConnectionException("Couldn't parse connection string " + uri, e);
}
Expand All @@ -72,13 +76,26 @@ private void initSerialPort(String name, int baud) throws Exception {
}

serialPort = SerialPort.getCommPort(name);
checkPermissions();

serialPort.setParity(SerialPort.NO_PARITY);
serialPort.setNumStopBits(SerialPort.ONE_STOP_BIT);
serialPort.setNumDataBits(8);
serialPort.addDataListener(this);
serialPort.setBaudRate(baud);
}

private void checkPermissions() {
if (!SystemUtils.IS_OS_LINUX) {
return;
}

File port = new File(serialPort.getSystemPortPath());
if (!port.canWrite() || !port.canRead() ) {
throw new ConnectionException("Do not have required permissions to open the device on " + serialPort.getSystemPortPath());
}
}

@Override
public void closePort() throws Exception {
if (serialPort != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,8 @@ public boolean canCancel() {
@Override
public boolean canSend() {
return isIdle() &&
this.gcodeFile != null;
this.gcodeFile != null &&
(controller != null && !controller.isStreaming());
}

@Override
Expand Down Expand Up @@ -762,7 +763,7 @@ private boolean openCommConnection(String port, int baudRate) throws Exception {
disconnect();
logger.log(Level.INFO, "Exception in openCommConnection.", e);
throw new Exception(Localization.getString("mainWindow.error.connection")
+ ": " + e.getMessage());
+ ": " + e.getMessage(), e);
}
return connected;
}
Expand Down

0 comments on commit 264cd09

Please sign in to comment.