Ivo Tebexreni
Ivo Tebexreni

Reputation: 164

Arduino - Processing bidirectional communication can't receive data via serial

I'm trying to communicate arduino with java using jssc. Just for testing I'm trying to receive the data on Arduino and sending back to PC, pretty straight forward. The problem is, board seems to not receive it. When a code for arduino to just send, works fine, but it just can't receive any data. I first thought the problem was jssc, so I change my application to Processing, same problem. I even try to change the board, no diference, the board is fine and it can communicate normally with Arduino Serial Monitor. So my problem must be the Java and Processing code. I know its a very simple problem, and i'm kind of ashamed for posting this, must be a very simple problem that past me.

One more thing, no errors at all is shown

Java code:

public class ArduinoTest{
    public static void main(String[] args) throws SerialPortException, InterruptedException {
        SerialPort serialPort = new SerialPort("COM6");
        serialPort.openPort();
        serialPort.setParams(SerialPort.BAUDRATE_9600,
                SerialPort.DATABITS_8,
                SerialPort.STOPBITS_1,
                SerialPort.PARITY_NONE);
        int i;
        serialPort.writeBytes("a".getBytes());
        while(true){
            a=serialPort.readBytes(1);
            i = a[0] & 0xff;
            System.out.println(i);
        }
    }
}

Processing code:

import processing.serial.*;

Serial port;
int r;
void setup(){
  port = new Serial(this, "COM6", 9600);
  port.write(1);
}
void draw(){
  if(port.available()>0){
    r = port.read();
    println(r);
  }
}

Arduino code:

int r1=0;

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

void loop() {
  if(Serial.available() > 0){
    r1 = Serial.read();
    Serial.write(r1);
  }
}

Upvotes: 1

Views: 508

Answers (1)

Codebreaker007
Codebreaker007

Reputation: 2989

Here is working communication code for Arduino:

const int led_pin = 13;    // Initializing the LED pin internal led
String char_output = "";   // Declaring a variable for output


void setup ( ) {
  pinMode(led_pin, OUTPUT); // Declaring the LED pin as output pin
  Serial.begin(9600);       // Starting the serial communication at 9600 baud rate

}

void loop ( ) {


  if (Serial.available ( ) > 0) {   // Checking if the Processing IDE has send a value or not

    char state = Serial.read ( );    // Reading the data received and saving in the state variable

    if (state == '1')  {          // If received data is '1', then turn on LED
      digitalWrite (led_pin, HIGH);
      char_output = "Led on";
    }

    if (state == '0') {     // If received data is '0', then turn off led
      digitalWrite (led_pin, LOW);
      char_output = "Led off";
    }
  }

  delay(50);
  Serial.println (char_output);     // Sending the output to Processing IDE
} 

here is the processing part:

import processing.serial.*;    // Importing the serial library to communicate with the Arduino

Serial myPort;      // Initializing a vairable named 'myPort' for serial communication
float background_color ;   // Variable for changing the background color

void setup ( ) {
  size (500,  500);     // Size of the serial window, you can increase or decrease as you want
  myPort  =  new Serial (this, "COM3",  9600); // Set the com port and the baud rate according to the Arduino IDE
  myPort.bufferUntil ( '\n' );   // Receiving the data from the Arduino IDE

}
void serialEvent  (Serial myPort) {
  background_color  =  float (myPort.readStringUntil ( '\n' ) ) ;  // Changing the background color according to received data
}

void draw ( ) {
  background ( 150, 50, background_color );   // Initial background color, when we will open the serial window
  if ( mousePressed  &&  ( mouseButton  ==  LEFT ) ) { // if the left mouse button is pressed
    myPort.write ( '1' ) ;       // send a '1' to the Arduino IDE
  }
  if  ( mousePressed  &&  ( mouseButton == RIGHT ) ) {  // if the right mouse button is pressed
    myPort.write ( '0' ) ;     // Send a '0' to the Arduino IDE
  }
}

Read the comments an see that reading from serial and printing to serial are apart

Upvotes: 2

Related Questions