Communicating Arduino with Java by serial problem

Hi All,

I'm trying to communicating Arduino with Java by serial. I have a program running in the loop which needs to produce signals in microseconds.

The issue is when I try to connect to the Arduino using Java (RxTx library) through serial, at the point where the communication happens between java and arduino there are somewhat seconds delay and this stops the continuation of the loop leaving without sending any signal for that time. Then once the communication is done the loop starts to continue.

As a simplest example if you have a Blink Led program running on the main loop,at the point of Java and Arduino communication that Blinking program stops for some seconds and continues back once the communication is successful.

Is there anyway that I can do the serial communication without interfering the main loop (the program which is running)?

Thanks
Nuwan

Is there anyway that I can do the serial communication without interfering the main loop (the program which is running)?

Arriving serial data should not interfere with your "main loop", because a byte is copied into the incoming serial buffer very, very quickly.

Your reading of the data from the buffer could interfere with your "main loop", but we can't see how or why, since you didn't post your code.

@PaulS - I think you got my question wrong, I'm not talking about the time where you send serial data to the Arduino using Java, I'm talking about the moment where the Java and Arduino hardware communicate (Handshake) for the first time.

This is where the java program discovers the serial port where the arduino is connected and starts its communication.

I hope this explains the scenario more.

Nuwan

When a PC program opens the Serial port it causes the Arduino to reset. You should write your PC program to expect that and to allow for the time it takes for the Arduino to go through its reset process. Your PC program should then keep the Serial port open until it is completely finished with the Arduino.

However, if what you want to do is to collect data from a running Arduino without causing it to reset the simplest way is probably to connect the PC to the Arduino using a USB TTL cable connected to the Rx and Tx pins (and GND).

...R

@PaulS - I think you got my question wrong,

Then you did a poor job of phrasing the question.

Had you posted some (Arduino) code, it would have been possible to understand what you were talking about. The information you added to your post clarified nothing.

@PaulS - I might have been written the question less descriptively, I think the reply to your first comment elaborates more into the issue. As I told you the code fragment is nothing but a blink code with Serial connection on its "setup"

void setup(){
  Serial.begin(9600);
  pinMode(13, OUTPUT);
}

void loop(){
  digitalWrite(13, HIGH);
  delay(1000);
  digitalWrite(13, LOW);
  delay(1000);
}

Thanks for the help.

I think I found the issue, its related to Robin's answer. The Arduino is resetting on the initialization of the
com port.

@Robin2 - I tried to use the software serial to achieve this but it seems my usb-ttl device is faulty. So I looked for a solution using the uno itself and found these links, I think they are what I was looking for. Need to try that out.

Thanks for the help.

https://tushev.org/articles/arduino/22/preventing-arduino-from-auto-reset-when-com-port-opens-closes
https://playground.arduino.cc/Main/DisablingAutoResetOnSerialConnection/

Nuwan

NuwanR:
@Robin2 - I tried to use the software serial to achieve this but it seems my usb-ttl device is faulty. So I looked for a solution using the uno itself and found these links, I think they are what I was looking for. Need to try that out.

In this situation I was not specifically recommending the use of SoftwareSerial. You can connect a USB-TTL converter to Rx Tx and GND and use regular HardwareSerial. However you will need to disconnect it when uploading a new program.

There are modifications to the hardware to prevent the auto-reset but they will probably interfere with uploading a new program which is why I prefer the USB-TTL converter. My suggestion is to buy a new one - they are not expensive.

...R

Hi Robin2, The way you proposed seems work fine. Before going to the hardware I tried to do it using software. I use RXTX to do the serial communication between Arduino and Java but it seems that setDTR(boolean) method doesn't work as expected.

Have you tried anything using software/code to prevent from Arduino not to reset when using the serial com? Do you know any library which works?

Thanks
Nuwan

NuwanR:
Have you tried anything using software/code to prevent from Arduino not to reset when using the serial com? Do you know any library which works?

That is not at all clear. Are you asking about preventing a reset when using the inbuilt USB connection?

If so, then you can only prevent that with a hardware fix - Google should find the details for you.

Be aware, however, that the normal process for uploading a program relies on the Arduino resetting.

...R