Flow Controll Settings for Serial Communication between Java RXTX and Arduino

For Adafruit customers who seek help with microcontrollers

Moderators: adafruit_support_bill, adafruit

Flow Controll Settings for Serial Communication between Java RXTX and Arduino

Postby Tilman » Wed May 02, 2012 3:32 pm

I have a simple sketch on my Arduino which just displays the serial input on a LCD Display. Using lynx term and the arduino serial monitor works fine: sent input is being displayed. The trouble starts when I want to start serial communication between my Java programm, running in Eclipse on a Win7 x64 machine, and the Seeeduino. I'm using the RXTX x64 build. The programm is intended to send and receive some string.getBytes() via the open port. Receiving on the Java side works, but receiving on the Arduino side fails.

It seems that the problem is the proper Flow Control setting. I saw that some people had the same issue like here http://stackoverflow.com/questions/1391 ... ng-in-rxtx

But this solution does not work for me. If I set the FlowControl to None, than I get only a block icon on the display, indicating, that the serial connection has been established, but nothing else. If I set the FlowControl to RCTS_IN | RCTS_OUT, then I get the string bytes only on the display when I close the established connection.

Why is the data only send when I close the connection (Flushing the out stream did not help as well) ? What am I doing wrong with the Flow Controll settings?

This is the modified connect() method I'm using.

Code: Select all
void connect(String portName) throws Exception {
        CommPortIdentifier portIdentifier = CommPortIdentifier
                .getPortIdentifier(portName);
        if (portIdentifier.isCurrentlyOwned()) {
            System.out.println("Error: Port is currently in use");
        } else {
            CommPort commPort = portIdentifier.open(this.getClass().getName(),
                    2000);

            if (commPort instanceof SerialPort) {
                SerialPort serialPort = (SerialPort) commPort;
                serialPort.setSerialPortParams(115200, SerialPort.DATABITS_8,
                        SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);

                try {
                  serialPort.setFlowControlMode(
                  //      SerialPort.FLOWCONTROL_NONE);
                  // OR
                  // If CTS/RTS is needed
                  //serialPort.setFlowControlMode(
                        SerialPort.FLOWCONTROL_RTSCTS_IN |
                        SerialPort.FLOWCONTROL_RTSCTS_OUT);
                } catch (UnsupportedCommOperationException ex) {
                  System.err.println(ex.getMessage());
                }

                serialPort.setRTS(true);

                in = serialPort.getInputStream();
                out = serialPort.getOutputStream();

                (new Thread(new SerialWriter(out))).start();

                serialPort.addEventListener(new SerialReader(in, this));
                serialPort.notifyOnDataAvailable(true);

            } else {
                System.out.println("Error: Only serial ports are to use!");
            }
        }
    }


Thanks in advance for your time
Tilman
 
Posts: 2
Joined: Wed May 02, 2012 3:28 pm

Re: Flow Controll Settings for Serial Communication between Java RXTX and Arduino

Postby uhe » Sun May 13, 2012 4:49 am

Hi,

so you have a probelm with receiving on the Arduino side or sending on the Java side.
Since you only show how you connect() how do you send stuff in Java?

I've never needed .setFlowControlMode(...) reading and writing just worked without that like:
Code: Select all
public boolean openPort(String name) {
    :
    _oStream = _curPort.getOutputStream();
    :
}

public boolean sendMsg(int[] msg){
    :
    for(int i=0; i < _maxSerialData; i++) {_oStream.write((byte)msg[i]);}
    :
}

There's no extra thread in there, just one for the GUI and one for the whole serial stuff.
"I have not failed. I've just found 10,000 ways that won't work." - Thomas Edison
uhe
 
Posts: 178
Joined: Mon Sep 03, 2007 3:50 pm
Location: metric world

Re: Flow Controll Settings for Serial Communication between Java RXTX and Arduino

Postby Tilman » Sun May 13, 2012 4:55 am

I double checked that with an Arduino Mini Pro, which worked flawlessly. So the problem was on the Arduino side.

To be exact, I'm using a Seeeduino Mega v1.22 which has a switch for manual or auto reset. If set to manual, you cannot upload anything to the microcontroller. If set to auto, the RXTX lib is not able to send.

Case closed =)
Tilman
 
Posts: 2
Joined: Wed May 02, 2012 3:28 pm

Re: Flow Controll Settings for Serial Communication between Java RXTX and Arduino

Postby adafruit_support_bill » Sun May 13, 2012 5:24 am

There is no flow control on the Arduino. Rx, Tx and Gnd are the only signals used for communication. DTR is used to reset the board. I don't know how Seeed implements the reset. Maybe that is what is confusing Java.
User avatar
adafruit_support_bill
 
Posts: 16043
Joined: Sat Feb 07, 2009 9:11 am


Return to Microcontrollers

Who is online

Users browsing this forum: No registered users and 2 guests

Stuff to buy from the Adafruit store and links to product documentation!


New Products [103]

Raspberry Pi[80]
 
FLORA[23]
 
Bunnie Studios[9]
 
FPGA[1]
 
mbed[11]
Arduino[60]
 
NETduino[14]
 
BeagleBone[24]
 
Android[6]
 
XBee[10]
More Dev Boards[30]


 
BoArduino[8]
 
SpokePOV[4]
 
TV-B-Gone[4]
 
MiniPOV[3]
 
SIM reader[3]
 
Microtouch[5]
 
Clocks & Watches[18]
 
Drawdio[4]
 
Brain Machine[1]
 
Game of Life[2]
 
MintyBoost[2]
More DIY Kits[16]


 
MaKey MaKey[3]
 
Tweet-a-Watt[5]
 
Young Engineers[33]
 
Discover Electronics[2]
 
Snap Circuits[4]
 
littleBits[3]
 
Project packs[8]


 
Breakout Boards[33]
LCDs & Displays[48]
Components & Parts[69]
Batteries & Power[49]
EL Wire/Tape/Panel[52]
LEDs[109]
 
Wireless[14]
Cables[61]
 
Lasers[6]
Sensors/Parts[145]
 
Enclosures/Cases[11]
 
Solar[11]
 
RFID / NFC[13]
Prototyping[70]
 
iDevices[13]
Tools[71]
 
Wearables[39]
 
CNC[37]
 
Robotics[29]
 
3D printing[1]
 
Materials[24]


 
Stickers[41]
 
Skill badges[55]
 
Books[25]
 
Circuit Playground[7]
 
Gift Certificates[4]