LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Serial communication with LabVIEW and arduino

Solved!
Go to solution

Hello

I am trying to communicate with arduino from LabVIEW using Visa Write/Read. 

Problem: From all other softwares like Ni Max I can communicate really well with arduino but not from LabVIEW.

 

*not the line feed character problem. I corrected that.

*In ni trace I can see that the data is send and I can see the Rx led at arduino blinking so arduino is recieving the data send.

*if I use a while loop and write like 2-3 times with visa write then the communication works again.

 

Can someone help me to solve the issue ? 

I am also posting a code that is working but I am writing 3 times but I get only 2 times an output at visa read and also a timeout error message.

Thank you 

Bitto 

  

0 Kudos
Message 1 of 16
(21,081 Views)

Are you sure you correct any line feed character problem?

 

My guess is the problem is in your Arduino code, but you didn't attach that.

0 Kudos
Message 2 of 16
(21,079 Views)

Thank you for the reply. The problem is not with the line feed because it works if I write like 3 times as you can see in my code and also the code in arduino is just does this : if serial.available then write back what I read.   

0 Kudos
Message 3 of 16
(21,066 Views)

To put it a little more bluntly than RavensFan did...

image.png

 

In the PICTURE of your code, you clearly show the Term Char being set to false. Since it is just a PICTURE, we are unable to tell what characters you are sending.

The error you are getting is most likely from the fact that you set your timeout to be 2 seconds, then wait for 3000 characters to arrive in your VISA read. Are you expecting 3000 chars? Remember, your Term Char has been disabled, so it is patently waiting for ALL the characters to arrive.

---------------------------------------------
Certified LabVIEW Developer (CLD)
Message 4 of 16
(21,058 Views)

 I added the termination concatenating string and I changed the code in arduino in such a way that I don't really need a term char to get a reply. 2 Seconds 300 characters is stupid, I agree I was trying to check what is happening and messed here and there I guess. I will upload the simple version of the code which still is not working normally but working with  visa write inside a for loop. Could you please check the program.

Thank you

Download All
Message 5 of 16
(21,050 Views)

I do not see any problems with the labview code. The only thing I could suggest would be to clear the buffers before writing anything. (Generally only a problem when connecting or disconnected the RS232 cables, or connecting to a spewing source)

image.png

The only thing else I can suggest is to put a loop-back cable on the RS232 port on the PC side. This will prove the labview code is solid. Typical trouble shooting steps. IE Divide and conquer 🙂

 

Good luck

---------------------------------------------
Certified LabVIEW Developer (CLD)
Message 6 of 16
(21,046 Views)
Solution
Accepted by topic author bj40

Which Arduino are you using? According to the FAQs if you are using one with a native USB you need to wait after serial begin.

 

Void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB
  }
}

void loop() {
  //proceed normally
}
 
Something else to consider is that on most Arduinos (unless specially modified), opening a serial connection on it's USB port will trigger a reset of the MCU. This is by design, to restart at the bootloader to allow reprogramming etc (it has to assume it was the Arduino IDE that just opened the COM port, until it knows otherwise).
 
In your LabVIEW code, I would add some delay after opening the port for the first time, but before you sent it any data.
This will allow the Arduino time to reboot, start executing its main program and be in a position to talk with your software over the serial port.
 
0xDEAD
 
EDIT: The more I think about this, the more certain I am that adding a delay in LabVIEW after opening the serial port will fix this problem.
The Arduino will still be rebooting while you are firing the first data at it.
Let us know how it goes.

 

Message 7 of 16
(21,012 Views)

I tried adding a timeout before the write function and also changed the arduino code as you mentioned. It didn't make any difference. 

Message 8 of 16
(20,979 Views)

Tried, no difference.

0 Kudos
Message 9 of 16
(20,978 Views)

The program works well after adding a waiting time before Visa write but I had to give a wait of 2 Seconds.

Anyway it works well now thank You 🙂

0 Kudos
Message 10 of 16
(20,975 Views)