I am new to this serial communication between Arduino and Java NetBeans. I am doing some small project and I need to get data from external EEPROM 24LC256 which is connected to Arduino UNO and pass it to the Java NetBeans platform not the Eclipse. So far I am using RXTX driver to communicate with Arduino in Java and easily get the data from serial monitor of Arduino IDE.

So can anyone help me, on how can I serially communicate with this external EEPROM using Java?

import gnu.io.*;
import java.util.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import java.util.Enumeration; 

public class last extends javax.swing.JFrame implements SerialPortEventListener  {
    SerialPort serialPort;
    private static final String PORT_NAMES[] = { 
        "COM3", // Windows
    private BufferedReader input;  
    private OutputStream output;   /** The output stream to the port */
    private static final int TIME_OUT = 2000; /** Milliseconds to block while waiting for port open */
    private static final int DATA_RATE = 9600;  /** Default bits per second for COM port. */
    public void initialize() {
        CommPortIdentifier portId = null;
        Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();

        while (portEnum.hasMoreElements()) {   //First, Find an instance of serial port as set in PORT_NAMES.
            CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
            for (String portName : PORT_NAMES) {
                if (currPortId.getName().equals(portName)) {
                    portId = currPortId;
        if (portId == null) {
            System.out.println("Could not find COM port.");
        try {          // open serial port, and use class name for the appName.
            serialPort = (SerialPort) portId.open(this.getClass().getName(), TIME_OUT);
            serialPort.setSerialPortParams(DATA_RATE,  // set port parameters

            input = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));   // open the streams
            output = serialPort.getOutputStream();
            serialPort.addEventListener(this);   // add event listeners
        } catch (Exception e) {

     * This should be called when you stop using the port.
     * This will prevent port locking on platforms like Linux.
    public synchronized void close() {                      
        if (serialPort != null) {

     * Handle an event on the serial port. Read the data and print it.
    public synchronized void serialEvent(SerialPortEvent oEvent) {
        if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
            try {
                String inputLine=input.readLine();
                System.out.println("the state of the led is  "+inputLine);
            } catch (Exception e) {
        // Ignore all the other eventTypes, but you should consider the other ones.

    public static void main(String[] args) throws Exception {
        last main = new last();
        Thread t = new Thread() {
            public void run() { //the following line will keep this app alive for 1000 seconds,
                //waiting for events to occur and responding to them (printing incoming messages to console).
                try {Thread.sleep(1000000);} catch (InterruptedException ie) {}

This is the code in Java NetBeans.

  • Perhaps you can attach your code for higher rate of getting a proper answer. Just a wild guess, have you try softserial? – dhimaspw Jan 12 '17 at 12:29
  • here i wrote code – Stenzin lobsang Jan 12 '17 at 12:46

Your EEPROM has I2C interface, so first you shall program Arduino to access it: An I2C EEPROM Class for Arduino

Then implement some kind of communication protocol over serial, to send commands instructing Arduino to read/write from EEPROM. For example, with following commands:

  • for writing write:<address>,<count>,<data_as_hex_string>
  • for reading read:<address>,<count>
  • response for reading: <address>,<count>,<data_as_hex_string>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.