Hello,
I tried to send data to the arduino, but it doesn't work, beacuse the led is switching off after 3 seconds.
Java code:
package Main;
import jssc.SerialPort;
import jssc.SerialPortException;
public class Main {
public void main(String[] args) {
SerialPort serialPort = new SerialPort("/dev/ttyUSB0");
try {
serialPort.openPort();//Open serial port
Thread.sleep(3000);
serialPort.setParams(9600, 8, 1, 0);//Set params. Also you can set params by this string: serialPort.setParams(9600, 8, 1, 0);
serialPort.writeBytes("a".getBytes());//Write data to port
Thread.sleep(3000);
serialPort.closePort();//Close serial port
}
catch (SerialPortException ex) {
System.out.println(ex);
}
catch(InterruptedException e) {
System.out.println(e);
}
}
}
As far as I can make out your Java program does this
opens the Serial port - this will reset the Arduino and turn off the LED
waits 3 seconds with the LED off
writes an 'a' to the Serial port - this will turn on the LED
waits 3 seconds with the LED turned on
repeats the four commands above
This exactly matches what you see on the Arduino
What happens if you only open the Serial port once and leave it open ?
yes this is possible ! as far as i know there are 2 ways, (but probably more)
1- the automatic reset when opening the port can be disabled through a hardware modification (i have a switchable 10uF capacitor on the reset-pin and GND of my Mega (this disables reset, but also stops new sketches to be uploaded, hence the switch)
2 - you can use a separate USB <=> TTL and connect that to hwSerial (or a swSerial for that matter) It's the on board USB connection that sends a pulse to the reset pin.
I have never wanted to run a PC program that communicates with an Arduino and then closes down while leaving the Arduino program running so I was not sure what would happen.
I created a very slightly modified version of my Python - Arduino demo using a Mega so that it could send a message from setup() through Serial3 to a separate PC connection using a USB-TTL cable. That way I could see if the Mega was reset when the Python program closed.
And the result is that closing the Python program did not cause the Mega to reset.