Search code examples
luauartnodemcu

How to turn off lua interpeter for uart? (Nodemcu)


I need to communicate with a special device on uart. The device is queried in every 500msec, and its response is stored in a local variable.

Here is a minimal working example:

my_data = "no data yet"

function devReq()
    uart.write(0, "value?\n\r") -- request next value from device
end

function devStart()
    uart.alt(0) 
    uart.setup(0, 2400, 7, uart.PARITY_NONE, uart.STOPBITS_2, 0)
    uart.on("data", "\r", 
        function(data)
            my_data=data                -- store value
            tmr.alarm(1, 500, tmr.ALARM_SINGLE, devReq, 0) -- request next value
        end
    )
    devReq() -- request first value 
end

This code works. I can connect do the uart port with putty. My problem is that the answer sent back by the device is also interpreted by lua:

> value?

423
stdin:1: unexpected symbol near '423'

> value?

2345
stdin:1: unexpected symbol near '2345'

> value?

So in addition of storing the value in my_data, the data is also interpreted by lua. How can I turn that off?


Solution

  • See uart.on(). It's the fourth parameter. You've omitted this so it defaults to 1 -- that is pass the input to the interpreter.