Electron 使用nodeS7 与PLC交互

1.下载依赖  

  npm i nodes7
 var nodes7 = require('nodes7');

2.使用,去掉业务代码即可

saveWarn(){
      this.$refs.warnFormRef.validate((valid) => {
        if (valid) { 
          // if(!this.isOK) { 
          //   return ElMessageBox.confirm(`作业需要进行中才能修改系统软件参数!`, "提示", { type: "error" })
          // } 
          let loading = ElLoading.service({
            text: ``,
            fullscreen: true,
            background: 'rgba(0, 0, 0, 0.3)' 
          }); 
          loading.setText('保存中...') 

          let paramData = {
            variables:{
              TEST5: 'DB1,INT104',  
              TEST6: 'DB1,INT102',  
              TEST7: 'DB1,INT100',  
              // TEST8: 'DB1,INT108',    
              // TEST9: 'DB1,INT108',    
            }, 
            items:['TEST5','TEST6','TEST7'],
            values:[
              +this.warnForm.few, 
              +this.warnForm.many,
              +this.warnForm.error,
              // +this.warnForm.warnTime, 
              // +this.warnForm.width, 
            ],
            type:'warn'
          }
          this.saveS7(paramData,loading) 
        }
      })
    },
    saveS7(e,loading){ 
      console.log(e)
      try{
        var conn = new nodes7(); 
        let PLCInfo = this.clientList.filter(item => item.type_dv === 2)
        let PLCIP = PLCInfo[0].ip_dv 
        conn.initiateConnection({ port: 102, host: PLCIP, rack: 0, slot: 1, localTSAP: 4096, remoteTSAP: 768, timeout: 5000, doNotOptimize: false}, connected); // slot 2 for 300/400, slot 1 for 1200/1500, change debug to true to get more info
        function connected(err) {
          if (typeof(err) !== "undefined") { 
            console.log(err); 
          }
          var variables = e.variables
          var items = e.items
          var valuesArr = e.values
          var type = e.type
          conn.setTranslationCB(function(tag) { return variables[tag]; });
          conn.addItems(items);
          conn.writeItems(items, valuesArr, (err, values)=>{
            console.log(e,err, values,'写入数据')
            loading?.close()
            if(!err){
              ElMessage({
                type: 'success',
                message: '保存成功',
                duration: 2000,
              })
              if(type === 'warn'){ 
                localStorage.setItem('warnForm', JSON.stringify(that.warnForm))
              }
              if(type === 'PLC'){  
                localStorage.setItem('PLCForm', JSON.stringify(that.PLCForm))
              }
            }else{
              ElMessage({
                type: 'error',
                message: 's7写入PLC失败',
                duration: 2000,
              })
            }
          }); 
          conn.dropConnection((err, values)=>{
            console.log(err, values,'断开')
          });
        }
      }catch(e){
        loading?.close()
        ElMessage({
            type: 'error',
            message: '保存失败',
            duration: 2000,
          })
      }
    },

  

  

posted @ 2025-04-24 17:33  福超  阅读(77)  评论(0)    收藏  举报