当前位置:网站首页>W801 / w800 WiFi socket development (I) - UDP
W801 / w800 WiFi socket development (I) - UDP
2022-04-23 01:39:00 【Mr. Zhao】
This is the catalogue
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
WIFI Series articles :
W801/W800-wifi-socket Development ( Two )-UDP Bluetooth control wifi Connect
Write it at the front :
I'm not going to revise the official documents , I will directly call the corresponding api, Because officials use a lot of callbacks and message queues , One link to another , It takes too much time to change the phone bill , It's not necessary .
One 、 Description of project
^^^^ Program function : Use onboard WIFI Connect the server on the computer side (UDP agreement , Use the network debugging assistant to simulate ), To transmit data . The code of this article can refer to :W801/W800-wifi-socket Development ( Two )-UDP Bluetooth control wifi Connect
^^^^ Starting from this article , Onboard will begin wifi Use . Make complaints about writing before you write it. , Development W80X It's official SDK, Function by function , It's true. It's a little uncomfortable ....
Two 、socket-udp official SDK carding
^^^^ This section is mainly used to sort out the official UDP Programming idea of connection , This is a function of a function to find out ah , It's really hard . I hope the watchman will use your little hand to make a fortune , Give me a little attention , Point a praise , Click a collection .
Be careful : It's a little windy here , If the novice may not be easy to understand , Watch it yourself a few more times . The program design will be put in the third chapter .
1、 Connect the router
^^^^ open wm_connect_net_demo.c
file ,demo in wifi The connection function of is under this file .
As shown in the figure above ,
demo_connect_net
The function entry parameter is wifi Name and password .
Take a chestnut :
demo_connect_net("yyds","1234567890");
// Be careful , There must be a delay , Otherwise, there will be problems with the connection .....
tls_os_time_delay(2000);
^^^ After execution, the development board is automatically connected to wifi 了 , At this time, the serial port will print :
Indicates that you have successfully joined the router , It can be seen that the router assigns to the development board IP:192.168.1.74
.
2、 Connect UDP
^^^^ open wm_udp_demo.c
file ,udp_demp The connection function of is under this file .
socket_udp_demo()'
Function parameters are represented separately Pattern ( Unicast and multicast ), port and IP. This article connects to the computer server , Therefore, the configuration is as follows :
socket_udp_demo(1,10086,"192.168.1.87");
Above IP It's my computer IP, Port is my custom port , It can be modified according to the actual situation . Next, we will deal with the execution idea of the function in detail .
^^^^socket_udp_demo()'
The first half of the function is all initialization related , Mainly look at the following structure :demo_udp
The structure is as follows , It can be seen that the structure contains connection related variables ,*sock_rx
and *sock_tx
, It is the receiving and sending pointer that needs to be used later , This means that subsequent transmissions can be transmitted directly 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 .
^^^^ Next, let's look directly at the following two tasks , Be careful demo_udp
This structure is passed to the two tasks .
^^^^ The first is the configuration and transmission related , The second is to receive data .
2.1、 First of all to enter demo_udp_task()'
function :( Function udp
It's from the last function , And the above demo_udp
Equivalent )
^^^^ Judge 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);
^^^^ Go straight into udp_net_status_changed_event()'
function , Official use of a demo_udp
The message queue sends the status of the function currently to be executed , and udp
Equivalent .
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;
}
}
^^^^ sign out udp_net_status_changed_event()'
Function to continue viewing demo_udp_task()'
function . A dead cycle , receive udp
The news of , there switch Medium msg From the previously mentioned upd
or demo_udp
( The two are equivalent ).
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;
}
}
^^^^ Sort out switch Execution order of : First, execute create_udp_socket_demo()'
, Create connection , You don't have to look at this function anymore , It will be automatically created successfully . then , You can enter the sending program .demo A separate sending function is written in :
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;
}
This function only specifies the length of the transmission , The content is not assigned ,tls_os_queue_send()'
Function to send a message DEMO_MSG_UART_RECEIVE_DATA
Parameters to demo_udp_task()'
Code for loop ,tls_os_queue_receive()
Function receives the value of the message queue , and switch perform . stay case DEMO_MSG_UART_RECEIVE_DATA:
Is the real occurrence code , The content sent is as follows :
memset(udp->sock_tx, 'u', len);
The appeal code is to fill all the contents sent with ’u’, Therefore, modify here directly udp->sock_tx Content , You can pass socket send data . But when actually sending, there is no assignment here .....
**2.2、** Next look at demo_udp_recv_task()'
function , This function has nothing to say , The received data will be automatically put into udp->sock_rx
in .
ret = recvfrom(udp->socket_num, udp->sock_rx, DEMO_UDP_BUF_SIZE,
0, (struct sockaddr *)&pin, &addrlen);
3、 ... and 、 Project design
^^^^ This section will deploy your own project , Read and send data .
3.1、 Modify sending and receiving codes
^^^^ Must be modified : , stay wm_udp_demo.c find demo_udp_task()'
function , take case DEMO_MSG_UART_RECEIVE_DATA:
Medium memset Screen out :
The code of this article must be masked
//memset(udp->sock_tx, 'u', len);
// The following codes can be added or not , Mainly print the data sent
// Add in 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;
^^^^ You don't have to modify : , stay wm_udp_demo.c find demo_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、 Encapsulated data
^^^^ stay wm_udp_demp.c
Of documents Last Add code :
//add by zxx start
void udp_send_data_self(u8 *data,int data_len)
{
// take data Copy all the contents of 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 statement
^^^^ stay wm_demp_console.h
Of documents 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、 The 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");
// It has to be
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);
}
}
Four 、 test
1、 Simulate server
^^^^ Open the network debugging assistant , Configure as follows ,IP Configure according to your actual situation .
2、 Download the program to the development board
^^^^ After the delay time , The development board and the server send data to each other .
^^^^ Development board
^^^^ The server .
版权声明
本文为[Mr. Zhao]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230136121288.html
边栏推荐
- Gbase 8s Group by 功能介绍
- Solve the problem when installing MySQL
- Detonate the bomb (DFS)
- ai2022新功能,illustrator 2022 新功能介绍
- Text justify, orientation, combine text attributes
- Google developer tool preserve log
- gin -get请求的小示例2-Handle处理post请求
- Unity combines itextsharp to generate PDF preparation DLL
- 清研环境深交所上市:年营收1.8亿 市值41亿
- 01 knapsack problem - and deformation problem
猜你喜欢
DFS parity pruning
engine. Post() handles post requests
全排列(DFS和next_permutation解法)
NR polar code VII - SCL (successful cancellation list coding)
JSP基础知识总结
Technology cloud report: cloud computing has entered the "second half". Where is the way out for domestic cloud?
CVPR | 2022 | expressed by transformer learning multiple hypotheses! A new framework for 3D human pose estimation!
Redis implements distributed locks
安装mysql出问题求解决
科技云报道:云计算进入“下半场”,国产云的出路在哪儿?
随机推荐
Abused "architect"!
Google developer tool preserve log
Gbase 8s Group by 功能介绍
Realize the function of progress bar through layerdrawable
Gbase 8s shared memory segment deletion
计蒜客(踏青)(染色块问题的DFS和BFS解法)
W801/W800-wifi-socket开发(二)-UDP蓝牙控制wifi连接
In the second half of the smart watch, opportunities and challenges coexist
CVPR | 2022 | expressed by transformer learning multiple hypotheses! A new framework for 3D human pose estimation!
Introduction and management of gbase 8s database log
关于C4D动画如何导入Lumion
Introduction to gbase 8s checkpoint
Completely uninstall antidote 10? What if the antidote uninstall is not clean?
Gbase 8s存儲結構簡介及空間管理
计蒜客家谱(dfs求直系后代数)
Solve the problem when installing MySQL
Planning garlic guest (outing) (DFS and BFS solution of dyeing block problem)
“自虐神器”一夜爆火:用手柄控制自己的脸,代码自取,后果自负
W801/W800/W806唯一ID/CPUID/FLASHID
Chapter 6 uses Matplotlib to draw thermodynamic diagram