How to set C# SerialPort object using NLUA?

120 views Asked by At

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?

2

There are 2 answers

0
infernalcucumber On BEST ANSWER

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()
0
Grilled Chipolatas On

This bit of code worked for me:

Ports = CLRPackage("System.IO.Ports", "System.IO.Ports")

port = Ports.SerialPort("COM5", 9600)


port.Open(port)

print(port.ReadTimeout)

port.WriteLine(port,"Hello");

port.Dispose(port)