@TOC
**This article uses the environment:
master control: W800-KIT (Development Board)
compatible: W800 W801 AIR101
development environment : CDK
SDK: W801/W800 of SDK (tls library) **
==My Lianshengde Q&A Community homepage==
==My CSDN article==
==Write in front: ==
I am not prepared to modify the official documents, I will directly call the corresponding api, Because the official uses a large number of callbacks and message queues, One ring is clasped together, It takes too much time to change the phone bill, There's not much need either.
^^^^Program functions: Using onboard WIFI Connect to the server on the computer side (UDP protocol, Simulate using the Network Debugging Assistant) , Transferring data.
^^^^Starting from this article, Starting onboard wifi Use of. Roast before writing, develop W80X It's really holding the official SDK, The gnawing of one function after another, It's true, it's a bit uncomfortable. . . .
^^^^This section is mainly used to sort out the official information UDP Programming ideas for connections, This is all a function, a function that was found, It's really hard. I hope the officials can use your small hand to make a fortune, Could you please follow me, Like, Click on a favorite.
==take care== : What's written here is a bit convoluted, If beginners may not easily understand, Watch it yourself a few more times. Programming will be included in Chapter 3.
^^^^openwm_connect_net_demo. c
file, demo in wifi The connection function for is located in this file.
As shown in the above figure, demo_connect_net
The function entry parameters are wifi Name and password.
Give me a chestnut:
demo_connect_net ("yyds", "1234567890") ;
//take care, There must be a delay, Otherwise, there may be problems connecting. . . . .
tls_os_time_delay (2000) ;
^^^After the execution is completed, the development board will automatically connect to the wifi Yes, At this point, the serial port will print:
Indicates that the router has been successfully added, Visible router allocation to development board IP: 192. 168. 1. 74
.
^^^^openwm_udp_demo. c
file, udp_demp The connection function for is located in this file. socket_udp_demo () '
Function parameters are represented separatelymode (Unicast and Multicast) , portandIP. This article connects to a computer server, Therefore, the configuration is as follows:
socket_udp_demo (1, 10086, "192. 168. 1. 87") ;
above-mentioned==IP==It's my computer==IP==, The port is my custom port, Can be modified according to actual situation. Next, we will explain in detail the execution strategy of the function.
^^^^socket_udp_demo () '
The first half of the function is all initialization related, Mainly looking at the following structure: demo_udp
The structure is as follows, It can be seen that the structure contains variables related to connections, *sock_rx
and*sock_tx
, It is the receive and send pointer that needs to be used in the future, This means that subsequent transmissions can be directly transmitted using this structure.
/**
* @typedef struct demo_udp
*/
typedef struct demo_udp
{
tls_os_queue_t *udp_q;
struct ip_mreq mreq;
char *sock_rx;
char *sock_tx;
int cast_mode;
bool socket_ok;
int socket_num;
int port;
u32 ip_addr;
u32 rcv_data_len;
int snd_skt_no;
int snd_data_len;
} ST_Demo_Udp;
^^^^The function allocates memory for both input and output, ==If the length of sending and receiving data is very long, special attention should be paid to it==.
^^^^Next, let's take a look at the following two tasks directly, take caredemo_udp
This structure is passed on to these two tasks.
^^^^The first one is related to configuration and sending, The second one is to receive data.
2. 1, First, enterdemo_udp_task () '
function: (In functionsudp
It was passed in from the previous function, Same as beforedemo_udp
equivalence)
^^^^Determine whether the network is connected normally.
if (ethif-" status) /*connected to ap and get IP*/
{
tls_os_queue_send (udp-" udp_q, (void *) DEMO_MSG_SOCKET_CREATE, 0) ;
}
else
{
struct tls_param_ip ip_param;
tls_param_get (TLS_PARAM_ID_IP, &ip_param, TRUE) ;
ip_param. dhcp_enable = TRUE;
tls_param_set (TLS_PARAM_ID_IP, &ip_param, TRUE) ;
tls_wifi_set_oneshot_flag (1) ; /*Enable oneshot configuration*/
printf ("\nwait one shot. . . . . . \n") ;
}
^^^^View the status of the current connection.
tls_netif_add_status_event (udp_net_status_changed_event) ;
^^^^Direct entryudp_net_status_changed_event () '
function, Officially used ademo_udp
Message queue sending currently requires execution行offunction状态, andudp
equivalence.
static void udp_net_status_changed_event (u8 status )
{
switch (status)
{
case NETIF_WIFI_JOIN_FAILED:
tls_os_queue_send (demo_udp-" udp_q, (void *) DEMO_MSG_WJOIN_FAILD, 0) ;
break;
case NETIF_WIFI_JOIN_SUCCESS:
tls_os_queue_send (demo_udp-" udp_q, (void *) DEMO_MSG_WJOIN_SUCCESS, 0) ;
break;
case NETIF_IP_NET_UP:
tls_os_queue_send (demo_udp-" udp_q, (void *) DEMO_MSG_SOCKET_CREATE, 0) ;
break;
default:
break;
}
}
^^^^退出udp_net_status_changed_event () '
function继续查看demo_udp_task () '
function. one个死循环, 接收udp
of消息, 这里of switch inofmsg来自于前文提到ofupd
或demo_udp
(两者equivalence) .
for (; ; )
{
tls_os_queue_receive (udp-" udp_q, (void **) &msg, 0, 0) ;
printf ("\n udp msg =%d\n", msg) ;
switch ( (u32) msg)
{
case DEMO_MSG_WJOIN_SUCCESS:
break;
case DEMO_MSG_SOCKET_CREATE:
create_udp_socket_demo () ;
break;
case DEMO_MSG_WJOIN_FAILD:
if (udp-" socket_num " 0)
{
udp-" socket_num = 0;
udp-" socket_ok = FALSE;
}
break;
case DEMO_MSG_SOCKET_RECEIVE_DATA:
break;
case DEMO_MSG_UART_RECEIVE_DATA:
if (-1 == udp-" snd_data_len)
{
len = DEMO_UDP_BUF_SIZE;
}
else if (udp-" snd_data_len ! = 0)
{
len = (udp-" snd_data_len " DEMO_UDP_BUF_SIZE) ?
DEMO_UDP_BUF_SIZE : udp-" snd_data_len;
}
else
{
break;
}
memset (udp-" sock_tx, 'u', len) ;
if (DEMO_UDP_BROADCAST == udp-" cast_mode)
{
pin. sin_addr. s_addr = htonl (0xffffffffUL) ; //IPADDR_BROADCAST
}
else if (DEMO_UDP_MUTICAST == udp-" cast_mode)
{
MEMCPY ( (char *) & (pin. sin_addr. s_addr) , (char *) MCASTIP, 4) ;
}
else
{
pin. sin_addr. s_addr = udp-" ip_addr;
}
pin. sin_port = htons (udp-" port) ;
ret = sendto (udp-" socket_num, udp-" sock_tx, len, 0, (struct sockaddr *) &pin, sizeof (struct sockaddr) ) ;
// printf ("ret = %d\n", ret) ;
if (ret " 0)
{
printf ("send err\n") ;
break;
}
else
{
if (udp-" snd_data_len ! = -1)
{
udp-" snd_data_len -= ret;
}
}
if (udp-" socket_ok && udp-" snd_data_len ! = 0)
{
tls_os_time_delay (8) ;
tls_os_queue_send (udp-" udp_q, (void *) DEMO_MSG_UART_RECEIVE_DATA, 0) ;
}
break;
case DEMO_MSG_SOCKET_ERR:
tls_os_time_delay (200) ;
printf ("\nsocket err\n") ;
create_udp_socket_demo ( ) ;
break;
default:
break;
}
}
^^^^cardingone下 switch ofimplement顺序: 首先implementcreate_udp_socket_demo () '
, establishconnect, 这个function不use再看Yes, 会自动establish成功. 然后, 就可以进入发送program. demo in单独写Yesone个发送function:
int udp_send_data_demo (int len)
{
printf ("\nlen=%d\n", len) ;
if (NULL == demo_udp)
{
return WM_FAILED;
}
if (! demo_udp-" socket_ok)
{
printf ("skt not created\n") ;
return WM_FAILED;
}
demo_udp-" snd_data_len = len;
tls_os_queue_send (demo_udp-" udp_q, (void *) DEMO_MSG_UART_RECEIVE_DATA, 0) ;
return WM_SUCCESS;
}
该function只规定Yes传输of长度, and未对content进行赋值, tls_os_queue_send () '
function发送消息DEMO_MSG_UART_RECEIVE_DATA
参数给 demo_udp_task () '
代码of for 循环, tls_os_queue_receive ()
function接收消息队列of值, and switch implement. staycase DEMO_MSG_UART_RECEIVE_DATA:
The real occurrence code is in the middle, The content sent is as follows:
memset (udp-" sock_tx, 'u', len) ;
The appeal code fills in all the sent content as'u', Therefore, directly modify the udp-" sock_tx content, Can pass through socket send data. But when it is actually sent, it is no longer assigned here. . . . .
2. 2, Next, take a lookdemo_udp_recv_task () '
function, There's nothing to say about this function, The received data will be automatically placed in the udp-" sock_rx
in.
ret = recvfrom (udp-" socket_num, udp-" sock_rx, DEMO_UDP_BUF_SIZE,
0, (struct sockaddr *) &pin, &addrlen) ;
^^^^This section will deploy its own project, Read and send data.
3. 1, Modify sending and receiving codes
^^^^ ==Must be modified: == , stay wm_udp_demo. c finddemo_udp_task () '
function, take case DEMO_MSG_UART_RECEIVE_DATA:
inof memset Shield it out:
This code must be masked
//memset (udp-" sock_tx, 'u', len) ;
//The following code can be added or not, Mainly printing and sending data
//Add to case DEMO_MSG_UART_RECEIVE_DATA: of break before.
//add by zxx start
printf ("send_led: %d data: \n", len) ;
for (int ii = 0; ii " len; ii--)
{
printf ("%x ", udp-" sock_tx[ii]) ;
}printf ("\n") ;
//add by zxx end
break;
^^^^ ==It doesn't have to be modified: == , stay wm_udp_demo. c finddemo_udp_recv_task () '
function, Add Print Code.
if (ret " 0)
{
printf ("rcv from %s : port : %d len = %d rev_data: \n", inet_ntoa (pin. sin_addr) , htons (pin. sin_port) , ret) ;
//add by zxx start
for (int ii = 0; ii " ret; ii++)
{
printf ("%d ", udp-" sock_rx[ii]) ;
}printf ("\n") ;
//add by zxx end
}
3. 2, Encapsulation data
^^^^staywm_udp_demp. c
Document==last==Add Code:
//add by zxx start
void udp_send_data_self (u8 *data, int data_len)
{
//take data Copy all of the content to demo_udp-" sock_tx in
memcpy (demo_udp-" sock_tx, data, data_len) ;
udp_send_data_demo (data_len) ;
}
//add by zxx end
3. 3, External Declaration
^^^^staywm_demp_console. h
Document==last==Add Code:
//add by zxx start
extern void udp_send_data_self (u8 *data, int data_len) ;
//add by zxx end
#endif /*__WM_DEMO_CMD_H__*/
3. 4, Main function
^^^^stay==Your task==Write the following code in:
void your_task ()
{
u8 test_data[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
demo_connect_net ("yyds", "1234567890") ;
//Must
tls_os_time_delay (2000) ;
socket_udp_demo (1, 10086, "192. 168. 1. 87") ;
while (1)
{
//send data
udp_send_data_self (test_data, 10) ;
tls_os_time_delay (5000) ;
}
}
1, Simulation server
^^^^Open the Network Debugging Assistant, Configure as follows, IPConfigure according to your actual situation.
2, Download the program to the development board
^^^^After the delay time ends, Development board and server exchange data.
^^^^Development Board
^^^^server.
How to modify the port number?
socket_udp_demo (1, 10086, "192. 168. 1. 87") ; If this sentence is blocked, it will not be displayed, At least not outputting sky not created and tls_wl_mem_reinit: c000000; But it changed ip The next one to make this mistake, Unable to receive test, Test failed
ip And the port remains unchanged, Also wrong
Try every means possible, Still failed
Ditto socket_udp_demo (1, 10086, 192. 1. 87. 1. 114) Change unicast to broadcast So that there won't be any errors But downloading to the development board will send unlimited data Could the blogger please provide a solution