Hello!
During my current project i have hit a kind of wall. When trying to communicate between a java program(Using rxtx 64bit) on Windows 8(64 bit) and a Arduino UNO Rev 3, it periodically halts.
And with halts, i mean it takes the program about 600ms to make a "handshake"(Five bytes from the computer, one back from the arduino) at a baud rate of 115200 bits/second.
Another thing which i have noticed happens is that the Arduino's RX(in)/TX(out) serial light light up in an odd fashion. Without errors, the lights flash as they should, but when it halts, the RX light remains on, while the TX light blinks at the speed of the responses.
NOTE: The system is supposed to be able to use the full baud rate for communication and this is a way of testing it, basically a form of stress test.
NOTE 2: The handshakes might not be that efficient / to often, but this is by design, at least while debugging.
This is an output log i have created to demonstrate the issue.
It's build up with: "NUMBER_OF_BYTES_SENT: (I_IN_ITERATION_OF_MAX_60) = NUMBER_OF_MS_IT_TOOK_TO_PERFORM_HANDSHAKE"
and a line("-------------") when the problem temporarily resolves itself.
ex:
130735: (46) = 503ms
130740: (47) = 606ms
----------------------
130780: (5) = 606ms
Full logs that follow the same log pattern. They might not be that interesting but show some kind of error pattern:
https://dl.dropboxusercontent.com/u/39389013/Debug/1.txt
https://dl.dropboxusercontent.com/u/39389013/Debug/2.txt
The test code running on the Arduino is:
//#define serialDataRate 9600
//#define serialDataRate 38400
#define serialDataRate 115200
int last;
int rd;
long lastTime = 0;
void setup() {
Serial.begin(serialDataRate);
last = 0;
lastTime = millis();
}
void loop() {
while (Serial.available() >= 5) {
lastTime = millis();
for(int i = 0; i < 5; i++){
rd = Serial.read();
if(millis() - lastTime > 100)
Serial.write(2); // Never happens?
last = rd;
}
Serial.write(1); // Handshake
}
}
Demo code running on the computer:
package default;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
public class MainController {
CountDownLatch latch = new CountDownLatch(1);
SerialController sc;
public MainController() {
sc = new SerialController();
SerialEventListener serialEvent = new SerialEventListener() {
public void Recived(int data) {
recivedPacket(data);
}
};
sc.setEventListener(serialEvent);
sc.initialize();
System.out.println("Serial baud rate: " + sc.serialPort.getBaudRate());
}
public void recivedPacket(int data) {
switch (data) {
case 1:
latch.countDown();
// System.out.println("Hand shake");
break;
case 2:
System.out.println("Error!");
break;
default:
System.out.println("Recived other:" + data);
break;
}
}
int bt = 0;
long lastTime = 0;
boolean divide = false;
byte[] buffer = new byte[5];
public void update() {
lastTime = System.currentTimeMillis();
for (int i = 0; i < 60; i++) { // this is used since my real project,
// when under maximum capacity needs to
// be able to send 60 packets of data
// every "frame".
for (int itt = 0; itt < 5; itt++) { // Create a packet of 5 bytes.
buffer[itt] = (byte) ((bt++) % 255); // Create a buffer to send.
// Sends an array of bytes
// in numerical order, 0 to
// 255, and then back to 0.
}
try {
sc.write(buffer);
} catch (IOException e1) {
e1.printStackTrace();
} // Using rxtx, writes the array to the serial connection.
try {
latch.await(); // Await the handshake
} catch (InterruptedException e) {
e.printStackTrace();
}
if (System.currentTimeMillis() - lastTime > 10) {
System.out.println(bt + ": (" + i + ") = " + (System.currentTimeMillis() - lastTime) + "ms");
divide = true;
} else if (divide) {
System.out.println("-------------------");
divide = false;
}
lastTime = System.currentTimeMillis(); // Update the time so that
// only the issues are
// logged.
}
}
public static void main(String[] args) throws Exception {
MainController mc = new MainController();
Thread.sleep(4000); // Wait 4 seconds for the connection to establish
while (true) {
mc.update();
Thread.sleep(50); // Wait time, can be lower, but this seems like a good place to start when debugging.
}
}
}
SerialController.java (sc). Modified version of a file that I've taken taken from an example on how to set up a connection:
https://dl.dropboxusercontent.com/u/39389013/Debug/SerialController.java
It also requires the rxtx library: RXTX for Java.
I'm sorry for not having a full project to share, but this should be enough to see the problem in action.
This is a problem I've been scratching my head at for hours, but with nothing to show for, so help with find a fix to this problem is deeply appreciated!
Thanks in advance.
Best regards
Ledge.