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

发布于 2024-09-12 16:45:17 字数 2439 浏览 4 评论 0原文

昨天我发布了一个关于输入流读取问题的问题,我得到了帮助。

我发现自己处于类似的情况,但这次我知道我正在做正确的事情,但它对我不起作用。

我正在从输入流中读取数据,但得到不同的值。无论我如何更改发送的数据,我都会得到相同的字符串(“toForklift-42”)作为值。首先,我在发送的值前加上“toForklift-”前缀,但我已将该字符串更改为不同的字符串,但我在括号中得到相同的字符串。我什至将数字 42 更改为不同的数字,但是当我运行该程序时,我在控制台中得到相同的字符串。以下是发送的内容:作为

发送和接收的两个类。

我使用 leJOS NXJ NXTConnector 建立连接并打开流。

// sender class

class PanButton implements Runnable {

    DataInputStream dis;
    DataOutputStream dos;
    TouchSensor touch = new TouchSensor(SensorPort.S4);





    PanicButtonCrossing(DataInputStream is, DataOutputStream os) {
    dos = os;
    dis = is;
    }

    public void run() {

        while (!touch.isPressed())  {}

            // If you get a message: KILL EVERYTHING    
            Motor.A.stop();
            Motor.B.stop();
            Motor.C.stop();

            try { // send 42
                int value = 42;

                dos.writeChars("ggggggggg" + 455 + "\n"); 
                dos.flush();
                Sound.systemSound(true, 3);
            } catch (IOException ioe) {
                LCD.drawString("Write Exception", 0, 0);
            }

            System.exit(1);


}






// Reader classs



public class InputReaderCrossing  implements Runnable{


      private DataInputStream dataIn;
      private DataOutputStream dataOut;

public InputReaderCrossing(DataInputStream dataIn, DataOutputStream dataOut) {
            this.dataIn = dataIn;
            this.dataOut = dataOut;
            this.sensor = sensor;
            this.readLock = new Object();

        }


public void run(){

while(true){

String dataFromCrossing1 = readLineFromCrossing();
System.out.println("CROSSING VALUE: " + dataFromCrossing1  + "  :VALUEEEEE");
}

}

private String readLineFromCrossing() throws IOException{ 
              StringBuffer sb = new StringBuffer(); 

              synchronized(readLock){ 
                 while(true) { 
                    char c = this.dataIn.readChar(); 
                    if (c == '\n') break; 
                    sb.append(c);
                 }
                 return sb.toString(); 
              } 
           }


}

我需要你的帮助。我已经花了6个小时但找不到原因。我不明白,无论我发送什么,我都会得到“toForklift-42”。

起初我尝试使用 writeInt() 方法发送 42 但后来在阅读器类方面我使用 readInt() 但我得到类似的东西:

745687894
459877455
456987456

所以我更改为字符串以找出原因,瞧,我得到了该字符串无论我发送什么。就像该字符串固定在输入流中并且没有发送任何内容。我不知道发生了什么事。

需要帮助

Yesterday i posted a question about a problem i had concerning inputstream reading and i was helped.

I find myself in similar situation but this time i know that I am doing the right thing but yet it is not working for me.

I am reading from an inputstream but i get different value. No matter how i change the data i send i get the same string ("toForklift-42") as the value. At first i prefix the value i send with "toForklift-" but i have changed that string to different strings yet i get the same string in the bracket. i Even changed the number 42 to a different number but yet when i run the program, i get the same string in the console. Below is what am sending : as

Two classes for sending and receiving.

Am using the leJOS NXJ NXTConnector to make the connection and open the streams.

// sender class

class PanButton implements Runnable {

    DataInputStream dis;
    DataOutputStream dos;
    TouchSensor touch = new TouchSensor(SensorPort.S4);





    PanicButtonCrossing(DataInputStream is, DataOutputStream os) {
    dos = os;
    dis = is;
    }

    public void run() {

        while (!touch.isPressed())  {}

            // If you get a message: KILL EVERYTHING    
            Motor.A.stop();
            Motor.B.stop();
            Motor.C.stop();

            try { // send 42
                int value = 42;

                dos.writeChars("ggggggggg" + 455 + "\n"); 
                dos.flush();
                Sound.systemSound(true, 3);
            } catch (IOException ioe) {
                LCD.drawString("Write Exception", 0, 0);
            }

            System.exit(1);


}






// Reader classs



public class InputReaderCrossing  implements Runnable{


      private DataInputStream dataIn;
      private DataOutputStream dataOut;

public InputReaderCrossing(DataInputStream dataIn, DataOutputStream dataOut) {
            this.dataIn = dataIn;
            this.dataOut = dataOut;
            this.sensor = sensor;
            this.readLock = new Object();

        }


public void run(){

while(true){

String dataFromCrossing1 = readLineFromCrossing();
System.out.println("CROSSING VALUE: " + dataFromCrossing1  + "  :VALUEEEEE");
}

}

private String readLineFromCrossing() throws IOException{ 
              StringBuffer sb = new StringBuffer(); 

              synchronized(readLock){ 
                 while(true) { 
                    char c = this.dataIn.readChar(); 
                    if (c == '\n') break; 
                    sb.append(c);
                 }
                 return sb.toString(); 
              } 
           }


}

I need your help please. i have spent 6 hours but can't find the the reason. I don't understand no matter what i send i get "toForklift-42".

At first i tried to send the 42 with the writeInt() methods but then on the reader class side i use readInt() but i get somethings like:

745687894
459877455
456987456

So i changed to the string to find out why and lo and behold i get that string no matter what i send. it is like that string is fixed in the input stream and nothing is sent. I don't know what is happening.

Need help

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

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

发布评论

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

评论(1

梦冥 2024-09-19 16:45:17

这是一个非常令人困惑的问题。我们并不真正知道哪个进程正在写入哪些数据,以及数据如何传输,以及哪个进程正在接收数据。这里有一些相关的代码可供查看,但我没有看到足够的代码来讲述这个故事。 dataFromCrossing1 如何获取其值?

我认为以这样的方式组织你的问题会很好,这样你就可以向局外人提出问题。也许在这样做的过程中,您可能会自己偶然发现解决方案。

原则上,如果您在程序中更改字符串常量,但仍然看到相同的输出,那么发生的情况是

  • 原始程序仍在运行;或者
  • 自原始版本以来的所有版本都出现错误,因此您曾经编译成功的类文件会一次又一次地运行;或者
  • 你并没有真正运行你认为正在运行的程序。

This is a very confusing question. We don't really know which process is writing which data, and how it's being transmitted, and which process is picking it up. There's some relevant code to see here, but I didn't see enough to tell the story. How did dataFromCrossing1 get its value?

I think it would do good to organize your problem in such a way that you can ask an outsider a question about it. Possibly in the process of doing so you could stumble on the solution yourself.

In principle, if you are changing string constants in your program yet you continue to see the same output, then what's happening is that

  • The original program is still running; or
  • all versions since the original have had errors, so the class files you once compiled successfully are running again and again; or
  • you're not really running the program you think you're running.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文