//#include "STC12C5410AD.H"
#include "STC12C5201.H"
#include "fun.h"
#include "ex_var.h"
//#include "PubDef.h" /* 宏定义 */
//#include "PubVar.h" /* 所有外部变量 声 明 */
//#include "pubfun.h" /* 所有外部函数声明 */
uchar *P_PCSetIndex; // 发送的指针
uchar SendLenth; //发送的数据长度
uchar ReviceLenth; //接收的数据长度
uchar PCSetRCommFlag; // 是否收到一条全的命令的标志位
uchar idata PCSetBuf[60];
uchar code CLString[15]="Ver2.0220090911";
uchar code HelpStr[129]=
"#<ADD><Flag><Command><RUN><Length><info><Verify><OXOD><Designed BY:dingtongfu><TEL:13758711976;QQ:86973136;Email:
[email protected]>";
uchar idata ApmlidAddr=1;
uint idata ABottleCapacity;
//# <地址> <标识码> <命令号> <执行状态> <信息长度> <信息体> <校验和> <0x0D>
// 结构定义
/*
typedef struct
{ uchar idata Hour;
uchar idata Minute;
uchar idata Second;
}TimerType; // 输液器时间
TimerType ApmlidTimer; //时间
调度命令
#01056F0000FE
#010168000220
#010166000225
#010167000250
#010169000250
#01016A000490BB
*/
uchar asc2hex(uchar c1)
{ if (c1<0x61 && c1>0x40)
{c1=c1-0x37;}
else if (c1<0x41)
{c1=c1-0x30;}
else if (c1>0x60)
{c1=c1-0x57;}
return(c1);
}
uchar twoascii2byte(uchar c1,uchar c2)
{ c1=asc2hex(c1);
c2=asc2hex(c2);
return((c1<<4)|c2);
}
/* 门禁协议的校验和算法 */
uchar SendChksum(uchar len,uchar *p) /*回发的校验和函数(要先HEX转成ASC再求校验和)*/
{
uchar i,ChksumData=0;
for(i=0;i<len;i++)
{ ChksumData += *p++;
// ChksumData+=hi_hex2asc(*p);
// ChksumData+=low_hex2asc(*p++);
}
return ~ChksumData+1;
}
uchar chk_pc_chksum(uint len,uchar *p) /*校验接收的协议校验和的函数*/
{ uint i;
uchar chk_sum=0;
for (i=0;i<len;i++)
{ chk_sum += *p++;
}
return(chk_sum); /*送回检验值*/
}
void MainPCDealFun() /* 处理PC 发送的命令 */
{// #010046000205XX
uchar *PDA=CLString;
uchar *Help=HelpStr;
uchar idata TempLen=0,TempAddrT;
uchar idata i,TempDataX,TempFlagW=1;
uchar idata ExecuteState=0; // 返回一个执行状态
if(PCSetRCommFlag==1) /* PC是否发了门禁协议 */
{ PCSetRCommFlag = 0;
TempAddrT = twoascii2byte(PCSetBuf[1],PCSetBuf[2]); // 取PC发过来的地址
if(TempAddrT==0xFF || ApmlidAddr==TempAddrT) //判地址、校验和
{
TempLen = twoascii2byte(PCSetBuf[9],PCSetBuf[10]); // 算参数长度
TempAddrT = twoascii2byte(PCSetBuf[11+TempLen],PCSetBuf[12+TempLen]); // 取PC发送的校验和
TempAddrT +=chk_pc_chksum(10+TempLen,&PCSetBuf[1]); // 算校验和
if(TempAddrT==0) // 校验和
{
TempDataX = twoascii2byte(PCSetBuf[5],PCSetBuf[6]); /*协议的命令码 */
switch(TempDataX)
{
case 0x11: // 广播读
{ PCSetBuf[6] = ApmlidAddr; // 送地址
for(i=0; i<15; i++) // 厂家信息
{
PCSetBuf[7+i] = *PDA++;
}
TempLen = 16;
}
break;
case 0x15: // 综全命令,取综合数据
{/*
PCLogoFlag=1;
SetTimer(5,10000);
PCSetBuf[6] = ApmlidPipeType; // 管型
PCSetBuf[7] = TrueDropSpeed; // 滴速
PCSetBuf[8] = (uchar)(AmlidRemind>>8);
PCSetBuf[9] = (uchar)AmlidRemind; // 提醒量
ApmlidTimer.Hour=UsedTimes/60;
ApmlidTimer.Minute=UsedTimes%60;
ApmlidTimer.Second=Second;
PCSetBuf[10] = ApmlidTimer.Hour;
PCSetBuf[11] = ApmlidTimer.Minute;
PCSetBuf[12] = ApmlidTimer.Second; // 输液时间
PCSetBuf[13] = (uchar )(TransfusionSize>>8);
PCSetBuf[14] = (uchar )(TransfusionSize); // 输液量
if(SysState==0)
{
PowerState=1;
}
PCSetBuf[15] = PowerState; // 供电电源状态
AlarmCode = ErrNumber;
PCSetBuf[16] = AlarmCode;
PCSetBuf[17] = (uchar )(AmlidRemindTimes>>8); //提醒时间
PCSetBuf[18] = (uchar )(AmlidRemindTimes); //提醒时间
PCSetBuf[19] = (uchar )(ABottleCapacity>>8); //单瓶总量
PCSetBuf[20] = (uchar )(ABottleCapacity);
PCSetBuf[21] = (uchar )(ABottleTimes>>8); //单瓶时间
PCSetBuf[22] = (uchar )(ABottleTimes);
TempLen=17;
*/
}
break;
//
case 0x61: //读安全密码
{
TempLen=2;
}
break;
case 0x62: //写安全密码
{
TempLen = 0;
}
break;
//
case 0x65: //读地址
{// PCSetBuf[6] = ApmlidAddr; // 送地址
TempLen=1;
}
break;
case 0x64: /*设置地址 */
{
// ApmlidAddr = twoascii2byte(PCSetBuf[11],PCSetBuf[12]); /* 设置的新地址数 */
// ApmliChkSum = (~ApmlidAddr)+1;
// write_eep_byte(ApmlidAddr,ADDR_EEP);
// write_eep_byte(ApmliChkSum,ADDRSum_EEP);
TempLen = 0;
}
break;
case 0x66: // 设管型
{
// Fan_PWM1(twoascii2byte(PCSetBuf[11],PCSetBuf[12]));
TempLen = 0;
}
break;
case 0x67: // 读管型
{
CR=~CR;
TempLen=1;
}
break;
case 0x68: //设滴速
{
//Heat_PWM0(twoascii2byte(PCSetBuf[11],PCSetBuf[12]));
TempLen = 0;
}
break;
case 0x69: // 读滴速
{
CCAPM1=0;
TempLen=1;
}
break;
case 0x6A: //设提醒量
{
SetTemp=twoascii2byte(PCSetBuf[11],PCSetBuf[12]);
TempLen = 0;
}
break;
case 0x6B: // 读提醒量 Int
{
TempLen=2;
}
break;
case 0x6C: //设提醒时间
{
TempLen = 0;
}
break;
case 0x6D: // 读提醒时间 Int
{
TempLen=2;
}
break;
case 0x6E: //设单瓶量
{ ABottleCapacity = (uint )(twoascii2byte(PCSetBuf[11],PCSetBuf[12]));
ABottleCapacity = (ABottleCapacity<<8);
ABottleCapacity += (uint )(twoascii2byte(PCSetBuf[13],PCSetBuf[14]));
TempLen = 0;
}
break;
case 0x6F: // 读单瓶量 Int
{
// TempData=Get_AD_result(0);
// FanCurrent=Get_AD_result(1);
// FanValtage=Get_AD_result(2);
// ABottleCapacity=FanCurrent;//TempData;
// ABottleCapacity = (ABottleCapacity<<8);
// ABottleCapacity+=FanValtage;
// PCSetBuf[6] = (uchar)(ABottleCapacity>>8);
// PCSetBuf[7] = (uchar)ABottleCapacity;
TempLen=2;
}
break;
case 0x70: //设单瓶时间
{
TempLen = 0;
}
break;
case 0x71: // 读单瓶时间 Int
{
TempLen=2;
}
break;
case 0xF0: // 跳入ISP区,执行下载
// GotoISP();
//ISP_CONTR=0x60;
//233031303046303030303030390D
break;
case 0xC9:
PCSetBuf[6] = ApmlidAddr; // 送地址
for(i=0; i<133; i++) // 厂家信息
{
PCSetBuf[7+i] = *Help++;
}
TempLen = 129;
break;
default:
TempFlagW = 0;
break;
}
}
else //校验和不正确
{ TempDataX = twoascii2byte(PCSetBuf[5],PCSetBuf[6]); /*协议的命令码 */
ExecuteState = 1; // 执行状态为校验和出错
TempLen = 0; // 信息体长度
}
if(TempFlagW==1)
{
PCSetBuf[1] = ApmlidAddr;
PCSetBuf[2] = twoascii2byte(PCSetBuf[3],PCSetBuf[4]); // 标识码
PCSetBuf[3] = TempDataX; //命令号
PCSetBuf[4] = ExecuteState; //执行状态
PCSetBuf[5] = TempLen; /* 信息体长度4 9 */
PCSetBuf[6+TempLen] = SendChksum(5+TempLen,&PCSetBuf[1]); // 校验和
PCSetBuf[7+TempLen] = 0x0D;
SendLenth = 7+TempLen;
P_PCSetIndex = &PCSetBuf[1];
SBUF='#';
}
}
}
}
void DealPCSetCommand(uchar TA1)
{
if(ReviceLenth<1) /* 头判断 */
{
if(TA1=='#')
{ PCSetBuf[ReviceLenth++] = TA1;
}
else
{ ReviceLenth = 0;
}
}
else
{ if(ReviceLenth>12) /* 接收的最短长度 */
{
if(ReviceLenth<50)
{
if(TA1==0x0D)
{ PCSetBuf[ReviceLenth] = TA1;
ReviceLenth = 0;
PCSetRCommFlag=1; /* 一条命令已接收到 */
}
else
{ PCSetBuf[ReviceLenth++] = TA1;
}
}
else
{ ReviceLenth = 0;
}
}
else
{ PCSetBuf[ReviceLenth++] = TA1;
}
}
}
void RevicePCCo