📄 main.c
字号:
while((IFG1 & UTXIFG0) == 0);
TXBUF0 = AddressIndex[m_AddressIndex];
while((IFG1 & UTXIFG0) == 0);
TXBUF0 = AddressIndex[m_AddressIndex];
SendData(ProtoCodeCon,6); //终端总线申请
CommunicationErr = LinkHandErr;
ConflictFlag |= TXDTIMEOUT; //设置发送标志
WatchTransTimeOut = 0;
break;
case STATESENDERR:
while((IFG1 & UTXIFG0) == 0);
TXBUF0 = AddressIndex[m_AddressIndex];
while((IFG1 & UTXIFG0) == 0);
TXBUF0 = AddressIndex[m_AddressIndex];
if((Flag & SwitchOn) == SwitchOn) //异常开关关闭
{
SendData(SwitchOnCode , 6);
}
else if((Flag & SwitchOff) == SwitchOff) //异常开关解除
{
SendData(SwitchOffCode , 6);
}
CommunicationErr = STATESENDERR;
ConflictFlag |= TXDTIMEOUT; //设置发送标志
WatchTransTimeOut = 0;
break;
case FREEBUSERR:
while((IFG1 & UTXIFG0) == 0); //数据发送完毕,发送释放总线请求
TXBUF0 = AddressIndex[m_AddressIndex];
while((IFG1 & UTXIFG0) == 0 ) ;
TXBUF0 = AddressIndex[m_AddressIndex];
SendData(CorrectCode, 2);
CommunicationErr = FREEBUSERR;
ConflictFlag |= TXDTIMEOUT; //设置发送标志
WatchTransTimeOut = 0;
break;
default:
break;
}
}
if((WatchRecevice >= 5)) //等待一桢传输结束
{
if((WatchRecevice >= 5) && (UartCounter <= 9)) //数据桢接收完毕~!
{
_DINT();
TBCCTL0 &= ~CCIE; //允许定时器A中断
WatchRecevice = 0;
UartCounter = 0;
if((ReceviceBuff[0] == AddressIndex[m_AddressIndex])
|| (ReceviceBuff[0] == '|')) //总线上接收到是自己地址和PLC的地址
{
for(i=0; i<45; i++);
UartFlag = 0X00;
UartCount = 0;
while((ReceviceBuff[UartCount] == CodeTable[0][UartCount]) && (UartCount <8))
{ //收到服务器发送的忙状态
if(UartCount == 5)
{
if(((ConflictFlag & LINKSENDFLG) == LINKSENDFLG)
&&((ConflictFlag & NEEDCOMM) == NEEDCOMM))
{ //发送总线请求产生冲突
Index = rand()%7; //产生下一个随机等待时间
Index += SecIndex; //与上一随机时间相加
TimerCount = 0;
SecIndex = Index; //保存当前随机数
UartFlag |= CONFLICTFLG;
ConflictFlag &= ~LINKSENDFLG; //总线冲突,允许再一次申请总线。
DisplayFlg &= ~FirstFlg; //不去在中断中再产生随机时间
}
else
{
UartFlag = 0X8000;
}
break;
}
else
{
UartCount++;
}
}
UartCount = 2;
while((ReceviceBuff[UartCount] == CodeTable[1][UartCount]) //总线申请允许
&& (UartCount < 10) && (UartFlag == 0X00))
{
if(UartCount == 6)
{
UartFlag |= BUSENABLEFLG; //总线申请允许
ConflictFlag |= SENDDISABLE; //不允许发送总线请求
ConflictFlag &= ~SENDENABLE; //允许发送总线请求
ConflictFlag |= HASLINKEDFLG; //已经与PLC建立链接
UartFlag &= ~CONFLICTFLG; //无总线冲突
SecIndex = 0; //当前延时值副本
TimerCount = 0; //总线空闲值
Index = 0; //当前延时
break; //跳出循环
}
else
{
UartCount++;
}
}
UartCount = 2;
while((ReceviceBuff[UartCount] == CodeTable[2][UartCount]) //总线释放指令
&& (UartCount < 10) && (UartFlag == 0X00))
{
if(UartCount == 6)
{
if((ConflictFlag & HASLINKEDFLG) == HASLINKEDFLG)
ConflictFlag &= ~NEEDCOMM;
UartFlag = 0X8000;
ConflictFlag &= ~SENDDISABLE; //允许发送总线请求
ConflictFlag |= SENDENABLE; //允许发送总线请求
ConflictFlag &= ~LINKSENDFLG; //无发送链接指令字
ConflictFlag &= ~HASLINKEDFLG; //释放与PLC建立的链接
//UartFlag &= ~CONFLICTFLG; //无总线冲突
DisplayFlg |= FirstFlg; //重新开始延时
SecIndex = 0; //当前延时值副本
TimerCount = 0; //总线空闲值
Index = 0; //当前延时
break; //跳出循环
}
else
{
UartCount++;
}
}
UartCount = 2;
while((ReceviceBuff[UartCount] == CodeTable[3][UartCount]) //收到PLC
&& (UartCount < 9) && (UartFlag == 0X00))
{
if(UartCount == 3)
{
switch(CommunicationErr)
{
case ReSendErr:
UartFlag = 0X00; //如果上次发送是重发指令
UartCount = 8;
break;
case SendDataErr: //如果上次发送是发送数据指令
break;
case ConfirmErr:
break;
case LinkHandErr: //上次发送是总线申请指令
UartFlag |= LinkHandFlg;
_NOP();
_NOP();
break;
case STATESENDERR: //发送状态错误
UartFlag |= BUSENABLEFLG;
break;
case FREEBUSERR: //总线释放指令
UartFlag |= SendDataFlg;
break;
}
break; //跳出循环
}
else
{
UartCount++;
}
}
UartCount=2;
while((ReceviceBuff[UartCount] == CodeTable[4][UartCount]) //PLC接收完毕指令
&& (UartCount<8) && (UartFlag == 0X00)
&& ((ConflictFlag & HASLINKEDFLG) == HASLINKEDFLG))
{
if(UartCount == 3)
{
switch(CommunicationErr) //错误通信指令字
{
case ReSendErr: //重发错误
case SendDataErr: //发送数据错误
case ConfirmErr: //确认错误
case LinkHandErr: //链接错误
case STATESENDERR: //发送状态错误
while((IFG1 & UTXIFG0) == 0); //数据发送完毕,发送释放总线请求
TXBUF0 = AddressIndex[m_AddressIndex];
while((IFG1 & UTXIFG0) == 0 ) ;
TXBUF0 = AddressIndex[m_AddressIndex];
SendData(CorrectCode, 2);
CommunicationErr = FREEBUSERR;
UartFlag = 0X8000;
ConflictFlag |= TXDTIMEOUT; //设置发送标志
WatchTransTimeOut = 0;
break;
}
break;
}
else
{
UartCount++;
}
}
UartCount = 2;
while((ReceviceBuff[UartCount] == CodeTable[5][UartCount])
&& (UartCount < 9) && (UartFlag == 0X00)
&& ((ConflictFlag & HASLINKEDFLG) == HASLINKEDFLG))
{
if(UartCount == 6)
{
P3OUT |= P3OUT_1; // 异常灯点亮
UartFlag = SendDataFlg;
break; //跳出循环
}
else
{
UartCount++;
}
}
UartCount = 2;
while((ReceviceBuff[UartCount] == CodeTable[6][UartCount])
&& (UartCount < 9) && (UartFlag == 0X00)
&& ((ConflictFlag & HASLINKEDFLG) == HASLINKEDFLG))
{
if(UartCount == 6)
{
P3OUT &= ~P3OUT_1; // 异常灯点灭
UartFlag = SendDataFlg;
break; //跳出循环
}
else
{
UartCount++;
}
}
if((UartFlag == 0X00)
&&((ConflictFlag & LINKSENDFLG) == LINKSENDFLG)) //没有总线发送权资格的子台 //必须有链接标志才能发送数据
{
while((IFG1 & UTXIFG0) == 0);
TXBUF0 = AddressIndex[m_AddressIndex];
while((IFG1 & UTXIFG0) == 0 ) ;
TXBUF0 = AddressIndex[m_AddressIndex];
SendData(ReTxdData,2); //要求重新发送
UartCount = 0;
CommunicationErr = ReSendErr; //记录发送的指令
ConflictFlag |= TXDTIMEOUT; //设置发送超时标志
WatchTransTimeOut = 0;
}
if((UartFlag & SendDataFlg) == SendDataFlg
&&((ConflictFlag & HASLINKEDFLG) == HASLINKEDFLG))
{
while((IFG1 & UTXIFG0) == 0); //数据发送完毕,发送释放总线请求
TXBUF0 = AddressIndex[m_AddressIndex];
while((IFG1 & UTXIFG0) == 0 ) ;
TXBUF0 = AddressIndex[m_AddressIndex];
SendData(CorrectCode, 2);
CommunicationErr = FREEBUSERR;
ConflictFlag |= TXDTIMEOUT; //设置发送超时标志
WatchTransTimeOut = 0;
}
if(((UartFlag & BUSENABLEFLG) == BUSENABLEFLG)
&&((ConflictFlag & HASLINKEDFLG) == HASLINKEDFLG)) //总线申请允许
{
while((IFG1 & UTXIFG0) == 0);
TXBUF0 = AddressIndex[m_AddressIndex];
while((IFG1 & UTXIFG0) == 0);
TXBUF0 = AddressIndex[m_AddressIndex];
if((Flag & SwitchOn) == SwitchOn) //异常开关关闭
{
SendData(SwitchOnCode , 6);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -