LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Unable to write to Arduino from LabVIEW through serial connection

Solved!
Go to solution

Hello, 

 

I am decently new to LabVIEW and am attempting to make an RC Plane motor test stand that will output values of RPM, Thrust, and Input Electric Power as supplied from an Arduino MEGA 2560. I have uploaded my VI that I am using to test the interaction between the Arduino and attached a picture of the test code (can't upload a .ino); this works for the most part except for my second VISA write function not being correctly read by the Arduino. I know that my VI is writing the data to the Arduino by using an indicator off the VISA Write VI return count parameter however the Arduino returns "1.00'\n'" when it should be returning the value as set by the gauge control on the front panel. 

If anyone has any experience using two serial write functions with Arduino that would be very helpful as I believe that it may be because the String buffer is not properly clearing after the button read but I'm not sure since everything online says that the Serial.read() function removes the byte value from the buffer. Some of the code that I have tried to fix this has been commented out for an indication as to what I have tried. 

 

Thank you! 

Download All
0 Kudos
Message 1 of 3
(207 Views)
Solution
Accepted by topic author FreezaIcey

It looks like you hae a race condition in your communications.  In your Arduino code, you read until a 1 is read, send back a "1" and then immediately check for new data.  But if the LabVIEW side hasn't even read the initial response yet and send the next packet, the Arduino will miss it due to the while(serial.available>0).

 

I think you are making the protocol too difficult.  Have the Aduino just read for the RPM setting.  If the is data, respond.  If not, send updated data.  On the LabVIEW side, only send the rpm when it changes.

 

You also should not need the Serial.flush() in there.  It just slows your code down.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Message 2 of 3
(158 Views)

Thank you for explaining that to me; I am unsure however, if it was the race condition that was causing the incorrect output as after simplifying the VI and the Arduino code I was still unable to get the correct output. I ended up switching the string concatenation method from effectively .concat((char)Serial.read()) to rpmString = rpmString + String((char)Serial.read()) and this ended up working. 

 

Again thank you for giving me some insight into how the program was working. 

 

Freeza

0 Kudos
Message 3 of 3
(91 Views)