Question

I just want to send an array adc_array=[w, x, y, z] from client to server. Below is the client side code whereas my server is in python which accepts json only. I get no error when i compile the code however get 2 warnings : 1- warning: pointer targets in passing argument 2 of 'UDPWrite' differ in signedness. 2- warning: no newline at end of file.

But at the server side, i am not able to receive the whole array, instead i just get the first character of the array i.e. [ .

I am new to C programming. I would really appreciate any help.

// Main function
void FlyportTask()
{
// Flyport connects to default network
WFConnect(WF_DEFAULT);
while(WFGetStat() != CONNECTED);
vTaskDelay(25);
UARTWrite(1,"Flyport Wi-fi connected...hello world!\r\n");

BOOL UdpSocketOpenRequest=TRUE;
BYTE UdpSocket=0;


// openinging UDP socket
if (UdpSocketOpenRequest) //open socket
{
    UdpSocketOpenRequest=FALSE;
    if (UdpSocket!=0) //if this is not equals to zero
    {
        UDPClientClose(UdpSocket);
    }   
    UARTWrite(1,"OpenSocket\r\n");
    UdpSocket= UDPClientOpen("10.0.0.106", "8000"); //Client socket opening 
}


while(1)
{
    //defining pointer
    int *array_pointer;
    int adc_array[4];
    int j;
    char buf[10];       //buffer to print 

    // I have made a separate function to get adc values which returns the pointer to the array.
    array_pointer = get_adcval();

    UARTWrite (1, "ADC Array\r\n");

    for (j = 0; j < 4; j++)
    {
        adc_array[j] = *(array_pointer + j);
        sprintf (buf, "%d", adc_array[j]);
        UARTWrite (1, buf);
        UARTWrite (1, "\n");
    }


    //if UDP socket is open, send the data 
    if ((UdpSocket!=0))
    {
        // defining pointer of serial_out
        char *s_out;
        int size;

        // creating a JSON array from adc_array with 4 elements
        cJSON * int_array = cJSON_CreateIntArray(adc_array,4);

        // Serializing the array
        s_out = cJSON_Print(int_array);

        //Writing to the serial output/monitor
        UARTWrite(1, "\r\narray to be sent\r\n");
        UARTWrite(1, s_out);
        UARTWrite(1,"\r\n");

                    // Assume adc_array=[1021, 1022, 1023, 1024]
                    // I get output [1021, 1022, 1023, 1024]

        //compose message
        size = strlen(s_out);
        UDPWrite (UdpSocket, s_out, size);

                    // at the server side, i just receive only first character i.e. [

        /*to free the memory */
        free(s_out);

    }



    //
    // remember to add delay vTaskDelay(50) 50ms
    //remember to close the socket


}


}
Was it helpful?

Solution

You didn't allocated memory for s_out. even if it is printing correct result on UART but still it can be overwritten by any of the UARTWrite functions or strlen() function in the next lines. If it is overwritten then the "size" variable will get the number of bytes starting from the first byte to first null character in the memory (this is how strlen() functions). hence the "size" value can be totally random. it can be 0 or 1 or 1000. if the size is not correct then you will receive only "size" number of bytes. In your case it is possible that size is one. try printing size before UDPWrite. fix this problem by adding a malloc call before serializing the array.

If it doesn't work either then check your receiver side. is your receiver working fine if you send some dummy data from a tested python client (or any other tested or reliable client)? if no then there is some problem with your receiver.

OTHER TIPS

Print out what strlen(s_out) returns, also print out the return value of UDPWrite ( I assume that like any write function this will be returning the size of the data which is written to the socket).

By reading the function names I presume you are using UDP transmission which is unreliable.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top