資源簡介
BC35.rar

代碼片段和文件信息
#include?“bc35.h“
//本程序只供學習使用,未經作者許可,不得用于其它任何用途????
//作者:余浩
//yh77530@163.com
//QQ:838625990
//創建日期:2019/6/6
u8???NB_ReceiveBuff[200];????//接收BUFF
u16??NB_ReceiveNum=0;????????//接收BUFF標志
u8???Command_Mode?=?0;???????//命令模式??默認關
u8???UDPSocket?=?0;??????????//UDPso
u8???NBok=0;
//獲取第n個字符的位置
char?NBget_Char_Position(u8?*istr?u8?ichar?u8?iposi)
{
??u8?posit=0;
u8?i;
for(i=0;*(istr+i)?!=?‘\0‘;i++)
{
??if(*(istr+i)?==?ichar)?posit++;
if(posit?==?iposi)??return?i;
}
return?-1;
}
//取出BUFF里面的數據
//返回0無數據,返回>0有數據
u8?receive_UDP_msg(char?*outstr)
{
??u8?ij=0;
??u8?pi;
if(NB_ReceiveNum?&?0x4000)
{
??pi?=?NBget_Char_Position(NB_ReceiveBuff?‘‘?2)+1;
??for(i=pi;(*(NB_ReceiveBuff+i)<=‘9‘&&*(NB_ReceiveBuff+i)>=‘0‘)||(*(NB_ReceiveBuff+i)<=‘F‘&&
???????????????*(NB_ReceiveBuff+i)>=‘A‘)||(*(NB_ReceiveBuff+i)<=‘f‘&&*(NB_ReceiveBuff+i)>=‘a‘);i+=2?)
??{
*(outstr+j)=0;
????if(*(NB_ReceiveBuff+i)>=‘0‘?&&?*(NB_ReceiveBuff+i)<=‘9‘)?????????*(outstr+j)?+=??(*(NB_ReceiveBuff+i)-‘0‘)*16;
else?if(*(NB_ReceiveBuff+i)<=‘F‘&&*(NB_ReceiveBuff+i)>=‘A‘)??????*(outstr+j)?+=??(*(NB_ReceiveBuff+i)-‘A‘+10)*16;
else?if(*(NB_ReceiveBuff+i)<=‘f‘&&*(NB_ReceiveBuff+i)>=‘a‘)??????*(outstr+j)?+=??(*(NB_ReceiveBuff+i)-‘a‘+10)*16;
????if(*(NB_ReceiveBuff+i+1)>=‘0‘?&&?*(NB_ReceiveBuff+i+1)<=‘9‘)?????*(outstr+j)?+=??(*(NB_ReceiveBuff+i+1)-‘0‘);
else?if(*(NB_ReceiveBuff+i+1)<=‘F‘&&*(NB_ReceiveBuff+i+1)>=‘A‘)??*(outstr+j)?+=??(*(NB_ReceiveBuff+i+1)-‘A‘+10);
else?if(*(NB_ReceiveBuff+i+1)<=‘f‘&&*(NB_ReceiveBuff+i+1)>=‘a‘)??*(outstr+j)?+=??(*(NB_ReceiveBuff+i+1)-‘a‘+10);
??????j++;
}
*(outstr+j)?=?0; ?
NB_ReceiveNum=0;
return?j;
}
else
??return?0;
}
//???例:15->F
char?valueToHexCh(int?value)
{
??char?result?=?‘\0‘;
if(value?>=?0??&&?value?<=?9)
{
??result?=?(char)(value+48);
}
else?if(value>=10?&&?value<=15)
{
??result?=?(char)(value-10+65);
}
}
//
int?StrToHex(char?*chchar?*hex)
{
int?temp?=?0;
while(*ch)
{
temp?=?(int)*ch;
*hex++?=??valueToHexCh(temp/16);
*hex++?=??valueToHexCh(temp%16);
?ch++;
}
*hex=‘\0‘;
}
//發送數據函數,傳入socket主機IP,遠程主機端口,數據長度,數據
//這里暫時使用字符串參數
//返回值0,發送成功(鑒于UDP為報文傳輸,數據主機是否接收到模塊是無法確認的)
//返回值1,發送失敗
uint8_t?send_UDP_msg(char?*hostIPchar?*portchar?*data)
{
char?ptr[200]=“AT+NSOST=“;
char?sockstr[2];
char?dataLen[3];
char?DATA[150];
u8?datal;
sprintf(sockstr“%d“UDPSocket);
datal?=?strlen(data);
sprintf(dataLen“%d“datal);?
StrToHex(dataDATA);
strcat(ptrsockstr);
strcat(ptr““);
strcat(ptrhostIP);
strcat(ptr““);
strcat(ptrport);
strcat(ptr““);
strcat(ptrdataLen);
strcat(ptr““);
strcat(ptrDATA);
strcat(ptr“\0“);
if(!BC95_send_cmd(ptr“OK“0))
{
return?0;
}
return?1;
}
//創建UDP鏈接,傳入本地UDP端口號,成功返回0-6的socket?id號,失敗返回1
u8?creat_UDP_socket(char*?local_port)
{
u8?i;
char?data[10]=““;
char?temp[64]=“AT+NSOCR=DGRAM17“;
strcat(templocal
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????8944??2019-07-31?17:22??BC35\bc35.c
?????文件????????679??2019-07-26?20:50??BC35\bc35.h
?????目錄??????????0??2019-07-31?17:22??BC35
-----------?---------??----------?-----??----
?????????????????9623????????????????????3
- 上一篇:STM32 矩陣鍵盤掃描
- 下一篇:PSCAD風電場同步發電機仿真及控制模型
評論
共有 條評論