Search code examples
c#luanlua

How to set C# SerialPort object using NLUA?


I'm initializing Lua in my app next way:

lua = new Lua();
lua.LoadCLRPackage();
lua["SerialPort"] = new SerialPort();
lua.DoFile("script.lua");

And add to project System.IO.Port to create SerialPort objects. So,my sctript containing next:

import ('Mynamespace')
import ('System.IO.Ports')
local myport=SerialPort("COM7",9600)

after exec I have an exception:attempt to call global 'SerialPort' (a userdata value)

What should I change to use next construction: SerialPort(String,Int 32) and get access to fields "BaudRate","PortName" SerialPort objects in my script?


Solution

  • I have add "local myport={}" before "myport=SerialPort" and its allowed me to set fields baud rate and port name.

    import ('ManipulatorGUI')
    local myport={}
    myport=SerialPort
    myport.BaudRate=9600
    myport.PortName="COM6"
    myport:Open()