我已经启动了 JAVA 并使用 RxTx 进行串行通信。
参考: 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/
在第二个链接中,我无法破译“this”的用法:谁能解释一下:
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 类的对象,如上面的代码中突出显示的那样(出现communicator = new Communicator(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");
}
}
....
}
我再次对这里使用“this”感到困惑(serialPort.addEventListener(this);)
我与 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/
添加事件监听器
public abstract void addEventListener(SerialPortEventListener lsnr) throws java.util.TooManyListenersException 注册一个 SerialPortEventListener 对象以侦听 SerialEvents。可以使用 notifyOnXXX 调用来表达对特定事件的兴趣。将使用描述事件的 SerialEvent 对象调用 SerialPortEventListener 的 serialEvent 方法。
我想知道它的用法,因为它是如何将“SerialPortEventListener lsnr”作为参数传递给上述代码中的 addEventListener 的。
谢谢