How to read from a serial port in lua

后端 未结 2 941
清歌不尽
清歌不尽 2021-01-18 03:40

I\'m new to lua and I\'m trying to receive data from the port, ttyACM0, I can write to the port by:

wserial = io.open(\"/dev/ttyACM0\",\"w\")
wserial:write(\         


        
2条回答
  •  庸人自扰
    2021-01-18 04:00

    I will not answer what you did wrong, but based one 1 answer here I did this for Windows environment:

    -- You can generate PowerShell script at run-time
    local script = [[
        $port= new-Object System.IO.Ports.SerialPort COM78,115200,None,8,one
        $port.open()
        $port.WriteLine('serialRequest')
        $port.ReadLine() > 'c:\\temp\\serialResponse.txt'
        $port.Close()
    ]]
    -- Now create powershell process and feed your script to its stdin
    local pipe = io.popen("powershell -command -", "w")
    pipe:write(script)
    pipe:close()
    

    Not very nice as response must be taken from file, but it works.

提交回复
热议问题