断开连接后重新连接到串行端口

时间:2013-03-10 18:55:15

标签: java serial-port arduino jfreechart

嘿我正在做一个程序,我在其中读取串行数据并将其显示在图表上。我的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");
}
}

1 个答案:

答案 0 :(得分:0)

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

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

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