public static synchronized void send(String command) throws ComInterruptException { if (!isConnected()) { disconnect(); throw new ComInterruptException("Not connected !"); } // No command if (command.trim().equals("")) { return; } while ((I().internal_isbusy() || initThread.isAlive()) && isConnected()) { try { Communication.class.wait(); } catch (InterruptedException ex) { throw new ComInterruptException("Interrupt!"); } } if (!isConnected()) { throw new ComInterruptException("Not connected !"); } doSendEvent(command); try { I().internal_send(command); } catch (MyException ex) { Logger.getLogger(Communication.class.getName()).log(Level.SEVERE, null, ex); resetAll(); doChangedEvent(ex.getMessage()); throw new ComInterruptException(ex.getMessage()); } }
private static synchronized void receive(String[] lines) { // remove emty lines ArrayList<String> l = new ArrayList<>(); for (String s : lines) if (!s.equals("")) l.add(s); lines = l.toArray(new String[0]); doResiveEvent(lines); for (String line : lines) { try { I().internal_receive(line); } catch (MyException ex) { Logger.getLogger(Communication.class.getName()).log(Level.SEVERE, null, ex); resetAll(); doChangedEvent(ex.getMessage()); return; } Double val = I().internal_ZEndStopHit(line); if (val != null) { doEnstopHitEvent(val); } } }
public static synchronized void disconnect() { resetAll(); doChangedEvent("Disconnected!"); }
public static synchronized void connect(final String port, final int speed) { // allready connected if (isConnected()) { doChangedEvent(status); return; } // allready running init if (core.isSimulation() && initThread.isAlive()) { return; } // Prepair connection resetAll(); doChangedEvent("Connecting ... "); // Do not block GUI: initThread = new Thread( new Runnable() { @Override public void run() { synchronized (Communication.class) { // Try to open Port try { core = AComCore.openPort( new IReceivedLines() { @Override public void received(String[] lines) { Communication.receive(lines); } }, new IDisconnect() { @Override public void disconnect(String status) { synchronized (Communication.class) { resetAll(); doChangedEvent(status); } } }, port, speed); } catch (Exception ex) { resetAll(); doChangedEvent(ex.getMessage()); return; } try { // Run connect I().internal_connect(); } catch (InterruptedException | MyException ex) { Logger.getLogger(Communication.class.getName()).log(Level.SEVERE, null, ex); resetAll(); doChangedEvent(ex.getMessage() == null ? status : ex.getMessage()); return; } doChangedEvent("Connected!"); Communication.class.notify(); } } }); initThread.start(); // Wait for initThread to be started while (initThread.getState() == Thread.State.NEW) { try { Thread.sleep(1); } catch (InterruptedException ex) { Logger.getLogger(Communication.class.getName()).log(Level.SEVERE, null, ex); } } }