📄 comfun.c
字号:
//comfun.c
/*=================================================
send:=>stx,len,cmd,id[1],id[0],0x00,data,edc,etx
ack :=>stx,len,cmd,id[1],id[0],status,edc,etx
==================================================*/
#include "com.h"
#include "comfun.h"
#include "string.h"
#include "sst89cxx.h"
#include "public.h"
unsigned char ReadFile(unsigned char *buf,unsigned char length);
unsigned char CheckCrc();
unsigned char ReceiveData(void);
void SendResponse(unsigned char status,unsigned char len,unsigned char *buf);
unsigned char IsShakeHands();
/************************************************************/
unsigned char ReadFile(unsigned char *buf,unsigned char length)
{
unsigned char i,temp;
for(i=0;i<length;i++)
{
if(ReadComm(&temp)!=TRUE)return FALSE;
*(buf+i)=temp;
}
return TRUE;
}
unsigned char ReceiveData(void)
{
unsigned char length;
timerout=10;
//stx
Buffer[1]=0x00;
while(timerout--)
{
Status=ReadFile(Buffer+0,1);
if(Status==0x00)
{
if(Buffer[0]==COMM_STX)break;
}
}
if(Status)return COMM_STX_ERROR;
//len
Status=ReadFile(Buffer+1,1);
if(Buffer[1]>(COMMMAXLEN-3)||Status)
{
return COMM_LEN_ERROR;
}
length=Buffer[1];
//data and xorcode
Status=ReadFile(Buffer+2,length+1);
if(Status)return COMM_DATA_ERROR;
//etx
Status=ReadFile(Buffer+2+length+1+1,1);
if(Buffer[2+length+1+1]!=COMM_ETX||Status)
{
return COMM_ETX_ERROR;
}
else
{
Status=CheckCrc();
if(Status!=COMM_OK) return COMM_CRC_ERROR;
}
return TRUE;
}
///////////////////////////////////////////
//检验crc:
unsigned char CheckCrc()
{
unsigned char i,CrcCode;
CrcCode=0X00; //PRESET_VALUE;
for(i=0;i<Buffer[1]+3;i++)
{
CrcCode^=Buffer[i];
}
if(CrcCode==0x00)return TRUE;
else return FALSE;
}
//////////////////////////////////////////
//发送数据
void SendResponse(unsigned char status,unsigned char len,unsigned char *buf)
{
unsigned char i;
unsigned char xorcode=0;
xorcode^=0x60^status^(len+1);
WriteComm(COMM_STX); //Delay(1);
WriteComm(len+1); // Delay(1);
WriteComm(status); //Delay(1);
for(i=0;i<len;i++)
{
WriteComm(buf[i]); //Delay(1);
xorcode^=buf[i];
}
WriteComm(xorcode); //Delay(1);
WriteComm(COMM_ETX);
}
unsigned char IsShakeHands()
{
unsigned char temp;
while(1)
{
WriteComm(0x55);
temp=0x00;
Status=ReadComm(&temp);
if(temp==0x55)
{
WriteComm(0x0f);
return TRUE;
}
if(Status!=TRUE)return FALSE;
}
return FALSE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -