需要帮助从输入流读取数据

发布于 2024-09-11 23:45:29 字数 2529 浏览 4 评论 0 原文

我有一个机器人和一个在 GUI 上运行的 GUI 应用程序。我在机器人端有一个 while 循环,它不断地将数据发送到 GUI。

在发送值之前,我首先发送一个值,GUI 将使用该值来确定随后必须读取多少个连续值,例如我发送类似的内容;

dataout.writeInt(2);
dataout.writeInt(50);
dataout.writeInt(506);
dataout.writeInt(50);
dataout.flush 

这里 GUI 读取 2,然后在情况 2 下,它将读取接下来的两个整数。

在 GUI 方面,我有一个 while 循环,它位于连续从输入流读取的线程的 run() 中。

在 GUI 上的循环内,我有一个 switch case 语句。

示例

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;

}

} 

t 没有按我想要的方式工作。这就是我得到的:

在读取第一个 int 后,我​​得到了它从输入流中读取的一系列数字。我不知道这些数字是从哪里来的。

我认为如果它无法读取我发送的号码,那么它一定会被阻止,但事实并非如此。

对于上面的例子,这就是我得到的:

2
1761635840
1946182912
1845523456
1761636096
1845523200
1006658048
16274152968 

2之后的所有数字,我不知道它们来自哪里。它不会读取我发送的 2 之后的数字。

我尝试插入一些 Thread.sleep(1000) 但不起作用。

我做错了什么?需要帮助

代码

//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

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.

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;

dataout.writeInt(2);
dataout.writeInt(50);
dataout.writeInt(506);
dataout.writeInt(50);
dataout.flush 

Here the GUI reads 2 and then under the case 2, it will read the next two integers.

On the GUI side i have i while loop that is in a run() of a thread that is reading from the inputstream continuosly.

Inside the loop on the GUI i have a switch case statement.

Example

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;

}

} 

t is not working as i want. This is what i get:

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.

I thought that if it cant read the numbers i send, then it must block, but it isn't.

For the example above this is what i get:

2
1761635840
1946182912
1845523456
1761636096
1845523200
1006658048
16274152968 

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.

I tried to insert some Thread.sleep(1000) but is not working.

What am i doing wrong? Need help

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尽揽少女心 2024-09-18 23:45:29

intentions 文本对您来说有什么意义吗?您正在从输入流中读取该内容;这就是这些数字在特定字符集中对应的内容。我的猜测是您已经向输出流写入了一些 HTML 内容。您确定只写入 DataOutputStream 而不是同时写入底层 OutputStream 吗?另外,这真的是你读到的代码的样子吗?如果是这样,readInt()方法是如何定义的?

编辑:用于解决上述问题的片段。

int input = 184549376 ;
byte[] bytes = { (byte)(input >> 24), (byte)(input >> 16),
        (byte)(input >> 8), (byte)(input) };
System.out.printf("int: %d hex: %08X string: %s",
        input, input, new String(bytes));

编辑#2:在您的代码中,您使用 writeInt() 编写并使用 read() 读取,这是完全 > 我所说的那种非对称性。您必须使用readInt()来读取使用writeInt()写入的字段。 InputStream.read (),您正在使用的,读取一个字节数据并将其存储在一个 int 中。不是你想要的。

Does the text intentions<b mean anything to you? You're reading that from your input stream; that's what those numbers correspond to in a certain character set. My guess is you've got a bit of HTML writing to your output stream. Are you sure you're ONLY writing to the DataOutputStream and not also the underlying OutputStream at the same time? Also, is that really what you read code looks like? If so, how is the readInt() method defined?

EDIT: Snippit for working out the above.

int input = 184549376 ;
byte[] bytes = { (byte)(input >> 24), (byte)(input >> 16),
        (byte)(input >> 8), (byte)(input) };
System.out.printf("int: %d hex: %08X string: %s",
        input, input, new String(bytes));

EDIT #2: In your code, you write with writeInt() and read with read() This is exactly the sort of non-symmetry I was talking about. You must use readInt() to read a field that was written with writeInt(). InputStream.read(), what you are using, reads one byte of data and stores it in an int. Not what you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文