From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Serial Communication Labview - Arduino

Hi all

 

I am really new to Labview and I am trying to set up a Serial Communication between my Arduino board and Labview.

I tried to write a really simple program that just prints out random numbers and read them in Labview, but I fail.. Can someone help?

 

Here is my Arduino Code, my LabView-VI (made up of examples I found in this forum) is attached:

 

long randNumber =0;
int readNumber=50;
void setup() 
{
  Serial.begin(9600);    // opens serial port, sets data rate to 9600 bps
}

void loop() {
  randNumber = random(readNumber); //dont read.. just print..
  Serial.println(randNumber); 
  delay(100);
}

 

I was also wondering if I don't need something like a "Handshake" between LabView and Arduino to make sure the Serial Connection is set up properly before I start reading out data? I did that when using SerialCommunication with Matlab and was wondering how to do that in LabView.

 

In the long run I would like LabView to read the SerialData from my digital Sensor (on Arduino UNO) that prints out 3 Values (X,Y and Z) on one line.

 

My LV-code will run through a few loops and at each step I want to measure and save the data. Will I have to open and close the serial port each time or can I open it once, read values at each step (even though I do much other stuff in between) and close it at the very end?

Please excuse my lack of knowledge, a working basic example would help a lot to get me started because I did not find anything working on the internet..

 

Thank you very much!

Kathi

0 Kudos
Message 1 of 11
(9,152 Views)

Do not use "Bytes at port" put a Line Feed as a term char in your Ardunio sketch and use that to terminate reads before a timeout.ardSeriale.PNG

 

 

========================
=== Engineer Ambiguously ===
========================
0 Kudos
Message 2 of 11
(9,126 Views)

Hey RTSLVU

 

Thanks for your answer.. would you mind specifying a bit more what you did here and why? It all sounds a bit japanese to me.. (sorry!) And would you mind uploading your changes in the *.vi and *.ino project so I could have a look at that minimal working example and build on that 🙂

 

Thank you very much!

 

0 Kudos
Message 3 of 11
(9,091 Views)

If your Arduino is sending a termination character (specific character that states the transmission is complete), then use it in your LabVIEW code.  With the Configure Serial Port, enable the termination character.  Then do not use the Bytes At Port.  Instead, just read a large number of characters.  The VISA Read will stop when the termination character is found, you read the desired number of bytes, or a timeout has occurred, whichever happens first.

 

Then, move your indicators inside of the loop.  That way they will actually get updated with each read.

 

And use a stop button to stop your loop.  Do NOT use the abort button to stop your code.  That doesn't allow your code to perform any cleanups.


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 4 of 11
(9,084 Views)

Perfect, thank you. I get it now more or less, you enabled the termination character, but where is it specified in the LabView Program? I don't use one in my arduino sketch and neither doI have it in LabView.. so why do we enable it?

 

I have two last questions about the serial communication, I tried to simply print out three values like this:

 

void setup() 
{
  Serial.begin(9600);    // opens serial port, sets data rate to 9600 bps
}

void loop() {
  Serial.println(200);
  Serial.println(20);
  Serial.println(-100);
}

 and then read them with Labview, show them to me in three different Waveformcharts and also write it to a txt file.

Now I encountered a few problems:

  1. Sometimes it reads 200 as first value, sometimes 20, sometimes -100, it just switches around, but 200 should always be the first value, how can I make sure of that? Do I just need to wait longer or kind of mark the values with start/termination characters so it knows it is now reading the first value?
  2. My text file screwes up, I tried to have the three values on one line and then go to the next line for the three new values.. I can't figure out what's happening, I tried Constant Space, Constant tab, empty string...

Thank you very much!

 

0 Kudos
Message 5 of 11
(9,057 Views)
You should not mark a thread as solved if you still have questions.

Your program design is fatally flawed. The communication to it is completely asynchronous so it is impossible to know which is the first, second, or third value. You can append the three values with something like a comma between them and a termination character at the end. You can add a unique prefix to reach value. You can send a specific value when requested.

Please look at the Linx sketch and other arduino sketches. The problem is at that end - not LabVIEW's.
0 Kudos
Message 6 of 11
(9,052 Views)

In Arduino there you have multiple functions to send data over the serial connection.

The two commen functions is print and, the one you are using, println.

 

The function println add a termination character to the end of the string, data, that you what to send.

This means that  every value you send right now has a termination character.

You could fix this by changing the first two println function to print function.

 

Then a little about the serial communication:

There are no handshake in serial communication. This means that when the Arduino sends your data, it does not care and can not know if there was anybody that received the data. It will just blindly send the data.

Depending on when you start your LabVIEW program and open for serial communication, the Arduino could have send hundreds of values.

 

There are multiple ways to "fix" this.

One could be to program your Arduino to wait on some data from your LabVIEW program before it sends data back. This requeires that you program your LabVIEW program to send some data before it starts to read serial data from the Arduino

Message 7 of 11
(9,035 Views)

Hey dkfire, thank you for your answer, I think I am now understanding the whole thing a bit more.. I tried to kind of synchronize my labview data to the data that is sent by arduino. It works somhow but I get the following issues:

 

- if my VISA Read is set to asynchronous, I get BlueScreen...

- If it is set to synchronous, there is Data missing, sometimes it measures only x and y and then three times z and it just goes crazy which makes it impossible to work with the data in MATLAB afterwards, using reshape.

 

Can anyone help me how to improve my code? I  know it's bad programming but I have never worked with Arduino nor Labview ever before so it is all pretty new to me so please be kind Smiley Happy

 

Here is my Arduino-Loop Code from my sensor:

void loop() {
 getSensorValues(); //update x,y and z
  
 //print out the x,y,z Values
 Serial.print('x');
 Serial.println(x); //print out x
 Serial.print('y');
 Serial.println(y); //print out y
 Serial.print('z');
 Serial.println(z); //print out z
  
 delay(100); //for readability
}

 and my labview code to read from the sensor and write it to a textfile (including position, given by another VI) is attached and looks like this:

 

measuresensor.PNG

 

 

 

I split up the data that is read by the sensor into the cases x, y and z and write them to a textfile that just has all the data one below the other, like:

0.1     <-- x position (given)

0.5     <-- y position (given)

3        <-- z position (given)

x3.9

y7

z2

x3.8

y7.1

z2

x3.9

y7

z2.1

 

 

The whole measurement-VI is inside a Loop that does other things, then measure and then wait 200ms (maybe its something with that?)

 

Any help on how I can make my code better is highly appreciatedSmiley Embarassed

 

Kathi

 

0 Kudos
Message 8 of 11
(8,821 Views)
You have no while loop do the code only runs once. Do NOT use the run continuous button.

You did not follow my suggestion to send all values at once so you'll still have a synchronization problem though it is better.
0 Kudos
Message 9 of 11
(8,784 Views)

Would you mind being a bit more specific? I am lost here 😞

 

I have a for-loop in which I call the measurement Sub-VI, so why would I need a while loop around my subVI? 

 

Also you suggested I could separate my values using commas, but how can I make sure I get 3 times all my 3 values?

 

0 Kudos
Message 10 of 11
(8,778 Views)