0

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

1 回答 1

0

看看这个使用套接字的客户端/服务器实现示例和这个简单的客户端服务器聊天程序也使用套接字

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

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

于 2013-03-14T12:49:31.537 回答