Compose string


#1

Send data via WIP plugin to server.

ascii response[255];

u8 data[6];

// fill sample date value
data[0] = 0x01;
data[1] = 0x02;
data[2] = 0x03;
data[3] = 0x04;
data[4] = 0x05;
data[5] = 0x06;

wm_sprintf(response, "%s%s%s", "rq", data, "\n");
wip_write(socket, response, wm_strlen(response));

server recieved: “rq\n”
\n is are 0x13 char

where my data?


#2

Hi,

where is your string end in data? strings need to be terminated with a null character in C, and this is required for use of %s. Your lucky that the program didn’t crash, because it could very well be that the memory contents after data doesn’t contain a 0x00 character for a length greater than 255 characters, so you would copy into the RAM after response variable and overwriting critical parts.

Also, I would suggest not to use wm_sprintf() just to concatenate strings. Use wm_strcpy() and wm_strcat() instead! I never had problems with that, where wm_sprintf() caused some trouble to me before.

Best Regards,
Jan


#3

Not only that… the “data” that is being sent are all non-printable characters.

If you look at the actual data you receive, you may see those additional bytes but they will not be printed as they are now.


#4

Help again:

ascii response[255];

u8 data[6];

// fill sample date value 
data[0] = 0x01; 
data[1] = 0x02; 
data[2] = 0x03; 
data[3] = 0x04; 
data[4] = 0x05; 
data[5] = 0x06;

wm_strcpy(response, "rq");  
wm_strcat(response, data);
wm_strcat(response, "\n");
wip_write(socket, response, wm_strlen(response));

server recieved “0x48 0x47 0xffffffbc 0x0a”
but where by data array?


#5

You should start by adding a zero at the end of data, so that the string is properly terminated.

ascii response[255];

u8 data[6+1];

// fill sample date value 
data[0] = 0x01; 
data[1] = 0x02; 
data[2] = 0x03; 
data[3] = 0x04; 
data[4] = 0x05; 
data[5] = 0x06;
data[6] = 0;

wm_strcpy(response, "rq");  
wm_strcat(response, data);
wm_strcat(response, "\n");
wip_write(socket, response, wm_strlen(response));

That’s really strange. Can you send a copy of response to UART1 or check it with TRACE() ? How about Debug mode, can you check if the string is built correctly?
How about avoiding wm_strcat():

ascii *response;

response="rq\x01\x02\x03\x04\x05\x06\n";
wip_write(socket, response, wm_strlen(response));

If you want to fix the problem, you need first to find exactly the point where it is: the first wm_strcat(), or the second wm_strcat(), or the WIP library, or the receiving server.


#6

I don’t believe for a moment that that is exactly what the server is receiving byte for byte.
Are you sure that your server side program is actually written correctly when it gives you this output?


#7

Hi usa,

where are these variable declarations in the code? are those local variables in a function or global variables?

Could you try to make them static like this

static ascii response[255];
static u8 data[6];

and see what happens?

Best Regards,
Jan


#8

Response is global static variable, data is local variable.

2milan_va thanks, will try.