我已经启动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