10-13-2020 11:22 AM
LabView NXG 5.0 Community Edition
I'm trying to send a simple command to an Arduino over a Serial connection:
It's really simple: configure the port, send one byte, read a response then close the session. I've configured the Arduino on a COM port via NI Max.
The issue is that when I run this code it always times-out on the VISA Read. EXCEPT if I turn on highlight execution or, say, break on the VISA port configuration and step through - in these cases it works perfectly and I get a response back from the Arduino.
It would seem that I can only get it to work by slowing down the code execution in some way. I've tried a delay in the code between write and read but it makes no difference; I've tried different variations of reading the Serial input on the Arduino, with and without delays, with no difference.
The only way I can get it to work is to turn on highlight execution or single-step through the VISA VIs.
The Arduino code is as simple as can be:
int var1;
String out;
void setup() {
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
var1 = Serial.read();
if (var1 == 'A') {
out = "Andrew\n";
} else if (var1 == 'K') {
out = "Katherine\n";
} else {
out = "Unknown\n";
}
Serial.println(out);
}
}
As I say, I've tried variations on the Serial access.
I've tried with NI IO Trace and the output of that for both scenarios (highlight execution, straight run) is exactly the same except for the timeout on the Read. The only potential pointer is that with highlight execution there is a 1.5second elapse time between the write and the read and 3ms for the straight run (hence why I've tried putting in delays.)
Has anyone got any idea of what may be happening?
Solved! Go to Solution.
10-13-2020 11:46 AM
Because of the bootloader, the Arduino resets when a serial port is opened, a process which can take a couple of seconds to complete. If you write to the board during this time, it will be lost. Instead of delaying between write and read, try a delay between open and write.
10-13-2020 12:01 PM
A couple of additional notes:
1. The Serial.PrintLn command appends a Carriage Return and a Line Feed to the string data you are writing from the Arduino. So your constants do not need the "\n" in them.
2. You should set the bytes to read on the VISA Read to be way more than you ever expect in a message. I tend to use 50 or 100. 10 will be on the hairy edge of working.
10-13-2020 02:27 PM
@Darin.K wrote:
Because of the bootloader, the Arduino resets when a serial port is opened, a process which can take a couple of seconds to complete. If you write to the board during this time, it will be lost. Instead of delaying between write and read, try a delay between open and write.
I did not know that (or had forgotten it) - but putting a delay in where you say works. Thanks for that quick response.
10-13-2020 02:29 PM
@crossrulz wrote:
A couple of additional notes:
1. The Serial.PrintLn command appends a Carriage Return and a Line Feed to the string data you are writing from the Arduino. So your constants do not need the "\n" in them.
2. You should set the bytes to read on the VISA Read to be way more than you ever expect in a message. I tend to use 50 or 100. 10 will be on the hairy edge of working.
1) Yep - I've played around with the code and this is just a hang up from older versions.
2) Yep (again) - I started with a much larger value and was just trying to see if 'too large' was an issue (I was clutching at straws!!)
Thanks for replying.