2016-01-25 76 views
0

我正在为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 

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

回答

1

的参数添加到函数定义:

function dcp:setname(nameOfStation) 

并使用它:

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

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

myDcp:setname("test-device")