Hi ,
i set up a serial communication between my Java environment and Arduino.
I made an easy program that write 1 to the Java console if i push a button on the Arduino and it works very good
and it is also very fast. The problem is on the other way around, i am trying to write a String from Java
to Arduino.
Here is the java code to write bytes:
public void writeSerial(String st)
{
try {
serialOut.write(st.getBytes());
} catch (IOException e) {}
}
Here i am just writing a String to the Serial Port.
The Arduino code to read the String:
void loop()
{
byte byteAvailable = Serial.available();
if(Serial.available() > 0)
{
lcd.setCursor(1,2);
lcd.print("Available");
}
What i am doing in the Arduino code is to write "Available " to an lcd screen
if i have data available in the serial port.
The main.java class will be something very easy like:
public class main {
public static void main(String args[]) throws Exception
{
serialCom test = new serialCom();
test.begin();
test.writeSerial("hello");
}
the begin() function is where i set up the port.
In this way i never get anything on my lcd screen unless i replace
test.writeSerial("hello");
with
while(true)
test.writeSerial("hello");
in the main function.
If i keep writing data to the serial port then i actually receive it. But i really need to have something much faster.
Here is how i set up the port..
public void begin() throws Exception
{
String serialMessage = "c" ;
CommPortIdentifier port = CommPortIdentifier.getPortIdentifier( "/dev/ttyACM2");
CommPort commPort = port.open(this.getClass().getName(),2000);
serialPort = (SerialPort) commPort;
//setting up the port. ;?ake sure that thew communication speed is the same in the Arduino IDE
serialPort.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
serialIn=serialPort.getInputStream();
serialOut=serialPort.getOutputStream();
serialReader = new BufferedReader( new InputStreamReader(serialIn) );
serialPort.addEventListener(this);
serialPort.notifyOnDataAvailable(true);
}
Anyone can help?
thank you