Send String from Java to Arduino (simple example)
Solution 1
It is solved. I put a Thread.sleep(4000)
after opening the port in the java code and now it works. The problem was that the Arduino is reset everytime the port is opened, so I was sending the data when the Arduino wasn't ready to listen.
char inputBuffer[10];
void setup()
{
Serial.begin(9600);
}
void loop()
{
while (true)
{
if (Serial.available() > 0)
{
Serial.readBytes(inputBuffer, 10);
delay(5000);
Serial.print("I got this ->");
Serial.print(inputBuffer);
Serial.println("<-");
}
}
}
Solution 2
You could use the Java-Arduino Communication Library. It can be found here: https://sourceforge.net/projects/javaarduinolibrary/. (be sure to download both jars) Then your code would look something like this:
import arduino.*;
class JavaArduinoComm {
public static void main(String[] args) {
Arduino obj = new Arduino('PortDescription', BAUD_RATE);
obj.openConnection();
}
}
Then you can use any of the following methods:
- String serialRead(int limit) - returns a string containing as many readings as the value of limit. recommended for reading
- void serialWrite(String s) - writes the contents of the entire string to the serial at once. written as string to serial.
- void serialWrite(String s, int noOfChars, int delay) - writes the contents of the strings to the serial gradually. It writes the string in incremental steps with 'noOfChars' charaacters each time, with a pause of 'delay' milliseconds between each write. written as string to serial. recommended to write String
- void serialWrite(char c) - writes the individual char to the serial in datatype char.
- void serialWrite(char c, int delay) - writes the individual char to the serial in datatype char and pauses the thread for delay milliseconds after. recommended to write char
takluiper
Updated on July 18, 2022Comments
-
takluiper almost 2 years
It is solved. I put a
Thread.sleep(4000);
after opening the port in the java code and now it works. The problem was that the arduino is reset everytime the port is opened. When i was sending the data, the arduino wasn't ready to listen.I'm new to arduino and Java, but i made a program so simple that I don't get why isn't working.
I send a String to the serial port that correspond to arduino (COM5):
import java.io.*; import java.util.*; import gnu.io.*; public class SimpleWrite { static Enumeration portList; static CommPortIdentifier portId; static String messageString = "color FF00FFEND"; static SerialPort serialPort; static OutputStream outputStream; public static void main(String[] args) { portList = CommPortIdentifier.getPortIdentifiers(); while (portList.hasMoreElements()) { portId = (CommPortIdentifier) portList.nextElement(); if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) { if (portId.getName().equals("COM5")) { try { serialPort = (SerialPort) portId.open("SimpleWriteApp", 2000); outputStream = serialPort.getOutputStream(); serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); outputStream.write(messageString.getBytes()); System.out.println(messageString); outputStream.close(); serialPort.close(); } catch (IOException e) {System.out.println("err3");} catch (PortInUseException e) {System.out.println("err");} catch (IOException e) {System.out.println("err1");} catch (UnsupportedCommOperationException e) {System.out.println("err2");} } } } } }
and the code in arduino to get that string:
char inputBuffer[10]; void setup() { Serial.begin(9600); } void loop() { while (true) { if (Serial.available() > 0) { Serial.readBytes(inputBuffer, Serial.available()); delay(5000); Serial.print("I got this ->"); Serial.print(inputBuffer); Serial.println("<-"); } } }
the while(true) is for testing purposes. I dont get nothing printed, and I dont know where the problem is. I have seen all the post about arduino and java here and i dont find nothing that helps. Thanks for the help and sorry if it is a stupid question, im a newbie to this
Im using RXTXcomm.jar. Version: RXTX-2.2-20081207
-
Georgi Peev almost 3 yearsFor ~1500ms are enough. Another way is to send a message from the Arduino in the setup function and have a polling for the data from the Java side waiting for that message and when received to unblock your sending of the data from the Java side.