Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9272331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:51:01+00:00 2026-06-18T15:51:01+00:00

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/

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T15:51:03+00:00Added an answer on June 18, 2026 at 3:51 pm

    this keyword is a reference to the current instance for which the code is being executed. So, since this is a reference, you can use it as any other reference. No problem in that.

    Now let’s take a look at your usage: –

    new Communicator(this);
    

    Since this statement is used inside the method of GUI class, so, this refers to the instance 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, since Communicator constructor takes a reference of type GUI: –

    public Communicator(GUI window)
    {
        this.window = window;
    }
    

    Now let’s move ahead with the next statement:

    serialPort.addEventListener(this);
    

    Here, you are registering the serialPort with an EventListener which is referenced by this. Since, this is used inside the class – Communicator, which implements SerialPortEventListener, so basically you are registering to a Communicator instance, which is nothing but a SerialPortEventListener. So, you are registering to that event.

    As far as your other code is concerned:

    serialPort.addEventListener(new SerialReader(in));
    

    Here, you have just used a new instance instead of this, since you are not inside SerialReader class. So, you don’t have this reference to any SerialReader instance, 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 SerialPortEventListener only.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been doing some java development lately and have started using Eclipse. For
greetings, today i have started to learn java using netbeans ide. i would like
The last months I have tried to get started with web developing using Java.
Recently, I have started doing UI development in Java; I used to do UI
So I just started doing numeric methods in Java; // I have a matrix
I have been doing Java for a long time and started Scala about 6
I just started doing Java Swing applications using MVC pattern and so far i
I have recently started doing some Java and one of the Maven checkstyle plugin
I have started learning Java and I have 1 issue: I write a simple
I have just started MongoDB + Java, and I am getting into queries, collections

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.