1

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


}


}
4

2 回答 2

0

打印出 strlen(s_out) 返回的内容,还打印出 UDPWrite 的返回值(我假设像任何写入函数一样,这将返回写入套接字的数据的大小)。

通过阅读函数名称,我认为您正在使用不可靠的 UDP 传输。

于 2013-09-23T23:43:22.817 回答
0

您没有为 s_out 分配内存。即使它在 UART 上打印正确的结果,但它仍然可以被下一行中的任何 UARTWrite 函数或 strlen() 函数覆盖。如果它被覆盖,那么“size”变量将获取内存中从第一个字节到第一个空字符的字节数(这就是 strlen() 的作用方式)。因此“大小”值可以是完全随机的。它可以是 0 或 1 或 1000。如果大小不正确,那么您将只收到“大小”字节数。在您的情况下,大小可能是 1。尝试在 UDPWrite 之前打印大小。通过在序列化数组之前添加 malloc 调用来解决此问题。

如果它也不起作用,请检查您的接收器端。如果您从经过测试的 python 客户端(或任何其他经过测试或可靠的客户端)发送一些虚拟数据,您的接收器工作正常吗?如果不是,那么您的接收器有问题。

于 2013-09-23T23:08:58.333 回答