-->
Page 1 of 2

Using values from a UPD pkt as array address - help needed

PostPosted: Fri Jul 15, 2016 6:10 pm
by Barnabybear
Hi, if you can help with this I would appreciate it.
I receive a UDP packet as follows:
Code: Select allint packetSize = Udp.parsePacket();
if (packetSize) {
int len = Udp.read(packetBuffer, 31);
 


Bytes 17 to 24 contain a file name that I need to use to address an array. I extract the file name as follows (this is where I think I go wrong as I create a second array):
Code: Select allfor (int i = 17; i < 25; i++) {
NAME [i -17] = (packetBuffer[i]);
}


The problem comes when I come to use ‘NAME’, I want to use it to specify which array to read data from.
Arrays in this format: byte 1234 [ ] = { d, a, t, a, 0,};
When I try to read a value from the array called ‘NAME’ using the following:
Code: Select allFunction ( NAME [ location in array 1234 ] );

It returns the value of the byte in NAME not the value of the byte in array.
Two examples:
Code: Select allArrays in this format: byte 1234 [ ] = { d, a, t, a, 0,};

Function (1234 [2]); // returns ‘t’ which is what I require.
Function (NAME [2]); returns ‘M’ which is not what I wanted but I understand why.

The project; some wearables that display effects and text dependant on the UDP packet received. Guests at the party will be able to push one of ten buttons that will send a birthday message to the wearables. All the other bits are sorted, I’m just haven’t been able to sort this last bit to link the two up yet and time is getting short. So any help will be greatly appreciated.
I’m using 1.6.5 as it’s been an ongoing project for my wife’s 50th birthday on the 30th of this month and I don’t want to upgrade until after then.
Thanks in advance.

Re: Using values from a UPD pkt as array address - help need

PostPosted: Fri Jul 15, 2016 7:16 pm
by martinayotte
@Barnabybear, where this whole "Function" macros comes from ? :?
I'm not aware of any thing about it.

For your issue, I would simply doing it in plain C/C++ with something like :

Code: Select allchar filename[16];
strcpy(filename, &packetBuffer[17]);

Re: Using values from a UPD pkt as array address - help need

PostPosted: Fri Jul 15, 2016 8:01 pm
by bbx10node
Maybe something like this?

Code: Select allconst byte array1[] = "1234";
const byte array2[] = "data";
byte *array_selector;
int i;

char NAME[16];
strcpy(NAME, &packetBuffer[17]);

if (strcmp(NAME, "one") == 0) {
    array_selector = array1;
}
else if (strcmp(NAME, "two") == 0) {
    array_selector = array2;
}
else {
    array_selector = NULL;
    Serial.println("NAME no match");
}

if (array_selector != NULL) {
    Serial.println(array_selector);
}

Re: Using values from a UPD pkt as array address - help need

PostPosted: Mon Jul 18, 2016 3:50 pm
by Barnabybear
Hi guys, thanks for the posts. I'm just working through some tutorials on pointers / addressing and I'll let you know how it goes.