@@ -21,7 +21,9 @@ This file is part of Universal Gcode Sender (UGS).
2121import com .fazecast .jSerialComm .SerialPort ;
2222import com .fazecast .jSerialComm .SerialPortDataListener ;
2323import org .apache .commons .lang3 .StringUtils ;
24+ import org .apache .commons .lang3 .SystemUtils ;
2425
26+ import java .io .File ;
2527import java .util .Arrays ;
2628import java .util .List ;
2729
@@ -48,6 +50,8 @@ public void setUri(String uri) {
4850 String portName = StringUtils .substringBetween (uri , ConnectionDriver .JSERIALCOMM .getProtocol (), ":" );
4951 int baudRate = Integer .parseInt (StringUtils .substringAfterLast (uri , ":" ));
5052 initSerialPort (portName , baudRate );
53+ } catch (ConnectionException e ) {
54+ throw e ;
5155 } catch (Exception e ) {
5256 throw new ConnectionException ("Couldn't parse connection string " + uri , e );
5357 }
@@ -72,13 +76,26 @@ private void initSerialPort(String name, int baud) throws Exception {
7276 }
7377
7478 serialPort = SerialPort .getCommPort (name );
79+ checkPermissions ();
80+
7581 serialPort .setParity (SerialPort .NO_PARITY );
7682 serialPort .setNumStopBits (SerialPort .ONE_STOP_BIT );
7783 serialPort .setNumDataBits (8 );
7884 serialPort .addDataListener (this );
7985 serialPort .setBaudRate (baud );
8086 }
8187
88+ private void checkPermissions () {
89+ if (!SystemUtils .IS_OS_LINUX ) {
90+ return ;
91+ }
92+
93+ File port = new File (serialPort .getSystemPortPath ());
94+ if (!port .canWrite () || !port .canRead () ) {
95+ throw new ConnectionException ("Do not have required permissions to open the device on " + serialPort .getSystemPortPath ());
96+ }
97+ }
98+
8299 @ Override
83100 public void closePort () throws Exception {
84101 if (serialPort != null ) {
0 commit comments