【问题标题】:Re-connecting to serial port after a disconnection断线后重连串口
【发布时间】:2013-03-14 20:49:31
【问题描述】:

嘿,我正在做一个程序,我在其中读取串行数据并将其显示在图表上。我的 java 项目中有两个文件:一个负责图形显示,另一个仅用于串行处理。该程序的要求之一是让我的 arduino 发送数据断开连接,在图表上显示一条消息,重新连接 arduino,然后让该消息消失并在没有用户交互的情况下再次发送数据。我在下面有我的串行文件。我不确定如何编辑它以查看它已与 PC 断开连接,然后在没有人工交互的情况下重新连接。谢谢

public class SerialTest implements SerialPortEventListener {
SerialPort serialPort;
private static final String PORT_NAMES[] = { 

        "COM11", // Windows
};
String turnon = "X";
String inputLine;
String temp;
String temp1;
String thirdDC;
static boolean deviceConn;

int value = 0;
int count;
String stop = "STOP";
String y = "Y";
String z = "Z";
String pop;
Double x;
int n;
int working;
private BufferedReader input;
/** The output stream to the port */
OutputStream output;
/** Milliseconds to block while waiting for port open */
private static final int TIME_OUT = 2000;
/** Default bits per second for COM port. */
private static final int DATA_RATE = 9600;

public void initialize() {
    working = 1;
    CommPortIdentifier portId = null;
    Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
    while (portEnum.hasMoreElements()) {
        CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
        for (String portName : PORT_NAMES) {
            if (currPortId.getName().equals(portName)) {
                portId = currPortId;
                break;
            }
        }
    }
    if (portId == null) {
        thirdDC = "Could not find COM Port. Error";
        System.out.println(thirdDC);

        return;
    }

    try {

        serialPort = (SerialPort) portId.open(this.getClass().getName(),
                TIME_OUT);

        serialPort.setSerialPortParams(DATA_RATE,
                SerialPort.DATABITS_8,
                SerialPort.STOPBITS_1,
                SerialPort.PARITY_NONE);

        input = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
        output = serialPort.getOutputStream();


        serialPort.addEventListener(this);
        serialPort.notifyOnDataAvailable(true);

    } catch (Exception e) {
        System.err.println(e.toString() + "BLAHHHHH");
    }
}


public synchronized void close() {
    if (serialPort != null) {
        serialPort.removeEventListener();
        serialPort.close();
    }
}

public synchronized void serialEvent(SerialPortEvent oEvent) {
    String stop = "ERROR";
    if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
        deviceConn = true;
        try {

            inputLine=input.readLine();
            if (inputLine.equals(y)){
                System.out.println(y);
                temp1 = "OFF";
                value = 0;
            }
            if (inputLine.equals(z)){
                System.out.println(z);
                temp1 = "ON";
                value = 0;
            }
            if (inputLine.equals(stop)){
                System.out.println(stop);
                temp = "ERROR";
                value = 1;  
            }


            x = Double.parseDouble(inputLine);
            if (x > 0){
            temp = inputLine;
            System.out.println(x);
            value = 0;
            }

        } catch (Exception e) {                             
        }
    }           
    }

public static void main(String[] args) throws Exception {
    Thread t=new Thread() {
        public void run() {
            try {
                Thread.sleep(1000);
        } 
            catch (InterruptedException ie) {}
        }
    };
    t.start();
    System.out.println("Started");
}
}

【问题讨论】:

  • 如果有断线,是不是有异常?然后你可以捕获它并处理重新连接并为你的图表调用一个事件。
  • @jpee,谢谢。我只是无法弄清楚它在代码中的哪个位置显示它断开连接。那有意义吗?我觉得如果我在代码中发现它断开连接的位置,我可以让它在屏幕上打印一条消息,并在该代码行的正下方,再次调用 initialize()。这是正确的想法吗?
  • 听起来不错,不确定是否需要所有的初始化方法。也许可以创建一个连接方法,该方法将从初始化和异常处理中调用。
  • 会是像this 这样的例子吗?我正在努力追随它,但我的程序中与此相关的部分确实得到了 100%。

标签: java serial-port arduino jfreechart


【解决方案1】:

查看使用套接字的客户端/服务器实现的 example 和同样使用 sockets 的简单客户端服务器聊天程序。

将您的SerialPortEventListener 放在服务器站点上,将图表放在客户端。您需要异步通信,服务器将消息广播给所有客户端,而不是使用双向通信。

使用两个单独的主类实现您的解决方案,您将能够随意停止和重新启动 eitehr 部分。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-20
    • 1970-01-01
    • 1970-01-01
    • 2012-08-07
    • 2019-02-22
    • 2013-11-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多