windows环境UDP发送free-d协议数据,全网独家!

话不多说上代码:

#include 
#include 
#include  
#include 
#include  
#pragma comment(lib,"ws2_32.lib")
using namespace std;

byte buf[29] = { 0xD1, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70 };		//缓冲区存放发送的数据
int  kk=0;


void count()
{
	byte buffer[5] = { 0 };
	int check = 0x40;
	int* b;

	for (int i = 0; i < 28; i++)
	{
		check -=buf[i];
	}
	b = (int*)(buffer + 0);
	*b = check;
	buf[28] = buffer[0];
}

DWORD WINAPI Fun(LPVOID lpParamter)
{
	int a=1;
	int k;
	byte buffer[10] = { 0 };
	int* b;

	printf("校准频率:");
	cin >> kk;

	while (1)
	{
		printf("请输入Pan:\n");
		cin >> a;
		k = a * 32768;
		b = (int*)(buffer + 0);
		*b = k;
		buf[2] = buffer[2];
		buf[3] = buffer[1];
		buf[4] = buffer[0];
		count();
		
		printf("请输入Tilt:\n");
		cin >> a;
		a = a * 32768;
		b = (int*)(buffer + 0);
		*b = a;
		buf[5] = buffer[2];
		buf[6] = buffer[1];
		buf[7] = buffer[0];
		count();

		printf("请输入Roll:\n");
		cin >> a;
		a = a * 32768;
		b = (int*)(buffer + 0);
		*b = a;
		buf[8] = buffer[2];
		buf[9] = buffer[1];
		buf[10] = buffer[0];
		count();

		printf("请输入X:\n");
		cin >> a;
		a = a * 64000;
		b = (int*)(buffer + 0);
		*b = a;
		buf[11] = buffer[2];
		buf[12] = buffer[1];
		buf[13] = buffer[0];
		count();

		printf("请输入Y:\n");
		cin >> a;
		a = a * 64000;
		b = (int*)(buffer + 0);
		*b = a;
		buf[14] = buffer[2];
		buf[15] = buffer[1];
		buf[16] = buffer[0];
		count();

		printf("请输入Z:\n");
		cin >> a;
		a = a * 64000;
		b = (int*)(buffer + 0);
		*b = a;
		buf[17] = buffer[2];
		buf[18] = buffer[1];
		buf[19] = buffer[0];
		count();
	}
}


int main()
{
	char param[20] = {0};
	printf("请输入ip地址:");
	cin >> param;
	
	WSADATA wsaData;				 //指向WinSocket信息结构的指针
	SOCKET hSocket;					 //创建套接字
	SOCKADDR_IN saUdpServ;			 //指向通信对象的结构体指针                          
	BOOL fBroadcast = TRUE;			 //用于setsockopt(),表示允许  
	int ncount = 0;					 //用于显示消息数目

	//*************************** 第一步初始化Winsock   *****************************//
	if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0)     //进行WinSocket的初始化
	{
		printf("Can't initiates windows socket!Program stop.\n");//初始化失败返回-1
		return -1;
	}

	//********************   第二步建立一个数据报类型的UDP套接字  ******************//
	hSocket = socket(PF_INET, SOCK_DGRAM, 0);

	//       setsockopt函数用于设置套接口选项
	//       采用广播形式须将第三个参数设置为SO_BROADCAST
	setsockopt(hSocket, SOL_SOCKET, SO_BROADCAST, (CHAR*)&fBroadcast, sizeof(BOOL));

	//  参数设置,注意要将IP地址设为INADDR_BROADCAST,表示发送广播UDP数据报
	saUdpServ.sin_family = AF_INET;
	saUdpServ.sin_addr.s_addr = inet_addr(param);		//htonl ( INADDR_BROADCAST );
	saUdpServ.sin_port = htons(6100);               //发送用的端口,可以根据需要更改


	HANDLE hThread = CreateThread(NULL, 0, Fun, NULL, 0, NULL);//创建线程
	
	while (1)
	{
		sendto(hSocket, (char*)&buf, 29, 0, (SOCKADDR*)&saUdpServ, sizeof(SOCKADDR_IN));
		Sleep(kk);
	}
	
	//*********************   第四步关闭socket  ***************************************//
	closesocket(hSocket);         //关闭监听socket
	WSACleanup();
	CloseHandle(hThread);	//关闭线程句柄
	system("pause");
	return 0;
}

根据free-d文档写的输出代码,为测试所用!
遇到的难点:
一、int类型数据两两拆分16进制数放进一个数组空间里。
二、同步信号,暂时未能获取,只能取巧,while循环无间隔发送,代码行运行速率将会是接收端的无数倍,再开启延时,在一瞬间达到同步效果。
三、一开始不知道checksum的计算方法,后来才知道文档的计算方法是0x40-数组的前28个空间的数据取最后两位。

你可能感兴趣的:(c语言)