I have started JAVA and doing serial communication using RxTx.
Referring to:
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/
In the 2nd link I am not able to decipher usage of ‘this’:
Can anyone please explain:
Communicator.java
public class Communicator implements SerialPortEventListener
{
GUI window = null;
..
..
public Communicator(GUI window)
{
this.window = window;
}
...
..
}
In 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);
}
...
..}
I am confused how this is used to create an object of Communicator class, as highlighted in above code(appearing communicator = new Communicator(this);)
Another confusion is:
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");
}
}
....
}
Again I am confused with the use of ‘this’ here (serialPort.addEventListener(this);)
I compared with the code at
http://rxtx.qbang.org/wiki/index.php/Event_based_two_way_Communication
there it suggests
...
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);
}
}
}
The description for addEventListener
http://docs.oracle.com/cd/E17802_01/products/products/javacomm/reference/api/
addEventListener
public abstract void addEventListener(SerialPortEventListener lsnr)
throws java.util.TooManyListenersException
Registers a SerialPortEventListener object to listen for SerialEvents. Interest in specific events may be expressed using the notifyOnXXX calls. The serialEvent method of SerialPortEventListener will be called with a SerialEvent object describing the event.
I want to know usage of this as how it is passing ‘SerialPortEventListener lsnr’ as the parameter to addEventListener in above code.
Thanks
thiskeyword is a reference to the current instance for which the code is being executed. So, sincethisis a reference, you can use it as any other reference. No problem in that.Now let’s take a look at your usage: –
Since this statement is used inside the method of
GUIclass, so,thisrefers to theinstance of GUI, currently executing the code. Now, by passing it to the constructor, you are simply passing the reference of current instance to it. And it’s quite valid, sinceCommunicatorconstructor takes a reference of typeGUI: –Now let’s move ahead with the next statement:
Here, you are registering the
serialPortwith anEventListenerwhich is referenced bythis. Since, this is used inside the class –Communicator, which implementsSerialPortEventListener, so basically you are registering to aCommunicatorinstance, which is nothing but aSerialPortEventListener. So, you are registering to that event.As far as your other code is concerned:
Here, you have just used a new
instanceinstead ofthis, since you are not insideSerialReaderclass. So, you don’t havethisreference to anySerialReaderinstance, and hence you need to create an object manually of that class.So, there is no difference. Because, in any case, you are registering the class that implements
SerialPortEventListeneronly.