In the following you will learn how to setup and write your first RedComm application in order to communicate with the serial port.
Its assumed that you installed redcomm as stated here: installation (windows)
Now startup Netbeans and create a new "Java application". Uncheck the option "create main class" and click finish.
First you need to add references to RxTx.jar and RedComm.jar. To do this right click on the "Libraries" node in your project structure and choose "Add JAR/Folder". Select "RxTx.jar" and click open. Do the same for "RedComm.jar".
After setting up your application it's time to create some GUI.
First of all create a new source code package. Right click "Source Packages" and choose "New->Java Package". Name it "redcomm_example" and click "finish".
Now right click the new package and choose "New->Java Class". Name it "TextArea" and click "finish".
TextArea will be a simple TextAre which will display the received data from the com port.
Open up "TextArea.java" and put in this code:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
package redcomm_example;
import altay.redcomm.SerialEvent;
import altay.redcomm.SerialPortReaderListener;
import javax.swing.JTextArea;
/**
*
@author Robin Altay
*/
public class TextArea extends JTextArea implements SerialPortReaderListener {
@Override
public void handleEvent(SerialEvent e) {
setText(new String(e.getData()));
}
}
*TextArea* extends *JTextArea*, because as stated above you want to implement an own TextArea, without the need to write the whole text managing stuff, swing behaviour etc. on your own. So your *TextArea* will basically behave like an normal *JTextArea*.
Next it is implementing *SerialPortReaderListener*. This will give you the possibility to handle incoming serial port events. In your *handleEvent(Serial event e)* Method you simply display the incoming data ("e.getData()") in the *TextArea*.
Thats all for handling incoming data. As you see its very easy to implement data handling and you get a nice encapsulated Component, which manages itself.
Now right click your project and select "Clean and build".
After that right click the "redcomm_example" package and choose "New->JFrame Form". Click "finish".
Insert some GUI elements as shown below:

Open up the code of your JFrame. First you need to declare an attribute of type "SerialPort" and an boolean "connected".
So put the following code in there:
import altay.redcomm.SerialPort;
/**
*
@author Robin Altay
*/
public class NewJFrame extends javax.swing.JFrame {
private SerialPort serialPort;
private boolean connected = false;
/* Creates new form NewJFrame /
public NewJFrame() {
initComponents();
serialPort = new SerialPort();
}
Double click your "Connect" Button and paste this code:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
private void tglButtonConnectActionPerformed(java.awt.event.ActionEvent evt) {
if (tglButtonConnect.isSelected()) {
try {
/*
* Connect to serial port with parameters from the specified
* configuration file ("port.properties").
*/
serialPort.connect("port.properties");
lblConnectionState.setText("Connected"); // Set label text.
lblConnectionState.setForeground(Color.GREEN); // Set label color.
/*
* Add TextArea txtRx to the list of observers.
* Everytime a new serial event will be generated txtRx will be
* notified about that event and is able to handle the data e.g.
* display it.
*/
serialPort.notifyOnEvent(txtRx);
connected = true; // Now we are connected to the com port.
} catch (NoSuchPortException ex) {
Logger.getLogger(SimpleCom.class.getName()).log(Level.SEVERE, null, ex);
} catch (PortInUseException ex) {
Logger.getLogger(SimpleCom.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnsupportedCommOperationException ex) {
Logger.getLogger(SimpleCom.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(SimpleCom.class.getName()).log(Level.SEVERE, null, ex);
} catch (TooManyListenersException ex) {
Logger.getLogger(SimpleCom.class.getName()).log(Level.SEVERE, null, ex);
}
} else {
serialPort.disconnect(); // Disconnect from com port
lblConnectionState.setText("Disconnected"); // Set label text
lblConnectionState.setForeground(Color.RED); // Set label color
connected = false; // Now we're disconnected
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Now double click the "Send" button and paste this:
private void btnSendActionPerformed(java.awt.event.ActionEvent evt) {
if (connected) {
serialPort.write(txtTx.getText()); // Write data onto the serial port
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Thats it! You've now written your first application, which communicates with the serial port.
You implemented your own SerialPortReaderListener which is notified when data is received. You we're also capable of writing the code that is needed to connect with the port through an configuration file.