title: Using Java to control Arduino over serial
body: <p>I'm programming a system which uses a Java program to control an Arduino over serial. The Arduino receives a <code>char</code> from the Java program and executes a pre-programmed action based on based on that <code>char</code>. The Arduino works exactly as expected when I used the Arduino Programmer serial monitor to send characters, but when I try to send the same characters from Java the Arduino doesn't respond.</p>
  
  <p>Here is the Arduino code:</p>
  
  <pre><code>void setup() {
    Serial.begin(9600);
  }
  
  void loop() {
    if (Serial.available() == 1) {
      char serialData = Serial.read();   
  
      // Activate servos based on input
      switch (serialData) {
          case '1':
              activateServo();
              break;
  
          case '2':
              readSensor1();
              break;
  
          case '3':
              readSensor2();
              break;
  
          default:
              // Ignore input
              break;
  
      }
    }
  }
  
  
  void activateServo() {
      // Code removed
  }
  
  void readSensor1() {
      // Code removed
  }
  
  void readSensor2() {
      // Code removed
  }
  </code></pre>
  
  <p>Here is the Java code. Note that I'm using the <a href="http://fazecast.github.io/jSerialComm/javadoc" rel="nofollow">jSerialComm</a> API:</p>
  
  <pre><code>import java.io.PrintWriter;
  import java.time.Duration;
  import java.time.Instant;
  
  import com.fazecast.jSerialComm.SerialPort;
  
  public class SerialWriteTest {
      private static char dataToSend = '1';
  
      public static void main(String[] args) {
          // Find all available ports
          SerialPort[] computerPorts = SerialPort.getCommPorts();
  
          // Select COM3
          SerialPort selectedPort = null;
  
          for (SerialPort p : computerPorts) {
              if (p.getSystemPortName().equals("COM3")) {
                  selectedPort = p;
              }
          }
  
          // Try to open port, terminate execution if not possible
          if (selectedPort == null) {
              return;
  
          } else if (selectedPort.openPort()) {
              System.out.println(selectedPort.getSystemPortName() + " successfully opened.");
  
          } else {
              System.out.println(selectedPort.getSystemPortName() + " failed to open.");
              return;
          }
  
          // Set port to scanner mode
          selectedPort.setComPortTimeouts(SerialPort.TIMEOUT_SCANNER, 0, 0);
  
          // Read from port and display
          try (PrintWriter output = new PrintWriter(selectedPort.getOutputStream())) {
              // Wait for 100 ms
              Instant start = Instant.now();
              while (Duration.between(start, Instant.now()).toMillis() &lt; 100) {}
  
              try {
                  output.write('1');
                  output.flush();
                  System.out.println("Sent");
  
              } catch (Exception e) {
                  System.out.println("Failed to send");
              }
          }
  
          selectedPort.closePort();
  
          System.out.println("--- Main finished ---");
      }
  }
  </code></pre>
  
  <p>Since the Arduino seems to be working with the serial monitor, I'm certain that the program is in the Java program. Can anyone see why the Java program doesn't seem to be sending the character '1' like it's supposed to?</p>