Cannot write serial Data to Arduino

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

I find Java code particularly impenetrable but I have written code in JRuby (which uses the JVM) to communicate with my Uno.

A common problem for beginners is that their PC code doesn't allow for the time it takes the Arduino to reset when the Serial port is opened. It's a good idea for the Arduino to send a short message to the PC from setup() and for the PC code to wait for that before sending anything.

This demo illustrates the technique and I think it includes JRuby code as well as Python.

...R

Hi,
that helped a lot!!
As you said i didn't give time enough to Arduino to set up the connectionm, that's how i modified the Java code:

public class main {
	public static void main(String args[]) throws Exception
	{
		
		serialCom test = new serialCom();
		test.begin();
		
               Thread.sleep(2000);
		test.writetoport("hello");
		
     }

Thread.sleep(2000) stops everything for 2 seconds then it writes "hello" to the serial port.
This solution works very well.

Thank you

I assume serialCom test = new serialCom(); is what opens the serial port

I don't know what test.begin() does and it may well be appropriate to run it in parallel with the Arduino resetting.

I would expect 2 seconds to be too short and something closer to 10 might be needed. It may be that the code in test.begin() is providing some additional waiting time.

...R

giulianoAI:
i set up a serial communication between my Java environment and Arduino.

Which Arduino?

How to use this forum