Pages: [1]   Go Down
Author Topic: Can't send integer to Arduno from Java  (Read 1300 times)
0 Members and 1 Guest are viewing this topic.
Poland - Wroclaw
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, my new problem  ;D

I want to send integers from Java application to Arduino, the problem is that sketch is perfectly working with Serial monitor from Arduino compiler, but don't want to do it with my Java soft, maybr you know, where is the problem:

Code:
package arduino;

import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import java.io.InputStream;
import java.io.OutputStream;
import processing.app.Preferences;

public class Main {
    static InputStream input;
    static OutputStream output;

    public static void main(String[] args) throws Exception{
        Preferences.init();
        System.out.println("Using port: " + Preferences.get("serial.port"));
        CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier(
                Preferences.get("serial.port"));

        SerialPort port = (SerialPort)portId.open("serial talk", 4000);
        input = port.getInputStream();
        output = port.getOutputStream();
        port.setSerialPortParams(Preferences.getInteger("serial.debug_rate"),
                SerialPort.DATABITS_8,
                SerialPort.STOPBITS_1,
                SerialPort.PARITY_NONE);


            boolean k=true;
            while(true)
            {
                if(k)
                {
               output.write(1200);
               output.flush();
               k=false;
                }

                
                while (input.available()!=0)
                {
            System.out.println((int)(input.read()));
                }

            }
    }
        
}



And Arduino sketch:

Code:

void setup()
{
  Serial.begin(9600);
}

void loop ()
{
  char serInString[20]="";
  int i=0;
  
    while (Serial.available())
  {
    serInString[i] = Serial.read();
    i++;
  }
  
  Serial.print(serInString);
  Serial.flush();
  delay(100);  // wait a bit, for serial data
}

Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 46
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

Firstly, you should be aware that java's idea of an int is a 4 byte value, but an arduino int is 2 bytes. In java you could use a short which is a 2 byte value.

Secondly, using the RXTX library you only send bytes over serial (http://java.sun.com/j2se/1.5.0/docs/api/java/io/OutputStream.html#write(int)), if you want to send a int then you're going to have to split it into two bytes then reassemble that on the arduino.

So in java you would do something like:

Code:
short x = 1200;
byte upper = x >> 8; //Get the upper 8 bits
byte lower = x & 0xFF; //Get the lower 8bits
output.write(upper);
output.write(lower);

Then on the arduino you'd have something like:

Code:
byte upper = Serial.read();
byte lower = Serial.read();
int x = (upper<<8) | lower; //Reassemble the number
Serial.print(x, DEC);
Logged

Poland - Wroclaw
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Is working, thanks very much, maybe you know where I can find some materials about synchronization? Sending a lot of integers can cause some mistakes...
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 46
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Do you mean synchronization in Java between multiple threads, or between the computer and the Arduino?

If it's the first then the Java Tutorial on concurrency is a good information source.

If it's the latter, then I'm assuming you're getting mistakes because the computer is sending the data faster than the arduino can process it. (This means the serial buffer will be filling up and bytes will be lost, resulting in mismatched upper and lower values). You can get around this by having the arduino send acknowledgements back so the computer knows when it is ready to send more data.

As an example, in a project I've been working on I have a Java class which manages the serial connection, and it counts the number of bytes that have been sent, when 100 bytes have been sent it will not send any more until a certain message is received from the arduino. The arduino then counts the number of bytes it has recieved and processed, and when that number reaches 100, it sends the request for more to the computer.
Logged

Poland - Wroclaw
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, the problem was loosing bytes betweent Arduino and Computer, your method is good, firstly i was thinking about sending reply after each integer, but it would loose to much time. I will try yours method. Thanks!
Logged

Pages: [1]   Go Up
Jump to: