LabVIEW Interface for Arduino Discussions

cancel
Showing results for 
Search instead for 
Did you mean: 

Sending String Array to and from Arduino

Hey ADrexelDragon,

If you want to send data to the Arduino within the LIFA framework you are going to need to use the Send/Receive and a custom command as you had started doing earlier. If you want to send numbers larger than 255 you will need to send them a byte at a time and then combine them into a single number using binary shifts and addition. There are examples of this being done in several of the cases including tone and analog read.

Kevin Fort
Principal Software Engineer
NI
0 Kudos
Message 11 of 46
(3,509 Views)

Kevin,

So here is what I am using to send my array to case 31 in arduino terms, case 0x1F:

ScreenHunter_01 Jan. 13 14.25.gif

Do I have to send this differently?

I think that this is where I am having trouble. I need to send this array byte by byte.

Correct?

If so, how do I do that?

Thank you.

CLD Certified 2014
0 Kudos
Message 12 of 46
(3,509 Views)

I am trying to receive a 4 byte number.

//////LabVIEW CODE

ScreenHunter_04 Jan. 13 17.44.gif

///////Arduino CODE

unsigned int incomingData = 0;

case 0x1F:

incomingData = (command[5]<<24 | command[4]<<16 | command[3]<<8 | command[2]);

Serial.println(command[2]);

Serial.println(command[3]);

Serial.println(command[4]);

Serial.println(command[5]);

Serial.println(incomingData);

break;

This code works with "12345" as shown below but not with "123456" I do not know why.

ScreenHunter_02 Jan. 13 17.42.gifScreenHunter_05 Jan. 13 17.47.gif

CLD Certified 2014
0 Kudos
Message 13 of 46
(3,509 Views)

Hey ADrexelDragon,

I think you are going over the max size of the unsigned int incomingData (check out the documentation here:http://arduino.cc/en/Reference/UnsignedInt)

Kevin Fort
Principal Software Engineer
NI
0 Kudos
Message 14 of 46
(3,509 Views)

Hey Kevin,

Yes. That is right. Silly mistake. Thank you.

How big of data array can I send at once to the Send/Receive subvi?

The program I created seems to send only 12 or less.

Can the Send/Receive subvi only work for 12 or less elements?

Thanks,

CLD Certified 2014
0 Kudos
Message 15 of 46
(3,509 Views)

With the Arduino packets how they currently are the max sixe you can send at once is a 13 byte array. You could change the number of Bytes per packet without too much work to increase this but I would suggest just using Send/Receive multiple times if you need that functionality.

Kevin Fort
Principal Software Engineer
NI
0 Kudos
Message 16 of 46
(3,509 Views)

In LabVIEW, I am sending this data array: {31, arraySize, Byte 1, Byte 2}

I am sending this data array, arraySize times.

Here is the arduino code used to gather the data from LabVIEW:

case 0x1F: //Recieve Data Array from LabVIEW

for (int i = 0; i <= command[2]; i++)

{

incomingData = (command[4]<<8 | command[3]);  //place data into array called incomingData

}

How do I know that incomingData array = the data array I am sending?

How would I print out the incomingData for me to see?

CLD Certified 2014
0 Kudos
Message 17 of 46
(3,509 Views)

Hey ADrexelDragon,

I am a little confused by your implementation here. I would think that if you have an arraySize constant you wouldn't always just pass two bytes to the function. Essentially what the code is currently doing is building an array(incomingData) with the same element in it arraySize times. Is this what you wanted?

I would think you would want something like {31, arraySize, Byte 1, Byte 2...Byte ArraySize}

case 0x1F: //Recieve Data Array from LabVIEW

for (int i = 2; i < arraySize/2; i++)

{

incomingData[i-2] = (command[i*2]<<8 | command[i*2+1]);  //place data into array called incomingData

}

Since the serial port is being used for communication with LabVIEW it is a little hard to get debug output when you are testing new cases. I would suggest either writing a command to send the array back to LabVIEW or testing your code in a separate sketch where you can just write the data out to the Serial Port and view it from the terminal

Kevin Fort
Principal Software Engineer
NI
0 Kudos
Message 18 of 46
(3,509 Views)

Kevin,
The byte 1 and byte 2 are arbitrary based on the element that is being
sent. However, the data array input to the send/recieve is always 4
elements.
In LV I have a for loop changing Byte 1 and Byte 2 based on the number.

I will work on moving the program to just the arduino code for debugging purposes.

Thank you for your help.

CLD Certified 2014
0 Kudos
Message 19 of 46
(3,509 Views)

Kevin,

The arduino can't seem to store the data as I thought.

Here is a second option I thought:

I could send a comma separated string array to the serial port from labview. (1000,2000,400,500,2000,30000)

I could listen to the serial port from the arduino and parse the string and build an array.

Something like this although I do not know how to place it into an array.

int incomingByte = -1; // incoming serial data

void setup() {

Serial.begin(9600); // opens serial port, sets data rate to 9600 bps

}

void loop() {

  // send data only when you receive data:

  if (Serial.available() > 0) {

    // read the incoming byte:

    incomingByte = Serial.read();

    incomingByte = incomingByte - '0';

    while(incomingByte <= 9 && incomingByte >= 0){

      // say what you got:

      Serial.print(incomingByte, DEC);

      break;

    }

    if(incomingByte == -4){

      // if comma recieved, then end of integer value

      Serial.print(",");

    }

  }

}

CLD Certified 2014
0 Kudos
Message 20 of 46
(3,509 Views)