我已經啓動JAVA並使用RxTx進行串行通信。'this'關鍵字:使用它作爲參數
參考: http://rxtx.qbang.org/wiki/index.php/Event_based_two_way_Communication http://henrypoon.wordpress.com/2011/01/01/serial-communication-in-java-with-example-program/
在第二連桿我無法破譯的使用 '這個': 任何人都可以請解釋:
Communicator.java
public class Communicator implements SerialPortEventListener
{
GUI window = null;
..
..
public Communicator(GUI window)
{
this.window = window;
}
...
..
}
在GUI.java
public class GUI extends javax.swing.JFrame {
Communicator communicator = null;
Communicator communicator = null;
//KeybindingController object
KeybindingController keybindingController = null;
/** Creates new form GUI */
public GUI() {
initComponents();
createObjects();
communicator.searchForPorts();
keybindingController.toggleControls();
keybindingController.bindKeys();
}
private void createObjects()
{
**communicator = new Communicator(this);**
keybindingController = new KeybindingController(this);
}
...
..}
我感到困惑這是如何用於創建通信器類的對象,如在上面的代碼中突出顯示(出現通信=新通訊(本);)
另一個混亂是: Communicator.java
public class Communicator implements SerialPortEventListener
{
...
...
public void connect()
{
String selectedPort = (String)window.cboxPorts.getSelectedItem();
selectedPortIdentifier = (CommPortIdentifier)portMap.get(selectedPort);
CommPort commPort = null;
try
{
//the method below returns an object of type CommPort
commPort = selectedPortIdentifier.open("TigerControlPanel", TIMEOUT);
//the CommPort object can be casted to a SerialPort object
serialPort = (SerialPort)commPort;
....
...}
public void initListener()
{
try
{
**serialPort.addEventListener(this);**
serialPort.notifyOnDataAvailable(true);
}
catch (TooManyListenersException e)
{
logText = "Too many listeners. (" + e.toString() + ")";
window.txtLog.setForeground(Color.red);
window.txtLog.append(logText + "\n");
}
}
....
}
再次我感到困惑與使用的 '這個' 此處(serialPort.addEventListener(本);)
我與代碼 http://rxtx.qbang.org/wiki/index.php/Event_based_two_way_Communication
比較那裏它建議
...
InputStream in = serialPort.getInputStream();
**serialPort.addEventListener(new SerialReader(in));**
...
public static class SerialReader implements SerialPortEventListener
{
private InputStream in;
private byte[] buffer = new byte[1024];
public SerialReader (InputStream in)
{
this.in = in;
}
public void serialEvent(**SerialPortEvent arg0**) {
int data;
try
{
int len = 0;
while ((data = in.read()) > -1)
{
if (data == '\n') {
break;
}
buffer[len++] = (byte) data;
}
System.out.print(new String(buffer,0,len));
}
catch (IOException e)
{
e.printStackTrace();
System.exit(-1);
}
}
}
的描述爲的addEventListener http://docs.oracle.com/cd/E17802_01/products/products/javacomm/reference/api/
的addEventListener
公共抽象無效的addEventListener(SerialPortEventListener LSNR) 拋出java.util.TooManyListenersException 註冊一個SerialPortEventListener對象來偵聽SerialEvents。對特定事件的興趣可以使用notifyOnXXX調用表示。 SerialPortEventListener的serialEvent方法將用一個描述事件的SerialEvent對象來調用。
我想知道它是如何將'SerialPortEventListener lsnr'作爲參數傳遞給上述代碼中的addEventListener的使用。
由於
我明白了。你能解釋第二次使用serialPort.addEventListener(this); – 2013-02-11 16:25:17
@GauravK。新增說明。 – 2013-02-11 16:27:29
明白了。謝了哥們。 serialPort.addEventListener(new SerialReader(in));在Communicator類中,但它不實現SerialPortEventListener。我猜datz爲什麼這樣表達。 – 2013-02-11 16:31:44