diff --git a/tools/cooja/apps/serial_socket/java/org/contikios/cooja/serialsocket/SerialSocketClient.java b/tools/cooja/apps/serial_socket/java/org/contikios/cooja/serialsocket/SerialSocketClient.java index e074437d2..113730952 100644 --- a/tools/cooja/apps/serial_socket/java/org/contikios/cooja/serialsocket/SerialSocketClient.java +++ b/tools/cooja/apps/serial_socket/java/org/contikios/cooja/serialsocket/SerialSocketClient.java @@ -115,10 +115,12 @@ public class SerialSocketClient extends VisPlugin implements MotePlugin { private DataOutputStream out; private final Mote mote; + private final Simulation simulation; public SerialSocketClient(Mote mote, Simulation simulation, final Cooja gui) { super("Serial Socket (CLIENT) (" + mote + ")", gui, false); this.mote = mote; + this.simulation = simulation; /* GUI components */ if (Cooja.isVisualized()) { @@ -411,9 +413,19 @@ public class SerialSocketClient extends VisPlugin implements MotePlugin { } if (numRead >= 0) { - for (int i = 0; i < numRead; i++) { - serialPort.writeByte(data[i]); - } + final int finalNumRead = numRead; + final byte[] finalData = data; + /* We are not on the simulation thread */ + simulation.invokeSimulationThread(new Runnable() { + + @Override + public void run() { + for (int i = 0; i < finalNumRead; i++) { + serialPort.writeByte(finalData[i]); + } + } + }); + inBytes += numRead; if (Cooja.isVisualized()) { socketToMoteLabel.setText(inBytes + " bytes"); diff --git a/tools/cooja/apps/serial_socket/java/org/contikios/cooja/serialsocket/SerialSocketServer.java b/tools/cooja/apps/serial_socket/java/org/contikios/cooja/serialsocket/SerialSocketServer.java index e3aabbc15..fa55da74b 100644 --- a/tools/cooja/apps/serial_socket/java/org/contikios/cooja/serialsocket/SerialSocketServer.java +++ b/tools/cooja/apps/serial_socket/java/org/contikios/cooja/serialsocket/SerialSocketServer.java @@ -113,10 +113,12 @@ public class SerialSocketServer extends VisPlugin implements MotePlugin { private Socket clientSocket; private Mote mote; + private Simulation simulation; public SerialSocketServer(Mote mote, Simulation simulation, final Cooja gui) { super("Serial Socket (SERVER) (" + mote + ")", gui, false); this.mote = mote; + this.simulation = simulation; updateTimer.start(); @@ -475,10 +477,19 @@ public class SerialSocketServer extends VisPlugin implements MotePlugin { logger.info("Forwarder: socket -> serial port"); while (numRead >= 0) { - for (int i = 0; i < numRead; i++) { - serialPort.writeByte(data[i]); - } - inBytes += numRead; + final int finalNumRead = numRead; + final byte[] finalData = data; + /* We are not on the simulation thread */ + simulation.invokeSimulationThread(new Runnable() { + + @Override + public void run() { + for (int i = 0; i < finalNumRead; i++) { + serialPort.writeByte(finalData[i]); + } + inBytes += finalNumRead; + } + }); try { numRead = in.read(data);