0

我在 Netbeans 上制作了一个 Java GUI,以便在将通过串行端口连接到设备的计算机上运行。此 GUI 用于显示将存储在其他设备中的用户信息。像文本文件或从设备的传感器检索一些数据。我刚刚完成了接口的轮廓,现在是比较难的部分,因为这是第一次处理串口。另一个设备是beagleboard。我什至不确定从哪里开始,因为我在这个论坛上看过,但似乎没有什么与我的问题完全相关。我假设我应该通过端口与设备建立连接,并根据用户请求发送信息请求。

有什么建议吗?是否应该在程序的单独 java 类中建立连接?gui由一个主JFrame和两个辅助JFrame组成,我(计划)基本上有一个按钮来建立主框架中的连接。

提前致谢, 亚历克斯

4

1 回答 1

1

是否应该在程序的单独 java 类中建立连接?

是的。一个 java 类应该足以建立连接、向 beagleboard 发送消息并接收响应。

我假设我应该通过端口与设备建立连接,并根据用户请求发送信息请求。

是的。您可以将请求格式化为 beagleboard 所需的任何格式。

这是我整理的一个类,以确保 Java 应用程序的单个实例运行。您将修改此为您的通信。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

public class ApplicationInstanceManager {

    private static final boolean DEBUG = false;

    private static ApplicationInstanceListener subListener;

    /** Randomly chosen, but static, high socket number */
    public static final int SINGLE_INSTANCE_NETWORK_SOCKET = 44331;

    /** Must end with newline */
    public static final String SINGLE_INSTANCE_SHARED_KEY = "$$RabidNewInstance$$\n";

    /**
     * Registers this instance of the application.
     * 
     * @return true if first instance, false if not.
     */
    public static boolean registerInstance() {
        // returnValueOnError should be true if lenient (allows app to run on
        // network error) or false if strict.
        boolean returnValueOnError = true;
        // try to open network socket
        // if success, listen to socket for new instance message, return true
        // if unable to open, connect to existing and send new instance message,
        // return false
        try {
            final ServerSocket socket = new ServerSocket(
                    SINGLE_INSTANCE_NETWORK_SOCKET, 10, InetAddress
                            .getLocalHost());
            if (DEBUG)
                System.out.println("Listening for application instances on socket "
                    + SINGLE_INSTANCE_NETWORK_SOCKET);
            Thread instanceListenerThread = new InstanceListenerThread(socket);
            instanceListenerThread.start();
            // listen
        } catch (UnknownHostException e) {
            EclipseLogging.logError(RabidPlugin.getDefault(),
                    RabidPlugin.PLUGIN_ID, e);
            return returnValueOnError;
        } catch (IOException e) {
            return portTaken(returnValueOnError, e);

        }
        return true;
    }

    private static boolean portTaken(boolean returnValueOnError, IOException e) {
        if (DEBUG)
            System.out.println("Port is already taken.  "
                    + "Notifying first instance.");
        try {
            Socket clientSocket = new Socket(InetAddress.getLocalHost(),
                    SINGLE_INSTANCE_NETWORK_SOCKET);
            OutputStream out = clientSocket.getOutputStream();
            out.write(SINGLE_INSTANCE_SHARED_KEY.getBytes());
            out.close();
            clientSocket.close();
            System.out.println("Successfully notified first instance.");
            return false;
        } catch (UnknownHostException e1) {
            EclipseLogging.logError(RabidPlugin.getDefault(),
                    RabidPlugin.PLUGIN_ID, e);
            return returnValueOnError;
        } catch (IOException e1) {
            EclipseLogging
                    .logError(
                            RabidPlugin.getDefault(),
                            RabidPlugin.PLUGIN_ID,
                            "Error connecting to local port for single instance notification",
                            e);
            return returnValueOnError;
        }
    }

    public static void setApplicationInstanceListener(
            ApplicationInstanceListener listener) {
        subListener = listener;
    }

    private static void fireNewInstance() {
        if (subListener != null) {
            subListener.newInstanceCreated();
        }
    }

    public static void main(String[] args) {
        if (!ApplicationInstanceManager.registerInstance()) {
            // instance already running.
            System.out.println("Another instance of this application " + 
                    "is already running.  Exiting.");
            System.exit(0);
        }
        ApplicationInstanceManager
                .setApplicationInstanceListener(new ApplicationInstanceListener() {
                    public void newInstanceCreated() {
                        System.out.println("New instance detected...");
                        // this is where your handler code goes...
                    }
                });
    }

    public static class InstanceListenerThread extends Thread {

        private ServerSocket socket;

        public InstanceListenerThread(ServerSocket socket) {
            this.socket = socket;
        }

        @Override
        public void run() {
            boolean socketClosed = false;
            while (!socketClosed) {
                if (socket.isClosed()) {
                    socketClosed = true;
                } else {
                    try {
                        Socket client = socket.accept();
                        BufferedReader in = new BufferedReader(
                                new InputStreamReader(client.getInputStream()));
                        String message = in.readLine();
                        if (SINGLE_INSTANCE_SHARED_KEY.trim().equals(
                                message.trim())) {
                            if (DEBUG)
                                System.out.println("Shared key matched - "
                                        + "new application instance found");
                            fireNewInstance();
                        }
                        in.close();
                        client.close();
                    } catch (IOException e) {
                        socketClosed = true;
                    }
                }
            }
        }
    }
}
于 2012-06-12T16:26:43.657 回答