#include <stdlib.h> // for exit() //
#include <netdb.h>
#include <stdio.h>
#include <string.h> // memset() //
#define DRONE_COMMAND_PORT 5556
#define MAX_MSG 2048
int main(int argc, char *argv[]) {
// declare variables
int sd, rc, flags, nd;
char command[MAX_MSG];
struct sockaddr_in clientAddr;
struct sockaddr_in droneAddr;
struct hostent *h;
struct timespec wait_command;
int seq=0;
char msg[MAX_MSG];
// initialize variables
flags = 0;
wait_command.tv_sec = 0;
wait_command.tv_nsec = 5000000;
// check command line args //
if(argc<2) {
printf("usage : %s <server name/IP>n", argv[0]);
exit(EXIT_FAILURE);
}
// get server IP address (no check if input is IP address or DNS name)
h = gethostbyname(argv[1]);
if(h==NULL) {
printf("%s: unknown host '%s' n", argv[0], argv[1]);
exit(EXIT_FAILURE);
}
// create structure for ardrone address & port
droneAddr.sin_family = h->h_addrtype;
droneAddr.sin_port = htons(DRONE_COMMAND_PORT);
memcpy((char *) &droneAddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length);
// create structure for this client
clientAddr.sin_family = AF_INET;
clientAddr.sin_addr.s_addr = htonl(INADDR_ANY);
clientAddr.sin_port = htons(0);
// socket creation
sd = socket(AF_INET,SOCK_DGRAM,0);
if(sd<0) {
printf("%s: cannot open socket n",argv[0]);
exit(EXIT_FAILURE);
}
// bind client's the port and address
rc = bind(sd, (struct sockaddr *) &clientAddr, sizeof(clientAddr));
if(rc<0) {
printf("%s: cannot bind portn", argv[0]);
exit(EXIT_FAILURE);
}
// send commands
char cmd[MAX_MSG],cmd2[MAX_MSG],cmd3[MAX_MSG],cmd4[MAX_MSG];
int i;
snprintf(cmd,MAX_MSG,"AT*CONFIG= %u,"general:navdata_demo","TRUE"",seq);
printf("%s: %sn",argv[0],cmd);
sendto(sd, cmd, MAX_MSG , flags, (struct sockaddr *) &droneAddr, sizeof(droneAddr));
seq++;
snprintf(cmd2,MAX_MSG,"AT*FTRIM=%u",seq);
printf("%s: %sn",argv[0],cmd2);
sendto(sd, cmd2, MAX_MSG , flags, (struct sockaddr *) &droneAddr, sizeof(droneAddr));
seq++;
snprintf(cmd3,MAX_MSG,"AT*PMODE= %u,2",seq);
printf("%s: %sn",argv[0],cmd3);
sendto(sd, cmd3, MAX_MSG , flags, (struct sockaddr *) &droneAddr, sizeof(droneAddr));
seq++;
char cmd1[MAX_MSG];
printf("%s: sending commands to %dn", argv[0], inet_ntoa(*(struct in_addr *)h->h_addr_list[0]));
for(i=0;i<10;i++)
{
cmd1[strlen(cmd1)-1] = 0x0d; // change last character from new line to carriage return
snprintf(cmd1,MAX_MSG,"AT*REF=%u,290718208",seq);
printf("%s: %sn",argv[0],cmd1);
snprintf(cmd1,MAX_MSG,"AT*REF=%u,290718208",seq+1);
printf("%s: %sn",argv[0],cmd1);
snprintf(cmd1,MAX_MSG,"AT*REF=%u,290718208",seq+2);
printf("%s: %sn",argv[0],cmd1);
snprintf(cmd1,MAX_MSG,"AT*REF=%u,290718208",seq+3);
printf("%s: %sn",argv[0],cmd1);
snprintf(cmd4,MAX_MSG,"AT*COMWDG=%u",seq+4);
printf("%s :%sn",argv[0],cmd4);
rc = sendto(sd, cmd1, MAX_MSG , flags, (struct sockaddr *) &droneAddr, sizeof(droneAddr));
if(rc<0) {
printf("%s: can not send datan",argv[0]);
close(sd);
exit(EXIT_FAILURE);
}
seq=seq+5;
nanosleep(&wait_command , NULL);
}
return 1;
}
嗨,这是我起飞Ar.drone的程序。编译时,我不会遇到任何错误。但是无人机没有撤离。printf语句中的AT命令正在打印。也没有错误。谁能告诉我我程序中是否有任何错误或为什么我的程序不起作用?
预先感谢。
关于Shiksha
我总是发送以下序列:
" at*config = 1,"常规:navdata_demo "," true " r"
" at*ctrl = 0 r"
然后,您想要的任何内容以1。
开始。我认为您缺少AT*ctrl = 0/r
尝试使用
连接()
而不是绑定端口。