91av视频/亚洲h视频/操亚洲美女/外国一级黄色毛片 - 国产三级三级三级三级

資源簡介

以前做過一個智能家居項目,其中涉及數據包和命令包的打包解包。可以下載下來學習使用

資源截圖

代碼片段和文件信息

#include?“pack.h“

/*函數名稱:unsigned?int?crc_16
??函數功能:16?bit?crc校驗
??輸入參數:unsigned?short?*ptr(存放需要校驗的16bit的數據)
??函數返回值:32位的數據包
*/
unsigned?int?crc_16(unsigned?short?*ptr)
{
unsigned?short?*ptrr?=?ptr;
unsigned?int?len??=?16;
unsigned?long?temp?=?0;
unsigned?int?crc;
unsigned?char?i;
//unsigned?short?*ptr?=?data;

while(len--)
{
for(i?=?0x80;i?!=?0;?i?=?i?>>?1)
{
temp?=?temp?*?2;
if?((temp?&?0x10000)?!=?0)
temp?=?temp?^?0x11021;
if?((*ptrr?&?i)?!=?0)
temp?=?temp?^?(0x10000?^?0x11021);
}
//printf(“%x\n“*ptr);
ptrr++;
}
crc?=?temp;
return?crc;
}

/*函數名稱:unsigned?int?datapack
??函數功能:打包
??輸入參數:struct?DATA?A(存放需要打包的數據(溫度))
??函數返回值:32位的數據包
*/
unsigned?int?datapack(struct?DATA?A)
{
//start?coding
short?d;//值
short?t;//臨時變量
short?a;//存放A.data
short?c;//循環變量
short?i;//余數
short?b[7];//存放a的二進制值
short?e2[7]={248163264128};//b的算子
unsigned?int?out;
unsigned?int?crc;
unsigned?short?data[16];
//unsigned?short?*ptr?=?data;

d=0;????//只使用正數部,(0)(16)
t?=?0;???//數據模式(0,0)(1514)
d=d+t;

if?(A.th?==?0)//溫度(00)(1312)
{
t?=?0;
d?=?d+t;
}
else??//濕度(01)(1312)
{
t?=?2048;
d?=?d+t;
}
if?(A.id?==?1)//id?(0,00)(1110,9)
{
t?=?0;
d?=?d+t;
}
else?if?(A.id?==?2)?//id?(0,01)(1110,9)
{
t?=?256;
d?=?d+t;
}
else?if?(A.id?==?3)?//id?(0,10)(1110,9)
{
t?=?512;
d?=?d+t;
}
else?//id?(0,11)(1110,9)
{
t?=?256+512;
d?=?d+t;
}

a?=?A.data;
while(a!=0)
{
i?=?a%2;
b[c]=i;
c++;
a=a/2;
}

for(;c>=0;c--)
{
t?=?b[c]*e2[c];
d?=?d+t;
}
//printf(“d?=?%x\n“d);
//return?d;
//crc
data[0]?=?d;
crc?=?crc_16(data);
//printf(“crc?=?%x\n“crc);
out?=?out?+?data[0];
out?=?out?< out?=?out?+?crc;
//printf(“0x%x\n“out);
return?out;
}

/*函數名稱:struct?DATA?datadepack
??函數功能:解包
??輸入參數:unsigned?int?in(存放需要解包的數據(溫度))
??函數返回值:結構體struct?DATA(存放數據)
*/
struct?DATA?datadepack(unsigned?int?in)
{
//crc
short?d;
unsigned?int?out;
unsigned?int?crc;
unsigned?short?data[16]={0};
data[0]?=?(in?>>?16);
//printf(“%d\n“data[0]);
//unsigned?short?*ptr?=?data;
crc?=?crc_16(data);
//printf(“%x\n“crc);
out?=?out?+?data[0];
out?=?out?< out?=?out?+?crc;
if?(in?!=?out)
printf?(“數據錯誤\n“);
else
{
d?=?data[0];
//?decoding
struct?DATA?A;//創建一個結構體
short?a=0;//輸入數據復制體
short?i=0;//余數
short?c=0;//循環變量
short?b[16]={0};//存放a的二進制值
short?e2[7]={1248163264};//b的算子
a?=?d;
while(a!=0)
{
i?=?a%2;
b[c]=i;
c++;
a=a/2;
}

//for?(c=0;c<=15;c++)
//printf(“%d?“b[c]);
//printf(“\n“);

if?(b[11]==0)
A.th?=?0;
else
A.th?=?1;

if?(b[9]==0)
{
if?(b[8]==0)
A.id?=?1;
else
A.id?=?2;
}
else
{
if?(b[8]==0)
A.id?=?3;
else
A.id?=?4;
}

a?=?0;
for?(c=0;c<=6;c++)
{
a?+=?b[c+1]*e2[c];
//printf(“%d?*?%d?\n“b[c+1]e2[c]);
}
//printf(“%d\n“a);
A.data?=?a;

return?A;
}

}

/*函數名稱:unsigned?int?cmdpack
??函數功能:打包
??輸入參數:struct?CMD?A(存放需要打包的數據(命令))
??函數返回值:32位的數據包
*/
unsigned?int?cmdpack(

?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件?????????243??2017-10-16?01:36??數據打包解包庫(含CRC校驗)\makefile
?????文件????????6119??2017-10-23?01:18??數據打包解包庫(含CRC校驗)\pack.c
?????文件????????1399??2017-10-23?09:06??數據打包解包庫(含CRC校驗)\pack.h
?????文件????????4568??2017-10-23?01:27??數據打包解包庫(含CRC校驗)\pack.o
?????文件?????????213??2017-10-16?10:38??數據打包解包庫(含CRC校驗)\readme.txt
?????文件???????13008??2017-10-23?01:27??數據打包解包庫(含CRC校驗)\test_c
?????文件?????????771??2017-10-23?01:19??數據打包解包庫(含CRC校驗)\test_c.c
?????文件????????2664??2017-10-23?01:27??數據打包解包庫(含CRC校驗)\test_c.o
?????文件???????13008??2017-10-23?01:27??數據打包解包庫(含CRC校驗)\test_d
?????文件?????????690??2017-10-23?01:20??數據打包解包庫(含CRC校驗)\test_d.c
?????文件????????2488??2017-10-23?01:27??數據打包解包庫(含CRC校驗)\test_d.o
?????文件???????15374??2017-10-16?11:01??數據打包解包庫(含CRC校驗)\項目數據結構解析v1.3.docx

評論

共有 條評論