2

我是一名学习移动通信和电子工程的成熟学生,这使我能够学习 Java,但我并不像我想要的那样熟练,但这就是这篇文章的目的。

我的delima是我想编写一个Java程序,使用tcp连接连接到我的无线到串行适配器(Wiz6000),但问题是我有我认为在创建连接时有效的代码但是当我将USB连接到串行转换器到适配器的串行输出并与超级终端建立链接,wifi 到串行适配器没有接收到数据。

我已经在下面包含了到目前为止的代码

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

public class Client1 {

    // Modify this value (xxx.xxx.x.xxx) to the IP address you want your client to connect to (the server’s IP address)
    static String hostString = "192.168.1.254"; 

    // Modify this value (xxxxx) to the port number that the server is accepting connections on
    static int portInt = 5000;  

    static Socket connectionSocket = null;
    static PrintWriter out = null;
    static BufferedReader in = null;
    static String fromUserString = null;
    static String fromServer = null;
    static BufferedReader stdIn = null;
    static String hostInetAddressString = null;  
    public int data;    

    public static void main(String[] args) {
        try {
            connectionSocket = new Socket(hostString, portInt);
            hostString = connectionSocket.getInetAddress().toString();
            out = new PrintWriter(connectionSocket.getOutputStream(), true);
            in = new BufferedReader(new   InputStreamReader(connectionSocket.getInputStream()));

            // Get the Inet Address of the connectionSocket and store it in the hostInetAddress InetAddress variable and then cast the hostInetAddress to a String named hostInetAddressString
            hostInetAddressString = connectionSocket.getInetAddress().toString();
            System.out.println("Connected to server with Inet Address " +     hostInetAddressString + " on port: " + connectionSocket.getPort() + "\n");
        }
        catch (Exception e) {
            System.err.println(e);
            System.exit(1);
        }

        // This is used to allow the client to receive input based on the program’s user pressing keys on their keyboard

        /* stdIn = new BufferedReader(new InputStreamReader(System.in));
        while (true) {
        try {
        if (fromUserString != null) {
        int data=Integer.parseInt(fromUserString);
        out.println(fromUserString);
        fromUserString = null;
        }   
        }
        catch (Exception e) {
        System.err.println(e);
        break;
        }
        }*/
        try
        {
            // Create a new instance of a OutputStreamWriter object
            // attached to a ByteArrayOutputStream.
            ByteArrayOutputStream out = new ByteArrayOutputStream();

            OutputStreamWriter writer = new OutputStreamWriter(out, "ASCII");

            // Write to the output stream.
            String s = "Hello World";
            writer.write(s);
            writer.flush();

            // Display the contents of the ByteArrayOutputStream.
            System.out.println(out.toString());

            // Display the encoding being used.
            System.out.println("encoding: " + writer.getEncoding());

            // Close the OutputStreamWriter object.
            writer.close();
        }
        catch (IOException ex)
        {
            System.out.println(ex.toString());
        }
    }

上周我一直在查看不同的站点,这些站点正在阅读 Java 套接字客户端和服务器、串行通信和序列化,但我只是不知道该采用哪种方式。

这样做的主要目的是创建一个连接到这个 wifi 到串行适配器的应用程序,并从用户那里获取信息并通过 wifi 连接发送它,其中 wifi 到串行的另一端连接到控制数字显示器的 PCB . PCB 由我兼职工作的一家电子公司制造,其设置方式可以通过串行连接接收命令。

所以简而言之,我希望应用程序通过无线连接连接到数字显示器并将数据发送出去以进行显示。

4

0 回答 0