回复:我要个新鲜的值,谁给我个方法?

Zaxife

  • 帖子

    2450
  • 精华

    31
  • 被关注

    78

论坛等级:至圣

注册时间:2008-07-24

普通 普通 如何晋级?

发布于 2017-09-09 23:41:19

11楼

策略大概这样做吧。

只是胡乱写的,不要较真。

void MB_Server(void){

     static uchar task = 0; //任务状态字

     static uchar unit = 1; //优先读写任务的从站地址

     static uchar unit2 = 1; //非优先必读任务的从站地址

    

     switch(task){

         case 0: //判断是否需要读取 n3

             if (Slave[unit][3].Read = true){

                 MB.Slave.Read(unit*10+3);

                 task = 1;

             }

             else{ //不需要读则直接转到下一个从站 判断完所有从站n3则转到 n4、n5 写判断

                 unit++;

                 if (unit > 5){

                      task = 2;

                 }

             }

             break;

        

         case 1: //等到读取完成 如果成功则取消读标志 不管成败都转到下一个从站读取 n3

             if (Slave[unit][3].Finish = true){

                 if (Slave[unit][3].TimerOver = true){

                      Slave[unit][3].Read = true;

                 }

                 else{

                      Slave[unit][3].Read = false;

                 }

                 task = 0;

                 unit++;

                 if (unit > 5){ //读取完5个从站则跳到 n4、n5 写判断

                     unit = 1;

                     task = 2;

                 }

             }

             break;

        

         case 2: //判断是否需要写 n4、n5

             if (Slave[unit][4].Write = true){

                 MB.Slave.Write(unit*10+4);

                 task = 3;

             }

             else if (Slave[unit][5].Write = true){

                 MB.Slave.Write(unit*10+5);

                 task = 3;

             }

             else{

                 unit++;

                 if (unit > 5){

                     unit = 1;

                     task = 4;

                 }

             }

             break;

        

         case 3:  //等到写完成 如果成功则取消写标志 不管成败都转到下一个从站 判断写n4、n5

             if (Slave[unit][4].Finish = true){

                 if (Slave[unit][4].TimerOver = true){

                      Slave[unit][4].Write = true;

                 }

                 else{

                      Slave[unit][4].Write = false;

                 }

                 task = 2;

                 unit++;

                 if (unit > 5){

                     unit = 1;

                     task = 4;

                  }

             }

             else if (Slave[unit][5].Finish = true){

                 if (Slave[unit][5].TimerOver = true){

                      Slave[unit][5].Write = true;

                 }

                 else{

                      Slave[unit][5].Write = false;

                 }

                 task = 2;

                 unit++;

                 if (unit > 5){

                      task = 4;

                 }

             }

             break;

        

         case 4: //读n1

             MB.Slave.Read(unit2*10+1);

             task = 5;

             break;

            

         case 5: //等到读n1完成,跳转到读n2

             if (Slave[unit2][1].Finish = true){

                 task = 6;

                 break;

             }

             break;

        

         case 6: //读n2

             MB.Slave.Read(unit2*10+2);

             task = 7;

             break;

        

         case 7: //等到读n2完成,跳转到判断读n3

             if (Slave[unit2][2].Finish = true){

                 task = 0;

                 unit2++;

                 if (uni2 > 5){

                      unit2 = 1;

                 }

             }

             break;

        

         default:

             task = 0;

             break;

      }

}


评论
编辑推荐: 关闭

请填写推广理由:

本版热门话题

SIMATIC S7-200 SMART

共有8941条技术帖

相关推荐

热门标签

相关帖子推荐

guzhang

恭喜,你发布的帖子

评为精华帖!

快扫描右侧二维码晒一晒吧!

再发帖或跟帖交流2条,就能晋升VIP啦!开启更多专属权限!

  • 分享

  • 只看
    楼主

top
您收到0封站内信:
×
×
信息提示
很抱歉!您所访问的页面不存在,或网址发生了变化,请稍后再试。