5

I've written a Java program to read my Arduino's outputs through Serial port (using RXTX Library). There's a small issue I'm facing.

Arduino continuously writes to serial. But every time, after running the Java program I have to reset the Arduino to make the program see the serial port values. Otherwise it won't read any value.

Hope it's clear. I need to know whether there's a way to fix this. How can I make my program to read serial port values when it is started without doing anything to external devices.

Thanks!

EDIT This is the method which'll initialize the serial port.

SerialPort serialPort;
private static final String PORT = "COM32";
private InputStream input;
private static final int TIME_OUT = 2000;
private static final int DATA_RATE = 9600;

public void initialize() {
        CommPortIdentifier portId = null;
        Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
        while (portEnum.hasMoreElements()) {
            CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
            if (currPortId.getName().equals(PORT)) {
                portId = currPortId;
                break;
            }
        }
        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);
            input = serialPort.getInputStream();
            serialPort.addEventListener(this);
            serialPort.notifyOnDataAvailable(true);
        } catch (Exception e) {
            System.err.println(e.toString());
        }
    }
Anubis
  • 1,490
  • 3
  • 20
  • 32
  • I don't know the specific arduino serial port implementation, but... have you checked flow control settings? – Axeman Aug 22 '12 at 08:07
  • The Arduino serial settings are: No flow control, 8 data bits, no parity, and 1 stop bit. Check to make sure your java settings match. – helloworld922 Aug 22 '12 at 08:19
  • If arduino has no flow control at all, then IMHO the flow control can't be the thing that stops data flow... – Axeman Aug 22 '12 at 08:27
  • @Axeman how can I check the settings. I'll edit the post and include the port initialization method..Thanks! – Anubis Aug 22 '12 at 08:29
  • Could it be related to this question on stackoverflow: http://stackoverflow.com/questions/10382578/flow-controll-settings-for-serial-communication-between-java-rxtx-and-arduino – Axeman Aug 22 '12 at 08:29
  • Sorry. I meant THIS question http://stackoverflow.com/questions/1391402/problem-receving-in-rxtx . Also, I see that you're using windows. Have you tried to see if using a normal serial terminal (teraterm or hyperterminal) the data transfer works as you expect, and to see with portmon (by Sysinternals) the differences between behaviour of terminal versus your java code? – Axeman Aug 22 '12 at 08:42
  • Take a look at a language called [Processing](http://processing.org/) Its loosely based on Java (you can write valid java code in processing and it will compile), and follows the same IDE pattern as the Ardunio IDE and they offer a easy [library](http://www.arduino.cc/playground/interfacing/processing) to interface between them. – Dean Aug 22 '12 at 09:35
  • @Axeman I've checked using teraterm. Arduino behave strange. Teraterm will catch the values **only** if it is started after the arduino is properly established. On the other hand, from arduino's Tx leds, I can see that arduino automatically starts Tx when terminal is started. (Before that Tx LED won't blink)..What is this?? – Anubis Aug 22 '12 at 10:27
  • @Anubis It seems that it could be a flow control issue, but I really don't undestrand. But it reminds me something I faced weeks ago: from your code I can assume that you're using an USB-Serial adapter. With some Prolific chipset, even using *NO* flow control and a simple cable (RX-TX-GND) I had to enable DTR and RTS signals on the PC software to have communication works... – Axeman Aug 22 '12 at 10:44
  • @Axeman you are partly correct, I'm not using an serial adapter. [Arduino](http://arduino.cc/en/Main/ArduinoBoardMega) communicate with PC through usb. But it writes data serial, (no any serial adaptor). I just have to treat it as serial. Please note that when the communication began, it works perfectly. The issue is related with the start of communication. It's not plug and play. After everything is set up properly, I have to reset the Arduino so that then only Java (or any other serial port reader) can read the values.. – Anubis Aug 22 '12 at 11:19
  • @Anubis FYI the IC marked FTDI that you can see on the board top left (from the page you linked) **IS** an usb-serial adapter :-) . I'll try to raise DTR and RTS in the PC software (serialport.setDTR(true) and serialport.setRTS(true), and see what happens with portmon – Axeman Aug 22 '12 at 12:12
  • @Anubis - I'm guessing it's to do with the FTDI/USB as well. I'd maybe try closing the port first and reopening it in your initialise routine (do a check to see if open first) In your shutdown routine make sure the port is closed. Also, you could check the FTDI docs to see how to deal with this, other people must have run into it before. – Oli Glaser Aug 22 '12 at 15:57
  • @Axeman Great!!! That was the issue! thanks to [this post](http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1249853051) and, more than that, thanks to you.. I could fix it..!! just had to reset DTS accordingly...many thanks to everyone helped!!! – Anubis Aug 23 '12 at 03:47
  • @Axeman please put your solution as an answer. So that I'll be able to accept it and it'll help others who may come across the same issue.. – Anubis Aug 23 '12 at 03:50

1 Answers1

3

The serial flow control signals (DTR and/or RTS) must be set accordingly to the Arduino specification and to the specification of USB-SERIAL adapter you're using.

Looking briefly at the schematic of your board, I can see that the RTS signal is disconnected, but DTR signal from FTDI chip is coupled with the ATMEGA's RESET pin (I imagine that this is done to allow Arduino uploader to reset the micro and upload new firmware to it).

This means that you have to set DTR signal (in your Java code) to avoid unwanted resets.

Axeman
  • 3,498
  • 20
  • 27