34 #include <sys/socket.h> 35 #include <sys/types.h> 36 #include <netinet/in.h> 41 #if (defined __QNX__) | (defined __QNXNTO__) 48 #include <arpa/inet.h> 57 #define BUFFER_LENGTH 2041 // minimum buffer size that can be used with qnx (I don't know why) 61 int main(
int argc,
char* argv[])
64 char help[] =
"--help";
69 float position[6] = {};
70 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
71 struct sockaddr_in gcAddr;
72 struct sockaddr_in locAddr;
76 socklen_t fromlen =
sizeof(gcAddr);
82 unsigned int temp = 0;
85 if ((argc == 2) && (strcmp(argv[1], help) == 0))
88 printf(
"\tUsage:\n\n");
90 printf(
"%s", argv[0]);
91 printf(
" <ip address of QGroundControl>\n");
92 printf(
"\tDefault for localhost: udp-server 127.0.0.1\n\n");
98 strcpy(target_ip,
"127.0.0.1");
101 strcpy(target_ip, argv[1]);
105 memset(&locAddr, 0,
sizeof(locAddr));
106 locAddr.sin_family = AF_INET;
107 locAddr.sin_addr.s_addr = INADDR_ANY;
108 locAddr.sin_port = htons(14551);
111 if (-1 == bind(sock,(
struct sockaddr *)&locAddr,
sizeof(
struct sockaddr)))
113 perror(
"error bind failed");
119 #if (defined __QNX__) | (defined __QNXNTO__) 120 if (fcntl(sock, F_SETFL, O_NONBLOCK | FASYNC) < 0)
122 if (fcntl(sock, F_SETFL, O_NONBLOCK | O_ASYNC) < 0)
126 fprintf(stderr,
"error setting nonblocking: %s\n", strerror(errno));
132 memset(&gcAddr, 0,
sizeof(gcAddr));
133 gcAddr.sin_family = AF_INET;
134 gcAddr.sin_addr.s_addr = inet_addr(target_ip);
135 gcAddr.sin_port = htons(14550);
145 bytes_sent = sendto(sock, buf, len, 0, (
struct sockaddr*)&gcAddr,
sizeof(
struct sockaddr_in));
148 mavlink_msg_sys_status_pack(1, 200, &msg, 0, 0, 0, 500, 11000, -1, -1, 0, 0, 0, 0, 0, 0);
150 bytes_sent = sendto(sock, buf, len, 0, (
struct sockaddr*)&gcAddr,
sizeof (
struct sockaddr_in));
154 position[0], position[1], position[2],
155 position[3], position[4], position[5]);
157 bytes_sent = sendto(sock, buf, len, 0, (
struct sockaddr*)&gcAddr,
sizeof(
struct sockaddr_in));
160 mavlink_msg_attitude_pack(1, 200, &msg,
microsSinceEpoch(), 1.2, 1.7, 3.14, 0.01, 0.02, 0.03);
162 bytes_sent = sendto(sock, buf, len, 0, (
struct sockaddr*)&gcAddr,
sizeof(
struct sockaddr_in));
166 recsize = recvfrom(sock, (
void *)buf,
BUFFER_LENGTH, 0, (
struct sockaddr *)&gcAddr, &fromlen);
173 printf(
"Bytes Received: %d\nDatagram: ", (
int)recsize);
174 for (i = 0; i < recsize; ++i)
177 printf(
"%02x ", (
unsigned char)temp);
181 printf(
"\nReceived packet: SYS: %d, COMP: %d, LEN: %d, MSG ID: %d\n", msg.
sysid, msg.
compid, msg.
len, msg.
msgid);
193 #if (defined __QNX__) | (defined __QNXNTO__) 197 struct timespec time;
201 clock_gettime(CLOCK_REALTIME, &time);
202 micros = (uint64_t)time.tv_sec * 1000000 + time.tv_nsec/1000;
214 gettimeofday(&tv, NULL);
215 micros = ((uint64_t)tv.tv_sec) * 1000000 + tv.tv_usec;
uint8_t msgid
ID of message in payload.
uint64_t microsSinceEpoch()
int main(int argc, char *argv[])
uint8_t sysid
ID of message sender system/aircraft.
uint8_t compid
ID of the message sender component.
uint8_t len
Length of payload.
MAVLINK_HELPER uint8_t mavlink_parse_char(uint8_t chan, uint8_t c, mavlink_message_t *r_message, mavlink_status_t *r_mavlink_status)
MAVLINK_HELPER uint16_t mavlink_msg_to_send_buffer(uint8_t *buffer, const mavlink_message_t *msg)
Pack a message to send it over a serial byte stream.