From 41b467352db056a250f43faf4eff7511f61f76b4 Mon Sep 17 00:00:00 2001 From: Joacim Breiler Date: Thu, 4 Jul 2024 17:24:19 +0200 Subject: [PATCH] Attempt to warn user if permissions to device is missing. And prevent the user from starting a stream twice when the machine is waiting for G4 dwell command (#2567) --- .../connection/JSerialCommConnection.java | 17 +++++++++++++++++ .../universalgcodesender/model/GUIBackend.java | 5 +++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/ugs-core/src/com/willwinder/universalgcodesender/connection/JSerialCommConnection.java b/ugs-core/src/com/willwinder/universalgcodesender/connection/JSerialCommConnection.java index 643ec3089e..aa3d6bd9ce 100644 --- a/ugs-core/src/com/willwinder/universalgcodesender/connection/JSerialCommConnection.java +++ b/ugs-core/src/com/willwinder/universalgcodesender/connection/JSerialCommConnection.java @@ -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; @@ -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); } @@ -72,6 +76,8 @@ 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); @@ -79,6 +85,17 @@ private void initSerialPort(String name, int baud) throws Exception { 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) { diff --git a/ugs-core/src/com/willwinder/universalgcodesender/model/GUIBackend.java b/ugs-core/src/com/willwinder/universalgcodesender/model/GUIBackend.java index b3ab94f3bf..738a57da00 100644 --- a/ugs-core/src/com/willwinder/universalgcodesender/model/GUIBackend.java +++ b/ugs-core/src/com/willwinder/universalgcodesender/model/GUIBackend.java @@ -617,7 +617,8 @@ public boolean canCancel() { @Override public boolean canSend() { return isIdle() && - this.gcodeFile != null; + this.gcodeFile != null && + (controller != null && !controller.isStreaming()); } @Override @@ -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; }