LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Serial communication with Arduino and VISA

Gurus of LabVIEW,

Please feel free to redirect me if this question has already been asked (I've looked around, but I haven't found what I'm looking for). I have created a program that reads data from the serial monitor in an Arduino that outputs "Hello World!", 100 ms delay, "1", 100 ms delay, "0", and 100 ms delay over and over again and I'd like to bring this data into LabVIEW via the VISA functionality and then split each of the strings into individual arrays (i.e. Hello World! a bunch of times, 1 a bunch of times, and 0 a bunch of times each in distinct arrays)

I have attached my LabVIEW and Arduino code for reference. (I had to write the Arduino code at the end since it won't allow me to attach it) Btw, I have connected pins 11 and A0 on the arduino in order to test just FYI.

Essentially the problem I'm running into is that every so often when I run the program "Hello World!" is not the first string of bytes that the VISA function reads. I'm guessing I could fix this with termination? I'm unsure as I have tried a few different things with limited success. Ideally, I'd want the LabVIEW program to wait until it finds "Hello World!" and start at that point so that everything would be in sync.

Arduino code:

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(11,OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
Serial.println("Hello world!");
delay(100);
digitalWrite(11,HIGH);
delay(100);
Serial.println(digitalRead(A0));
delay(100);
digitalWrite(11,LOW);
delay(100);
Serial.println(digitalRead(A0));
}



0 Kudos
Message 1 of 5
(3,813 Views)

Println sends a termination character, doesn't it?

 

Instead of doing a printlline for each and every piece of data which keeps you from distinguishing one piece of data from the other, separate the items by some other separator, and save the printlin and its output of a termination character for the last piece of data.

 

Then in LabVIEW, you can enable the termination character, read a large number of bytes (more than the longest message you ever expect to get), and break apart the pieces of data by way of the separator.  Perhaps a tab character or a comma.

0 Kudos
Message 2 of 5
(3,807 Views)

RavensFan,

Okay, I think I've done that now. I still get random mixed up letters at the beginning randomly.

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(11,OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
Serial.print(",Hello world!,");
delay(100);
digitalWrite(11,HIGH);
delay(100);
Serial.print(digitalRead(A0));
Serial.print(",");
delay(100);
digitalWrite(11,LOW);
delay(100);
Serial.println(digitalRead(A0));
}

Download All
0 Kudos
Message 3 of 5
(3,785 Views)

RavensFan,

Nevermind, I see what you meant. Send it as "Hello World!,1,0" rinse/repeat then use labview functions to split up. The only problem is now I'm getting errors from the communication as shown in the image.

0 Kudos
Message 4 of 5
(3,774 Views)

You are still making a few mistakes.

 

You are NOT reading a large number of bytes like I said.  You are using Bytes at Port which is the wrong thing to use like 99% of the time, and certainly when you have a termination character.  Get rid of that and just wire a large number into the VISA Read.  It looks like your typical message is about 17 bytes.  ??  Well go and wire a large number like 100.

 

The even bigger problem is that your VI shows that the bytes at port is up to 231 which means your VI is falling behind the incoming data.  Why?  Your script seems to have a total value of delays of about 400 msec.  (I see 4 statements of Delay(100), so I imagine that is 100 msec)    So your device is sending new data about every 1/2 second.  But your For Loop has a delay of 1 second (1000 msec) in it.  For every two messages your device sends, you are only reading one, thus your buffer fills up.  Get rid of the Wait function in the For Loop.

 

Third, you have three array indicators called Hello World?,  One?,  Two?  I imagine that means you want each message broken into those 3 arrays.  Well you never break apart the messages and build just 1 array at the auto-index tunnel.  Then proceed to use the Decimation function to break that into 3 1-D arrays.   Read the help for Decimate Array and you'll see that is probably not the function you want.  Break the string apart inside the loop, then build that into 3 arrays using auto-indexing tunnels.

0 Kudos
Message 5 of 5
(3,738 Views)