2013-03-10 166 views
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"); 
} 
} 
+0

如果有斷開連接,是否有異常?然後,您可以捕捉它並處理重新連接併爲您的圖形調用事件。 – jpee 2013-03-10 19:28:58

+0

@jpee,謝謝。我只是無法弄清楚代碼中的哪個部分顯示它斷開連接。那有意義嗎?我感覺如果我在代碼中發現它斷開連接的位置,我可以讓它在屏幕上打印一條消息,並在該行代碼的下面,再次調用initialize()。這是正確的想法嗎? – Patrick 2013-03-10 19:46:05

+0

聽起來不錯,不確定是否需要所有初始化方法。也許可以創建一個連接方法,它將從初始化和異常處理中調用。 – jpee 2013-03-10 19:51:56

回答

0

看一看這個example使用套接字客戶端/服務器實現的,也使用sockets這種簡單的客戶端服務器聊天程序。

將您的SerialPortEventListener放在服務器站點和客戶端的圖表上。您需要異步通信,服務器將消息廣播給所有客戶端,而不是使用雙向通信。

使用兩個獨立的主類來實現您的解決方案,您將能夠隨意停止和重新啓動eitehr部分。

相關問題