Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
连接到串行端口的Java GUI_Java_User Interface_Connection_Serial Port - Fatal编程技术网

连接到串行端口的Java GUI

连接到串行端口的Java GUI,java,user-interface,connection,serial-port,Java,User Interface,Connection,Serial Port,我在Netbeans上制作了一个java GUI,运行在一台将通过串行端口连接到设备的计算机上。此GUI用于显示将存储在其他设备中的用户信息。例如文本文件或从设备的传感器检索一些数据。我刚刚完成了接口的概述,现在是更难的部分,因为这是第一次处理串行端口。另一个设备是beagleboard。 我甚至有点不确定从哪里开始,正如我在这个论坛上看到的,但似乎没有什么与我的问题完全相关。我假设我应该通过端口与设备建立连接,并根据用户的请求发送信息请求 有什么建议吗? 是否应该在程序的单独java类中建立连

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

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

提前感谢,, 亚历克斯

是否应该在程序的单独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;
                    }
                }
            }
        }
    }
}

“gui由一个主JFrame和两个辅助JFrame组成”参见@Andrew Thompson:有人正在积极地教人们使用多个JFrame。当我看到你写下或写下自己关于一个且只有一个JFrame时,我感觉自己像一条大马哈鱼,游向上游。@GilbertLeBlanc“当我看到你写下”哦,对我来说,这是我“最常用的行”的复制/粘贴。每次我用它的时候,我都懒得打字@安德烈霍姆普森:谢谢你的建议,我想我得调查一下。首先,它看起来并不友好。谢谢你,我将不得不研究一下,看看使用套接字是否有效。然后,我主要看到有人推荐使用javax.comm或clone RXTX。看起来我还需要研究多个JFrame…现在我有时间使用代码并开始理解你把什么放在一起了,我还有一些额外的问题。私有静态应用程序InstanceListener子Listener;这将是项目中另一个等待创建实例的类?还有一件事:日食,这是从哪里来的?这是用来存储错误的日志文件吗?提前感谢,,Alex@alex91:EclipseLogging是我专门为显示和记录Eclipse插件错误而编写的Eclipse插件的一部分。ApplicationInstanceListener是我用一个方法newInstanceCreated()创建的侦听器。我编写的实际侦听器代码是另一个Eclipse插件应用程序类的一部分,用于强制当前Eclipse窗口成为焦点。您的侦听器代码显然会有所不同。