Troubles with UDP send

Hi all,

I try to build up the udp communication between DK-S7G2 and PC, 

now I could receive packet from PC udp with 

nx_udp_socket_receive(&udp_socket, &packet_ptr, NX_WAIT_FOREVER);

but when I try to send packet, I always received the return value NX_PTR_ERROR

no matter use the nx_udp_socket_send function or nxd_udp_socket_send function.

What's the actual meaning for Invalid socket pointer? and what's the common way to solve this problem?

I've atteched  .rar file for the related netx_thread_entry.c as below.

netx_thread_entry.rar

  • Hello Feng,

    NX_PTR_ERROR means that either &udp_socket or &packet_ptr are not valid pointers. Before you call the function, make sure that packet_ptr is POINTER to existing NX_PACKET struct, i.e. NX_PACKET packet; NX_PACKET * packet_ptr = &packet;

    I can't investigate the thread as the one you posted is for pdc, not netx.

    Regards

  • In reply to Karol:

    Thanks@Karol,

    Related sentence in my codes are described as below

    NX_PACKET               packet_ptr;

    ...

    #define     SLAVE_IP_ADDRESS       IP_ADDRESS(192,168,1,2)

    ...

    /* Enable UDP traffic. */
    status = nx_udp_enable(&ip_0);

    /* Check for UDP enable errors. */
    if (status)
    error_counter++;

    /* Create UDP socket. */

    status = nx_udp_socket_create(&ip_0,&udp_socket,"UDP Socket 0", NX_IP_NORMAL, NX_FRAGMENT_OKAY,0x80,30);

    /* Check for UDP socket create errors. */
    if (status)
    error_counter++;

    /* Bind the previously created UDP socket to port 11000 on the
    previously created IP instance. If the port is already bound,
    wait for 300 timer ticks before giving up. */
    status = nx_udp_socket_bind(&udp_socket, 11000, 300);
    /* If status is NX_SUCCESS, the UDP socket is now bound to
    port 11000.*/

    /* Check for UDP bind errors. */
    if (status)
    error_counter++;

    /* Send a packet to the UDP server at server_address on port 11000. */
    slave_address.nxd_ip_version = NX_IP_VERSION_V4;
    slave_address.nxd_ip_address.v4 = SLAVE_IP_ADDRESS;
    status = nxd_udp_socket_send(&udp_socket, packet_ptr, &slave_address, 11000);

    /* Check for UDP send errors. */
    if (status)
    error_counter++;

    And function  nxd_udp_socket_send is defined in nx_api.h as

    #define nxd_udp_socket_send(s,p,i,t)            _nxde_udp_socket_send(s,&p,i,t)

    for detail, I re-upload a new attachment for netx_thread_entry, if convenient, please kindly investigate it...

    6443.netx_thread_entry.rar

  • In reply to feng:

    Hello feng,

    The third argument you're passing is wrong. You shouldn't pass an IP instance struct but just the IP address in ULONG format (just like you defined SLAVE_IP_ADDRESS). Do not rely on the header definition as the _nxe function takes pointer to a pointer as an argument, and hence additional & is required in the #define.

    Try replacing NX_PACKET packet_ptr with this:

    static NX_PACKET packet;

    NX_PACKET * packet_ptr = &packet;

    And then pass packet_ptr into the function.

    EDIT:
    Also, I spotted you're mixing nx and nxd API calls. You shouldn't have both (NetX and NetX Duo) in the same project.

    Regards,

    Karol

  • In reply to Karol:

    Hi Karol,

    Thank you for your support : D

    and as what you said,

    1) I change the API calls to nx API only nx_udp_socket_send

    and example for this function provided by NetX Duo User Guide Version5 like this,

    ULONG server_address;

    /* Set the UDP Client IP address. */

    server_address = IP_ADDRESS(1,2,3,5);

    /* Send a packet to the UDP server at server_address on port 12. */

    status = nx_udp_socket_send(&client_socket, packet_ptr,

    server_address, 12);

    /* If status == NX_SUCCESS, the application successfully transmitted

    the packet out the UDP socket to its peer. */

    2) Replace NX_PACKET packet_ptr to a pointer to pointer

    details are described as below,

    /************************************************************************/

    ...

    static NX_PACKET        packet_ptr;

    NX_PACKET *packet_ptr_ptr = &packet_ptr;

    ....

    ULONG slave_address = IP_ADDRESS(192,168,1,2);

    ...

    ...

    /* Send a packet to the UDP server at server_address on port 11000. */

    status = nx_udp_socket_send(&udp_socket, packet_ptr_ptr, slave_address, 11000);

    /* If status == NX_SUCCESS, the application successfully transmitted

    the packet out the UDP socket to its peer. */

    /* Check for UDP enable errors.  */

    if (status)

    error_counter++;

    ...

    ...

    /************************************************************************/

    But unfortunately, when I run this program, I still get status value as NX_PTR_ERROR (0x07) Invalid socket pointer...

    Is there any necessary step I missed? Or what should I do next to fix this problem?

    (Here is the attached new codes for detail...)

    5808.netx_thread_entry.rar

  • In reply to Karol:

    Hi Karol,

    Finally, I find out the reason why the value 0x07 (NX_PTR_ERROR : Invalid socket pointer) is returned when I use nx_udp_socket_send function,

    I forget to add nx_udp_socket_checksum_enable function before the udp receive and send action...

    And after inserting the function, my udp program works normally....

    Thanks for your strong support :) @Karol

    ...

    Best Regards,

    feng

  • In reply to feng:

    Hello Karol, feng
    Can you provide api call flow for udp communication? I have now set up a WiFi connection, intending to use my microcontroller as a server, and then send information to the connected device, but I have limited level. thank you very much
    best regards
    yyk
  • In reply to yyk:

    Hello yyk,

    the UDP protocol is described in details in the NetX User Guide:

    In short, the required API functions on the server side are:

    • nx_udp_enable
    • nx_udp_socket_create
    • nx_udp_socket_bind
    • nx_udp_socket_send

    Best regards,
    anper