1

I am trying to have a 2way serial communication between LabVIEW and Arduino. I tried to make a basic program to grasp the concept but it does not seem to work.

Labview (2 int values) >> Arduino read

Arduino (1 int value) >> Labview read

Here, my LabVIEW program has 2 double variables which I put in numbers (they will later be cast to an int). Then, it is put into an array and converted to a comma-separated integer string. This is then written to the VISA port.

On the Arduino side, the serial port is read using the serial.parseInt function. Then the int is then converted into a 2-byte array which is then written to the serial port.

This is then read by the LabVIEW with VISA read and the string is converted to a byte array and reconstructed into an I16 integer.

Currently, the results are as follows: The sent number are 80 and 80 and after being received by the Arduino and relayed back, the results displayed is 6961 as a string and 26977 in int form.

The arduino program works with the serial monitor where if the number 19789 was put in, it would output MM.

enter image description here

void setup() {
  Serial.begin(9600);
  while(!Serial){;}
  Serial.println("serial ready");
}

int input;
int input2;
void loop() {
  // put your main code here, to run repeatedly:
  if (Serial.available()>0){
    input = Serial.parseInt();
    input2= Serial.parseInt();
  }
  
 byte buff[2];
  if (Serial.read() == -1){
    buff[1] = (byte)input;
    buff[0] = (byte)(input>>8);
    Serial.write(buff,2);
  }  
  delay(100);
}

labview VI enter image description here

3
  • It's not entirely clear what you're actually sending or receiving. FWIW if you shift 80 (0x50)>> 8 you're going to end up with zero. You're also throwing input2 away, perhaps that was intentional. It might be a good idea to isolate each side of the comms, e.g., test the Arduino code in isolation until it's doing what you actually want, and introduce LabView afterwards. – Dave Newton Jun 1 at 16:08
  • yes, I know that it will be removed since int is 2 bytes and I would like to split the int into 2 bytes in the array for sending. Then these 2 bytes will be reconstructed later in LabVIEW. this is done in case i am sending a number larger than 256. – trex98 Jun 1 at 16:25
  • It's an int, you can just check if it's > 256 and stuff 0 info buff[0]. The intent isn't particularly clear, though. – Dave Newton Jun 1 at 16:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.