Note that there are some explanatory texts on larger screens.

plurals
  1. PONeed Help Reading Data From Inputstream
    text
    copied!<p>I have a robot and a GUI application running on a GUI. I have a while loop on the robot side that is constantly sending data to the GUI.</p> <p>Before i send a value, i send first a value which the GUI will use to determine how many consecutive values it must read afterwards for instance i send something like;</p> <pre><code>dataout.writeInt(2); dataout.writeInt(50); dataout.writeInt(506); dataout.writeInt(50); dataout.flush </code></pre> <p>Here the GUI reads 2 and then under the case 2, it will read the next two integers.</p> <p>On the GUI side i have i while loop that is in a run() of a thread that is reading from the inputstream continuosly.</p> <p>Inside the loop on the GUI i have a switch case statement.</p> <p>Example </p> <pre><code>while(true){ int val = dataIn.readIn() switch(val){ case 1: int color = readInt(); break; case 2: int me= readInt(); int you= readInt(); break; case 3: int megg = readInt(); int youss = readInt(); int mes = readInt(); int youe = readInt(); break; } } </code></pre> <p>t is not working as i want. This is what i get:</p> <p>After it reads the first int, i get a series of numbers that it is reading from the inputstream. i don't know where those numbers come from.</p> <p>I thought that if it cant read the numbers i send, then it must block, but it isn't.</p> <p>For the example above this is what i get:</p> <pre><code>2 1761635840 1946182912 1845523456 1761636096 1845523200 1006658048 16274152968 </code></pre> <p>All the numbers after the 2, i don't know where they come from. it doesn't read the numbers after the 2 i send.</p> <p>I tried to insert some Thread.sleep(1000) but is not working.</p> <p>What am i doing wrong? Need help</p> <p>CODE</p> <pre><code>//This code on the robot public class ForkliftColorSensorReader implements Runnable{ DataOutputStream outputStream; ColorSensor colorSensor; public ForkliftColorSensorReader(ColorSensor colorSensor, DataOutputStream outputStream) { this.outputStream = outputStream; this.colorSensor = colorSensor; } public void run() { int code = 1; while (code == 1){ try { Thread.sleep(1000); outputStream.writeInt(10); outputStream.flush(); outputStream.writeInt(2); outputStream.flush(); } catch (Exception e) { } } try { Thread.sleep(1000); outputStream.writeInt(20); outputStream.flush(); outputStream.writeInt(4); outputStream.flush(); } catch (Exception e) { } } } } //This code on the GUI public class Receive implements Runnable{ int num = this.dataIn.readInt(); public void run(){ switch(num){ case 10: int color = this.dataIn.read(); break; case 20: int c = this.dataIn.read(); break; default; } } } // I am using NXTConnector from the GUI to make the connection to the robot. //I then use the DataOutputstream from the connection to read the data </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload