簡體   English   中英

Labview與Java的TCP連接

[英]Labview TCP connection with Java

我正在嘗試通過TCP套接字從Labview發送數據,並使用Java接收數據。

我使用的是Labview的示例TCP VI(我無法發布圖片)。

我意識到有一個TCP讀取,但我還沒到那點。 我的問題是處理類型。

import java.io.*;
import java.net.*;

public class JavaApplication3 {
    public static void main(String[] args) throws IOException {

        String serverHostname = new String ("97.77.53.127");

        Socket echoSocket = null;
        PrintWriter out = null;
        BufferedReader in = null;

        try {
            echoSocket = new Socket(serverHostname, 6340);
            out = new PrintWriter(echoSocket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(
                                        echoSocket.getInputStream()));
        } catch (UnknownHostException e) {

            System.exit(1);
        } catch (IOException e) {
            System.exit(1);
        }

        BufferedReader stdIn = new BufferedReader(
                                       new InputStreamReader(System.in));
        String userInput;

        System.out.print ("input: ");
        while ((userInput = stdIn.readLine()) != null) {
            out.println(userInput);
            System.out.println("echo: " + in.readLine());
            System.out.print ("input: ");
        }

        out.close();
        in.close();
        stdIn.close();
        echoSocket.close();
    }
}

我要處理的第一個問題是,當我收到來自Labview VI的輸入時,會在Java程序中得到:

input: d
echo: ?��/�?�~gʕ ?�$���;P?��G��j�?��"�?�?��;���h?�
input: input: d
echo: ?��/�?�~gʕ ?�$���;P?��G��j�?��"�?�?��;���h?�
input: 

我以為我的問題出在類型轉換上,但我真的不知道如何解決。 任何幫助,將不勝感激。

好的,然后嘗試類似:

int temp = 0;
while( (temp = in.read()) != -1){
   System.out.print( (char)temp );
}

只需將其放在代碼中的某個位置即可。 這只是將返回的int強制轉換為char值,這將打印出字母。

讓我知道事情的后續。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM