qxf0218
级别: 探索解密
精华主题: 0
发帖数量: 18 个
工控威望: 135 点
下载积分: 607 分
在线时间: 3(小时)
注册时间: 2014-03-24
最后登录: 2016-07-11
查看qxf0218的 主题 / 回贴
楼主  发表于: 2014-07-01 11:01
西门子S7-300 BCC数据效验和             谁有用过的程序?分享一份。谢谢
学无止境
zjiori
级别: 正式会员
精华主题: 0
发帖数量: 4 个
工控威望: 94 点
下载积分: 578 分
在线时间: 3(小时)
注册时间: 2014-12-09
最后登录: 2014-12-31
查看zjiori的 主题 / 回贴
1楼  发表于: 2014-12-16 16:00
接口: IN: DB_NO  INT  DB number
           LEN    INT  Length of source area
       OUT:RETVAL INT  RETVAL
       TEMP:w_db_no  Word  DB number
            w_bcc_value  Word   actual BCC value
            w_temp_val   Word   temporary value
            d_loop_akt   DWord  actual value of the loop counter
            d_loop       DWord  loop counter

  L     #LEN                        // load length                  
      L     2                           // decrement length by 2
      -I                                // last 2 bytes are reserved for BCC
      SLD   3                           // calculate bit offset
      T     #d_loop                     // save the last bit address

      L     #DB_NO                      // load DB number
      T     #w_db_no
      OPN   DB [#w_db_no]               // and open DBs

      L     0
      T     #d_loop_akt                 // initialize loop counter
      T     #w_bcc_value                // initialize the BCC

// loop for calculating the BCC for (LEN - 2) bytes
// abort if d_loop_akt = d_loop

LOOP: L     DBB [#d_loop_akt]           // load byte from DB
      L     #w_bcc_value                // load actual BCC
      XOW                               // EXOR
      T     #w_bcc_value                // and save

      L     8                           // increment the bit address by 8
      L     #d_loop_akt                 // to address the next byte
      +D    
      T     #d_loop_akt

      L     #d_loop
      <I    
      JC    LOOP

// load the actual BCC
// and compare with the last two bytes of the source DB
// example: #LEN      22
//           0..19     data bytes
//           20,21     BCC-HI, BCC-LO

      L     #w_bcc_value                // load BCC
      AW    W#16#F0                     //separate the most significant nibble
      SLW   4                           // shift the least significant nibble to the highbyte
      OW    W#16#3000                   // OR 30h to Highbyte to receive ASCII character
      T     #w_temp_val                 // and save temporary

      L     #w_bcc_value                // load BCC
      AW    W#16#F                      //separate the least significant nibble
      OW    W#16#30                     // OR 30h to Lowbyte to receive ASCII character
      L     #w_temp_val
      OW    

      L     DBW [#d_loop_akt]           // Load BCC from DB
      ==I                               // and compare
      L     0                           // value of RETVAL, if comparison ok
      JC    ENDE

      L     -1                          // value of RETVAL, if comparison not ok
ENDE: T     #RETVAL

      BE