11-11-2014 02:11 PM
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
11-11-2014 03:45 PM - edited 11-11-2014 03:46 PM
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.
11-12-2014 08:06 AM
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!
11-12-2014 08:32 AM
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.
11-12-2014 01:46 PM
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:
Thank you very much!
11-12-2014 01:58 PM
11-12-2014 03:07 PM
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
11-27-2014 04:52 AM - edited 11-27-2014 05:01 AM
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
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:
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 appreciated
Kathi
11-27-2014 09:00 AM
11-27-2014 09:16 AM
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?