title: JAVA - Serial Data receiving illegal character
body: <p>I am trying to read a serial data with 4 characters sent from Arduino to the Raspberry Pi using the following codes.</p>
  
  <pre><code>int serialPort = Serial.serialOpen("/dev/ttyACM0",9600);
  if(serialPort==-1){
      serialPort = Serial.serialOpen("/dev/ttyACM1",9600);
  }    
  char[] charArray = {' ', ' ', ' ', ' '}; //Initializing char array     
  Serial.serialPuts(serialPort, 8 + "x"); //Sending Request Code to Arduino
  int received = 0; 
  while(received == 0){ //Wait for input
      for(int i = 0; i &lt; 4; i++){
          charArray[i] = (char)Serial.serialGetchar(serialPort);
          received = 1;
      }
  }
  String tempString = new String(charArray);
  System.out.println(tempString);
  </code></pre>
  
  <p>However, upon reading the "tempString", it shows this ���� instead of the data sent by the Arduino.</p>
  
  <p>The Arduino is sending the proper data so I don't think the problem is in Arduino. This code works with 1 character data. What do you think is wrong with these codes?</p>
  
  <p>P.S. The codes work if executed in a loop, though the first execution outputs illegal characters</p>