woshinibaba321
nininiccccc
采纳率0%
2020-03-21 07:56 阅读 242

关于单片机通讯协议代码程序架构的问题

项目内容:集中控制器是485协议,硬件原因现在做一个转接板,通过转接板把485协议转换成uart协议
请教问题:整个软件的架构,是通过两个中断进行收发,但是在调试过程中感觉软件架构有问题,485接收后打开网络(uart)处理标志位,处理完成之后将内容发出去,以下是关于代码的截图,想咨询一下,这样处理收发是否有问题图片说明

图片说明
图片说明
转接板中主动向下位机即设备发送查询指令,是通过定时器,每五秒计时,标志位置1来实现的,请教数据收发与主动查询如何做到互斥关系

main.c轮询:
```while(1)
    {
        Wdtclear();                 //清看门狗处理
        Rs485Communication();       //RS485通讯处理
        NetcomLogical();            //网络家电通讯处理
        timer();                    //计时处理
        SysErrorDeal();
    }

void Rs485Communication(void)
{
unsigned char UartCheckSum=0;
unsigned char i;
Wdtclear();
if(0 == F_Rs485ReceOrTran) //接收状态
{
if(g_uiRs485Recetimer > 1000) //接收超时,重新接收 20100928 把时间放宽47-->1000
{
g_uiRs485Recetimer = 0;
re_u0c1 = 1;

te_u0c1 = 0;
IO_DE_RE = 0;
g_ucRs485Pointer= 0;
F_Rs485ReceOrTran = 0;
F_Rs485ReceHeadFlag = 0; //接收错误时,清除接到帧头标志 20100427@1
return;
}
if(1 == F_Rs485ReceDataEndFlag) //接收完成
{
F_Rs485ReceDataEndFlag = 0; //清除接收完成标志
if(0xFB == g_ucRs485ReceDataBuff[g_ucRs485DataNum-1])
{

for(i=2;i<(g_ucRs485DataNum-2);i++) //计算校验和
{
UartCheckSum += g_ucRs485ReceDataBuff[i];
}
if(UartCheckSum == g_ucRs485ReceDataBuff[g_ucRs485DataNum-2]) //校验和正确
{
F_Rs485FeedbackAck = 0;
F_Rs485FeedBackStatus = 0;
F_Rs485FeedBackSysMonitor = 0;
F_Rs485FeedBackConMonitor = 0;
F_Rs485FeedBackWorkMonitor = 0;
F_Rs485FeedBackWorkMonitor1 = 0; //20100927
F_RS485Monitor = 0; // 20101209
F_Rs485FeedBackTimermonitor = 0;
for(i=0;i<g_ucRs485DataNum;i++)

{
g_ucRs485ReceData[i]=g_ucRs485ReceDataBuff[i];
}

                        F_Rs485ReceOrTran=1;        //转换为发送
                        Rs485RecedataFrame();            //接收正确后数据处理
                        g_ucRS485Time = 0;              //接收超时用
                        F_SysRs485Begin = 1;
                        #if(M_RS485BUSYDETECT)
                            F_SystemRs485EnableSend = 0;     //20101201    等待总线空闲
                        #endif
                        //goto lebel;
                    }
                    else                            //校验和错误  重新接收
                    {
                        re_u0c1 = 1;   
                        IO_DE_RE = 0; 
                        g_ucRs485Pointer= 0;
                        F_Rs485ReceOrTran = 0;
                        F_Rs485ReceHeadFlag = 0;                //接收错误时,清除接到帧头标志 20100427@1
                        g_uiRs485Recetimer = 0;
                    }//end  of  if(UartCheckSum == Net_rece_data_buff[Net_data_num-1])  //校验和正确
            }
            else//帧尾错误,重新接收
            {
                    re_u0c1 = 1;    
                    IO_DE_RE = 0;
                    g_ucRs485Pointer= 0;
                    F_Rs485ReceOrTran = 0;
                    F_Rs485ReceHeadFlag = 0;                //接收错误时,清除接到帧头标志 20100427@1
                    g_uiRs485Recetimer = 0;
            }   
    }
    else                                //未接收完成
    {
    }  //end of     if(1 == NetReceDataEndFlag) //接收完成
}

//lebel: //发送状态
else
{
if(0 == F_Rs485TranStart)
{
//****************************************** 等待总线空闲 20101201
#if(M_RS485BUSYDETECT)
if(0 == F_SystemRs485EnableSend)
{
if(g_uiRs485BusBusyTimer > 20) //总线清闲 125us*20 = 2500us
{
F_SystemRs485EnableSend = 1;
}
return;
}
#endif
//**********************************************
Rs485TrandataFrame();
F_Rs485TranStart = 1;

F_Rs485TranEndFlag = 0;
u0tb = g_ucRs485TranData[0];
te_u0c1 = 1;
re_u0c1 = 0;
IO_DE_RE = 1; //485发送

            g_ucRs485Pointer = 1;
    }
    else        //开始发送
    {
        if(0 == F_Rs485TranEndFlag)
        {   

        }  
        else
        {
            te_u0c1 = 0;
            re_u0c1 = 1;
            IO_DE_RE = 0;
            g_ucRs485Pointer =0;
            F_Rs485TranStart = 0;
            F_Rs485ReceOrTran = 0;      
            F_Rs485ReceHeadFlag = 0;                //重新接收时,清除接到帧头标志 20100427@1     
            g_uiRs485Recetimer = 0;
        }
    }// end of  if(0 == NetTranStartFlag)
}

}


void NetcomLogical(void)
{
Wdtclear();
Check();
NetCommunication();
TempLock();


```void  NetCommunication(void)
```{
    unsigned char UartCheckSum=0;
    unsigned char i;

    if(0 == F_NetReceOrTran)    //接收状态
    {
        if(1 == F_NetReceDataEnd) //接收完成
        {
            F_NetReceDataEnd = 0;   //清除接收完成标志

            for(i=2;i<(g_ucNetDataNum-1);i++)  //计算校验和
            {
                UartCheckSum += g_ucNetRecedataBuff[i];
            }
            UartCheckSum=UartCheckSum+0x55*g_ucNetDataAdd; //校验和加上发送数据中如果有0xFF的时增加的0x55
            g_ucNetDataAdd=0;
            if(UartCheckSum == g_ucNetRecedataBuff[g_ucNetDataNum-1])   //校验和正确
            {
                F_NetFeedBackInvalid = 0;
                F_NetFeedBackStatus = 0;
                g_ucMasterUartTime=0;   //主机通讯超时
                for(i=0;i<g_ucNetDataNum;i++)       
                {
                    g_ucNetRecedata[i]=g_ucNetRecedataBuff[i];
                }

                F_NetReceOrTran=1;        //转换为发送
                R232_ReFrameDeal();
            }
            else                            //校验和错误  重新接收
            {
                re_u2c1 = 1;    
                g_ucNetPointer= 0;
                F_NetReceOrTran = 0;
            }//end  of  if(UartCheckSum == Net_rece_data_buff[Net_data_num-1])  //校验和正确   
        }
        else                                //未接收完成
        {
            te_u2c1 = 0;            
            re_u2c1 = 1;
        }  //end of     if(1 == NetReceDataEndFlag) //接收完成
    }
    else                                    //发送状态
    { 
        if ( 0 == Uhome_Re2Tr50ms )                     //接收转发送延迟时间到  @20121030
        {
            if(0 == F_NetTranStart)
            {
                if ((F_SysNetConFlag==1)||(F_SysNetCheckFlag==1)||(F_SysNetCheckEFlag==1))
                {
                     Uhome_TrFrameDealNEW();
                     F_NetTranStart = 1;    
                     F_NetTranEnd = 0;  
                     te_u2c1 = 1;
                     re_u2c1 = 0;
                     u2tbl = g_ucNetTrandata[0];
                     g_ucNetPointertr = 1;
                     F_SysNetConFlag=0;
                     F_SysNetCheckFlag=0;
                     F_SysNetCheckEFlag=0;
                }           
            }
            else        //开始发送
            {
                if(0 == F_NetTranEnd)
                {
                    g_ucNetPointertr =0;
                    F_NetTranStart = 0;
                    F_NetReceOrTran = 0;    
                    te_u2c1 = 0;
                    re_u2c1 = 1;

                }
            }// end of  if(0 == NetTranStartFlag)
        }
    }
```}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

相关推荐