【问题标题】:Convert code from 'hard-coding' to 'soft-coding'将代码从“硬编码”转换为“软编码”
【发布时间】:2016-01-25 11:28:50
【问题描述】:

我正在为 PROFINET 设备配置 DCP 模块并为此使用 Lua。我的硬编码代码如下所示:

function dcp:setname()
    local pkt = CreateFromPath("ethernet/profinet/dcp/dcp_block_nameofstation")
    pkt.src.from_string(tc.Mac)
    pkt.dst.from_string("00:a0:45:38:20:ec")
    pkt.get_layer("profinet").frameid.from_string("0xfefd")
    local d = pkt.get_layer("dcp")
    local xid = tostring(rand()) 
    d.service_id.from_string("4")
    d.xid.from_string(xid)

    d.service_type.from_string("0")

    d.get_layer("dcp_block_nameofstation").Option.from_string("2")
    d.get_layer("dcp_block_nameofstation").SubOption.from_string("2")

    d.get_layer("dcp_block_nameofstation").BlockInfo .from_string("1")
    d.get_layer("dcp_block_nameofstation").NameOfStation.from_string("test-device")
end

我需要将“测试设备”作为参数传递,它是一个字符串。我该怎么做?

【问题讨论】:

    标签: c function parameters lua arguments


    【解决方案1】:

    在函数定义中添加参数:

    function dcp:setname(nameOfStation)
    

    并使用它:

    d.get_layer("dcp_block_nameofstation").NameOfStation.from_string(nameOfStation)
    

    然后,调用函数,传递一个值:

    myDcp:setname("test-device")
    

    【讨论】:

      猜你喜欢
      • 2015-03-18
      • 1970-01-01
      • 2013-01-22
      • 2014-09-01
      • 2023-03-24
      • 2020-11-04
      • 2011-05-04
      • 2013-04-20
      • 1970-01-01
      相关资源
      最近更新 更多