LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Arduino and LabVIEW Communication - Strange behavior

Solved!
Go to solution

Hi y'all,

 

let me get right into the topic.

 

Right now i try to use the Arduino Genuino Micro to work with LabVIEW.

 

Things work perfectly when i use a terminal or console.

 

The Programm works like this. If i send a 'f' char i get 'ok' as an answer. If i send a '?' i get the output 'A100'.

 

char receivedChar;
boolean newData = false;


void setup() {
  Serial.begin(9600);
  Serial.println("Arduino is ready!");

}

void loop() {
  recvOneChar();
  if(receivedChar == 'f' && newData == true)
  {
    Serial.println("ok");
  }
  if(receivedChar == '?' && newData == true)
  {
    Serial.println("A100");
  }
}

void recvOneChar()
{
  if (Serial.available() > 0)
  {
    receivedChar = Serial.read();
    newData = true;
  }
  else{
  newData = false;
  }
}

Now to the strange thing, when i work with a terminal program like "Serial Input Monitor" or "H-Term" the device responses as it should. You can see it in the picture with the text "Terminal".

 

Now it gets iexciting, when i send the same strings with LabVIEW, i get different responses. It look like lag.

 

The first message is always correct. When i try an other character it responses with the wrong responses. See the picture with the text "LabVIEW". The problem is, this only happens in LabVIEW and nowhere else.

 

Any ideas? 

 

Download All
0 Kudos
Message 1 of 11
(4,429 Views)

In order to receive all data from serial port, you should read till you receive your termination character or till no more data available on serial port after a certain delay.

I cannot understand the language that you write your note in the VI.

 

Benoit

Message 2 of 11
(4,426 Views)

Thank you for your time and sorry for the VI! That is not important anyway anymore.

 

I wrote a code where the Arduino reads until the Termination char is at the Port, still the same result.

 

I just wrote the new code for the Arduino, so it returns everything that it reads. Well the result is a bit confusing.

 

It is still the same, in both scenarios they get the same Strings, yet behave differt. I just have no clue what is it that LabVIEW does differntly. I mean the just send the exact same thing, how comes that it doesn't yield the same results?

Download All
0 Kudos
Message 3 of 11
(4,395 Views)

I don't see where you configured your serial port in LabVIEW.  This seems to be a subVI, do you have a main VI calling this?  IF so, please attach it.

 

Why are you using so many Value property nodes?  Use the actual terminals.  I don't see why you are initializing the terminals to false at the beginning of your VI.

 

 

0 Kudos
Message 4 of 11
(4,376 Views)

@TheTinyBang wrote:

I just wrote the new code for the Arduino, so it returns everything that it reads. Well the result is a bit confusing.


Could you share this code?  It kind of looks like a normal C string is being written (always ends with a NULL, 0x00) by the Arduino.


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
0 Kudos
Message 5 of 11
(4,374 Views)

I have worked with Arduino with LabVIEW.

I have worked with Arduino with Putty.

 

There is no difference.

 

The difference is what the OP is doing that he has not told us yet!

It is known as PICNIC . 

Smiley Happy

.

0 Kudos
Message 6 of 11
(4,354 Views)

TL;DR: RS232 is hella slow, needed some time between sendin a request and receiving one. Short: Send -> Wait -> Read. Not like mine, Send->Read.

 

Sorry for keepin you all waitin!

Over the weekend I tried to analyze the problem, now I have the solution. It is at the bottom of this Post.

The problem was my shitty programming.

  

First here is the Arduino-Code

 

const byte numChars = 32;
char receivedChars[numChars]; // an array to store the received data

boolean newData = false;

void setup() {
  Serial.begin(9600);
  Serial.println("<Arduino is ready>");
}

void loop() {
  recvWithEndMarker();
  showNewData();
}

void recvWithEndMarker() {
  static byte ndx = 0;
  char endMarker = '\n';
  char rc;

  // if (Serial.available() > 0) {
  while (Serial.available() > 0 && newData == false) {
    rc = Serial.read();

    if (rc != endMarker) {
      receivedChars[ndx] = rc;
      ndx++;
      if (ndx >= numChars) {
        ndx = numChars - 1;
      }
    }
    else {
      receivedChars[ndx] = '\0'; // terminate the string
      ndx = 0;
      newData = true;
      newDaten = true;
    }
  }
}

void showNewData() {
  if (newData == true) {
    if(strcmp(receivedChars,"f") == 0)
    {
      Serial.println("OK");
    }
    if(strcmp(receivedChars,"?") == 0)
    {
      Serial.println("A100");
    }
    newData = false;
  }
}

 

Over the course of the weekend I tried different combinations of LabView and Windows.

  • Windows 10 + LabView 2018
  • Windows 10 + LabView 2018
  • Windows 7 + LabView 2013
  • Windows 7 + LabView 2013

All on 3 different PC's and Laptops

 

Now in all instances I still got the same problem. The Arduino doesn't respond to the first string, but when the second string is send, the Arduino responses to the first string. When the third string is send, it responses to the second string.

 

I checked if the Arduino doesn't receive some strings. So I wrote a code that records everything that comes in and gives it back to me. It did indeed receive every single string.

 

So in the end I hooked up a oscilloscope to see what is send and what is received.

And voila! It always responses. Then it finally clicked. Right after I sended a request I just tried to read the buffer. But it takes a bit of time for the Arduino to respond.

 

 

SOLUTION

The fault lied here:

The faulty versionThe faulty versionThe workin versionThe workin version

 So all in all it was my fault. Sorry to bother y'all!

 

 

Thanks for your time, have a great day my friends.

 

 

0 Kudos
Message 7 of 11
(4,320 Views)
Solution
Accepted by TheTinyBang

What you show is not a solution.

 

The problem is that you are using Bytes at Port which is the wrong thing to use 99% of the time.  Even your "working" program will break if the while loop stops once it receives only 2 of let's say 5 bytes in your message, because you are set to read only the number of bytes that it saw when it first detected there were more than 0 bytes.

 

The correct solution is to use the termination character.  When you use println, you are sending a carriage return.  So set your LabVIEW code with the VISA Serial configure to have the termination character enabled and be a carriage return.  By default, what you have now when nothing is wired in is that it is enabled and is a linefeed character.  So you need to wire a decimal 13 into the termination character input if it happens to be a carriage return.  Then you will just read a sufficiently large number of bytes that is higher than your longest message.

 

 Example_VI_BD

Message 8 of 11
(4,314 Views)

That is why I love this community! Thanks for the information!

0 Kudos
Message 9 of 11
(4,302 Views)

1. You might want to consider using the Serial.readStringUntil() function in your Arduino code, which will simplify things.

2. DO NOT USE THE BYTES AT PORT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

As has already been stated, your Arduino is using the PrintLn command.  This means it adds a Carriage Return (0xD, CR) and a Line Feed (0xA, LF) to the end of the message.  So all you need to do is enable the termination character and set it to the Line Feed (both defaults on the VISA Configure Serial Port).  Then you just tell the VISA Read to read more bytes than you ever expect in a message (I like to use 50) and the VISA Read will wait until it reads that termination character, it reads the number of bytes you tell it (again, set the number of bytes to be larger than any message will be), or you get a timeout.

 

So from RavensFan's code, I would just delete the constants on the top of the VISA Configure Serial Port and it will work just fine.


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 10 of 11
(4,296 Views)