2016-10-13 8 views
-1

난 내 JFrame의로 다른 클래스의 메소드를 호출 할 수있는 문제가자바 + 우노 + RFID : 호출 방법은 자바 RFID를 읽어

내가이 포럼에서 다른 사람에서 가져온이 내 방법 클래스

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 

package transaksi_satu; 

import java.io.BufferedReader; 
import java.io.InputStreamReader; 
import java.io.OutputStream; 
import gnu.io.CommPortIdentifier; 
import gnu.io.SerialPort; 
import gnu.io.SerialPortEvent; 
import gnu.io.SerialPortEventListener; 
import java.util.Enumeration; 


public class UnoConnect implements SerialPortEventListener{ 

    SerialPort serialPort; 

    private static final String PORT_NAMES[] = {"COM3"}; 
    private BufferedReader input; 
    private OutputStream output1; 
    private static final int TIME_OUT = 2000; 
    private static final int DATA_RATE = 9600; 

    public UnoConnect(){ 
    CommPortIdentifier portId = null; 
    Enumeration portEnum = CommPortIdentifier.getPortIdentifiers(); 

    //First, Find an instance of serial port as set in PORT_NAMES. 
    while (portEnum.hasMoreElements()) { 
     CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement(); 
     for (String portName : PORT_NAMES) { 
      if (currPortId.getName().equals(portName)) { 
       portId = currPortId; 
       break; 
      } 
     } 
    } 
    if (portId == null) { 
     System.out.println("Could not find COM port."); 
     return; 
    } 

    try { 
     serialPort = (SerialPort) portId.open(this.getClass().getName(), 
       TIME_OUT); 
     serialPort.setSerialPortParams(DATA_RATE, 
       SerialPort.DATABITS_8, 
       SerialPort.STOPBITS_1, 
       SerialPort.PARITY_NONE); 

     // open the streams 
     input = new BufferedReader(new InputStreamReader(serialPort.getInputStream())); 
     output1 = serialPort.getOutputStream(); 

     serialPort.addEventListener(this); 
     serialPort.notifyOnDataAvailable(true); 
    } catch (Exception e) { 
     System.err.println(e.toString()); 
    } 



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

public void serialEvent(SerialPortEvent oEvent) { 
    if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) { 
     try { 
      String inputLine=null; 
      if (input.ready()) { 
       inputLine = input.readLine(); 
          System.out.println(inputLine); 
          //Lbl_ID.setText(inputLine);     
      } 

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

} 
} 

소스 코드의 맨 아래에있는 "public void serialEvent (SerialPortEvent oEvent)"함수에서 결과를 얻고 싶습니다. 다른 것으로 호출하려고하면 그 코드가 나타나지 않습니다.

나는 어떤 실수를합니까? 누군가 나를 도울 수 있습니까?

+0

디버깅을 했습니까? 어쩌면 예외적 인 '시리얼 이벤트'가 호출되지 않았을 수도 있습니다. – Beloo

+0

@Beloo 예, 결과가 "Build Successfull"입니다. –

+0

디버깅이 아니라 건물입니다. 디버깅은 실행중인 응용 프로그램에 디버거를 연결하고 각 행의 입력 및 출력 결과를 확인하기 위해 단계별로 프로그램 작업을 수행하는 과정입니다. – Beloo

답변

0

방법 public void serialEvent(SerialPortEvent oEvent)은 직렬 포트에서 무엇인가를 받았을 때 실행됩니다 (사용자가 직접 호출하지 않기 위해). 수신 된 내용을 처리하고 그림을 그릴 필요가 있습니다 (예를 들어주의를 기울이십시오 내부의 메모에 수신 된 데이터로 무엇인가해야합니다.) :

public void serialEvent(SerialPortEvent oEvent) { 
    if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) { 
     try { 
      String inputLine=null; 
      if (input.ready()) { 
       inputLine = input.readLine(); 
          System.out.println(inputLine); 
          // DO SOMETHING WITH THE inputLine RECEIVED HERE 
          // EG. PASS IT TO SOME OTHER SERVICE METHOD 

      } 

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

} 
+0

도와 주시겠습니까? 메서드에있는 경우 다른 서비스 메서드에 결과를 전달 ??? –

+0

나는 며칠 동안 여기에 문제가있어 두통을 겪는다. –

+0

(해당 서비스에 대한 적절한 방법을 호출하기 만하면) 해당 서비스에 대한 참조를 수신기에 추가한다. 이 리스너를 적절한 직렬 포트 (이전에 생성 된)에 등록하여 이벤트를 수신해야합니다. 당신이 사용하는 라이브러리의 문서를보십시오. –