Sending data from Java program to Arduino Uno

I'm currently building a project that requires communication between an Arduino Uno and a Java program. The Java program consists of multiple files, one of which is responsible for sending data that can be used by other Java client programs. I want to add my Arduino Uno program to this system and have it be able to read data that would be sent from one of the Java files. My goal is to send a single decimal value from the Java program to the Arduino, but I'm not too sure how to go about doing this, given my experience with Arduinos is minimal. The idea is that the user can press a key on the computer to trigger some data formation handled by the Java program, that would then be sent to an Arduino Uno that is connected to the same computer. My goal is to have the data sent as quick as possible so that it can simulate reading data in a "real-time".

Thank you in advance!

Java <— serial port —> Arduino

I suspect you need to establish the PC to Arduino communication before the PC user tries to send data.


Error si in java code, line 42. Definitly.

Have a look at the examples in Serial Input Basics - simple reliable non-blocking ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

The technique in the 3rd example will be the most reliable. It is what I use for Arduino to Arduino and Arduino to PC communication.

You can send data in a compatible format with code like this (or the equivalent in any other programming language)

Serial.print('<'); // start marker
Serial.print(','); // comma separator
Serial.println('>'); // end marker

This Simple Python - Arduino demo should give you the general idea about the PC-Arduino communication. You should be able to replace the Python code with equivalent functionality in any other programming language.

The Python code should work on Windows if you edit it to use the Windows style of COM ports.