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: 

NI-VISA - How to Send More Than One Serial Value to Arduino

As the name suggests, I am using the NI-VISA plugin to control an Arduino UNO via serial communication. For the sake of example, I have one code on the Arduino that measures analog voltage, and the other code that will count the frequency of an analog wave. If I add both of the codes together, I would be serially printing the value of analog voltage as well as frequency respectively, but the issue I'm facing is that NI-VISA is only able to read one value, I have no clear direction on how to LabVIEW will read different serial printing sent by Arduino.

 

There is another issue that I am facing with sending serially sending data to LabVIEW. The values are always sent as whole numbers, and not as fractions, eg, if the voltage calculated by the arduino is 4.235V, LabVIEW reads this as 4V. I have attached the codes, images, as well as the LabVIEW VIs in discussion. Any help would be greatly appreciated.

 

void setup() {
  Serial.begin(9600);

}

void loop() {
  int pot=analogRead(A0);
  Serial.println(pot);
}

 

const int pulsePin = 8;       //digital pin 8 of arduino used as frequency counter
int pulseHigh;                //to count high time in us
int pulseLow;                 //to count low time in us
float pulseTotal;             //high time plus low time equals total time
float frequency;              //calculated frequency
void setup()
{
    Serial.begin(9600);        //to send to labview
    pinMode(pulsePin,INPUT);  //setting pin state as input
    delay(5000);              //compiler based delay, timer will be learned and implemented later
}
void loop()
{    
    pulseHigh = pulseIn(pulsePin,HIGH);
    pulseLow = pulseIn(pulsePin,LOW);
    
    pulseTotal = pulseHigh + pulseLow;        //time period of the pulse in microseconds
    frequency=1000000/pulseTotal;             //trequency in Hz
    
    Serial.print(frequency);                  //Send to labview
    //Serial.println(" Hz");                    //send to labview
    delay(50);                               //0.5 second delay
}
0 Kudos
Message 1 of 2
(947 Views)

Hi Umerator,

 


@TheUmerator wrote:

 

There is another issue that I am facing with sending serially sending data to LabVIEW. The values are always sent as whole numbers, and not as fractions, eg, if the voltage calculated by the arduino is 4.235V, LabVIEW reads this as 4V. I have attached the codes, images, as well as the LabVIEW VIs in discussion. Any help would be greatly appreciated.

 

void setup() {
  Serial.begin(9600);

}

void loop() {
  int pot=analogRead(A0);
  Serial.println(pot);
}

 


The variable "pot" is declared as "int", so why do you even expect to receive fractional values?

 


@TheUmerator wrote:

If I add both of the codes together, I would be serially printing the value of analog voltage as well as frequency respectively, but the issue I'm facing is that NI-VISA is only able to read one value, I have no clear direction on how to LabVIEW will read different serial printing sent by Arduino.


You could send both values separated by a comma or semicolon like

serial.println(pot, ";", frequency)

Then you will also receive both values in one message on the LabVIEW receiver side…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 2 of 2
(908 Views)