鸿蒙大疆无人机,鸿蒙HarmonyOS 结合 大疆Tello TT无人机 开启智能家居物联网新模式...

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74#include

#include

#include

#include

#include "udp_client_test.h"

#include "net_common.h"

static char response[128] = "";

void UdpClientTest(const char* host, unsigned short port)

{

ssize_t retval = 0;

int sockfd = socket(AF_INET, SOCK_DGRAM, 0); // UDP socket

struct sockaddr_in toAddr = {0};

toAddr.sin_family = AF_INET;

toAddr.sin_port = htons(port); // 端口号,从主机字节序转为网络字节序

if (inet_pton(AF_INET, host, &toAddr.sin_addr) <= 0) { // 将主机IP地址从“点分十进制”字符串 转化为 标准格式(32位整数)

printf("inet_pton faiLED!\r\n");

goto do_cleanup;

}

// UDP socket 是 “无连接的” ,因此每次发送都必须先指定目标主机和端口,主机可以是多播地址

//从这里开始给无人机发送SDK中的指令,控制无人机

retval = sendto(sockfd, "command", sizeof("command"), 0, (struct sockaddr *)&toAddr, sizeof(toAddr));

printf("send UDP message {%s} %ld done!\r\n","command", retval);

osDelay(500);

retval = sendto(sockfd, "takeoff", sizeof("takeoff"), 0, (struct sockaddr *)&toAddr, sizeof(toAddr));

printf("send UDP message {%s} %ld done!\r\n","takeoff", retval);

osDelay(800);

retval = sendto(sockfd, "up 50", sizeof("up 50"), 0, (struct sockaddr *)&toAddr, sizeof(toAddr));

printf("send UDP message {%s} %ld done!\r\n","up 50", retval);

osDelay(500);

retval = sendto(sockfd, "flip r", sizeof("flip r"), 0, (struct sockaddr *)&toAddr, sizeof(toAddr));

printf("send UDP message {%s} %ld done!\r\n","flip r", retval);

osDelay(800);

retval = sendto(sockfd, "cw 360", sizeof("cw 360"), 0, (struct sockaddr *)&toAddr, sizeof(toAddr));

printf("send UDP message {%s} %ld done!\r\n","cw 360", retval);

osDelay(800);

retval = sendto(sockfd, "ccw 360", sizeof("ccw 360"), 0, (struct sockaddr *)&toAddr, sizeof(toAddr));

printf("send UDP message {%s} %ld done!\r\n","ccw 360", retval);

osDelay(500);

retval = sendto(sockfd, "flip r", sizeof("flip l"), 0, (struct sockaddr *)&toAddr, sizeof(toAddr));

printf("send UDP message {%s} %ld done!\r\n","flip r", retval);

osDelay(800);

retval = sendto(sockfd, "left 80", sizeof("left 80"), 0, (struct sockaddr *)&toAddr, sizeof(toAddr));

printf("send UDP message {%s} %ld done!\r\n","left 80", retval);

osDelay(500);

retval = sendto(sockfd, "right 80", sizeof("right 80"), 0, (struct sockaddr *)&toAddr, sizeof(toAddr));

printf("send UDP message {%s} %ld done!\r\n","right 80", retval);

osDelay(500);

retval = sendto(sockfd, "land", sizeof("land"), 0, (struct sockaddr *)&toAddr, sizeof(toAddr));

printf("send UDP message {%s} %ld done!\r\n","land", retval);

struct sockaddr_in fromAddr = {0};

socklen_t fromLen = sizeof(fromAddr);

// UDP socket 是 “无连接的” ,因此每次接收时前并不知道消息来自何处,通过 fromAddr 参数可以得到发送方的信息(主机、端口号)

retval = recvfrom(sockfd, &response, sizeof(response), 0, (struct sockaddr *)&fromAddr, &fromLen);

if (retval <= 0) {

printf("recvfrom failed or abort, %ld, %d!\r\n", retval, errno);

goto do_cleanup;

}

response[retval] = '\0';

printf("recv UDP message {%s} %ld done!\r\n", response, retval);

printf("peer info: ipaddr = %s, port = %d\r\n", inet_ntoa(fromAddr.sin_addr), ntohs(fromAddr.sin_port));

do_cleanup:

printf("do_cleanup...\r\n");

close(sockfd);

}

你可能感兴趣的:(鸿蒙大疆无人机)