2

I have an XBee-PRO S1 on Arduino Uno R3 which acts like a transmitter to send data to another XBee which is acting like a receiver to turn on the LEDs connected to it. Here is what I'm doing:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import gnu.io.CommPortIdentifier;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import gnu.io.UnsupportedCommOperationException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.TooManyListenersException;

public class NewClass implements SerialPortEventListener {

    SerialPort serialPort;
    OutputStream out;

    private static final String PORT_NAME = "COM10"; //(XBee Transmitter)
    private BufferedReader input;
    private static final int TIME_OUT = 2000;
    private static final int DATA_RATE = 9600;
    // End of input chars
    //private static final byte EOIC = 3;

    public void initialize() {
        CommPortIdentifier portId = null;
        Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();

        //First, find an instance of serial port as set in PORT_NAME.
        while (portEnum.hasMoreElements()) {
            CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
            if (currPortId.getName().equals(PORT_NAME)) {
                portId = currPortId;
            }
        }
        if (portId == null) {
            System.out.println("Could not find COM port.");
            return;
        }

        try {
            serialPort = (SerialPort) portId.open(this.getClass().getName(), TIME_OUT);
            serialPort.setSerialPortParams(DATA_RATE, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);

            // Open the input stream
            input = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));

            serialPort.addEventListener(this);
            serialPort.notifyOnDataAvailable(true);
            serialPort.setEndOfInputChar((byte) 3);
        }
        catch (PortInUseException | UnsupportedCommOperationException | IOException | TooManyListenersException e) {
            System.err.println(e.toString());
        }
    }

    public synchronized void close() {
        if (serialPort != null) {
            serialPort.removeEventListener();
            serialPort.close();
        }
    }

    @Override
    public synchronized void serialEvent(SerialPortEvent oEvent) {
        if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
            try {
                char inputLine;
                //dat = new ArrayList<Character>();
                if (input.ready()) {
                    inputLine = (char) input.read();
                    Thread.sleep(1500);
                    sendChar();
                }
            }
            catch (Exception e) {
                System.err.println(e.toString());
                System.out.println("Error reading");
            }
        }
        // Ignore all the other eventTypes, but you should consider the other ones.
    }

    public synchronized void sendChar() {

        try {
            Thread.sleep(1500);
            out = serialPort.getOutputStream();
                System.out.println("out is not null");
                for (int i = 0; i <= 900; i++) {
                    Thread.sleep(1500);
                    out.write((char) 'A');
                    out.flush();
                    System.out.println(i+") written-> A"); //For debugging
                }
        }
        catch (InterruptedException | IOException e) {

        }
    }


    public synchronized void cRead(char data) {
        if (data != ' ') {
            System.out.print(data);
            //getTimestamp();
        }
    }

    public static void main(String[] args) throws Exception {
        NewClass main = new NewClass();
        main.initialize();
        Thread t = new Thread() {
            @Override
            public void run() {
                //The following line will keep this application alive for 12 hours,
                //waiting for events to occur and responding to them (printing
                //incoming messages to console).
                try {
                    Thread.sleep(43200000);
                }
                catch (InterruptedException ie) {

                }
            }
        };
        t.start();
        System.out.println("Started");
    }
}

One problem with this code is that, when I get some incoming signal at the COM port then only the sendChar() is called which is due to the condition if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE). Also the data is not being sent. I don't know what other event types are there because of lack of proper documentation.

What I want is to send data to Arduino without receiving anything. What am I doing wrong or missing?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
jayantS
  • 817
  • 13
  • 29
  • 3
    You should break up the problem into smaller, manageable pieces. (1) Connect the Arduino's serial port to a PC running a terminal emulator program to debug your code. Once that is working, then (2) connect the XBee to the PC and make sure that it is configured and can do RF transfers. See this [answer](http://stackoverflow.com/questions/18478276/xbee-linux-serial-port-on-rasberry-pi/18483246#18483246) and make sure you are not stuck in command mode. *"acts like a transmitter...acting like a receiver"* -- The XBee modules are radio transceivers that do transmit and receive. – sawdust Aug 29 '13 at 21:24
  • @sawdust: You are right. The XBee was in the command mode that is why it was not transmitting the data received at the Arduino serial. Problem Solved! – jayantS Aug 31 '13 at 13:09

0 Answers0