【问题标题】:Send string variable via TCP connection Lua通过 TCP 连接 Lua 发送字符串变量
【发布时间】:2016-10-11 18:00:29
【问题描述】:

我在 Lua 中每 10 秒通过 TCP 连接在两个 ESP8266 模块之间发送数据:

string="abc"
cl=net.createConnection(net.TCP, 0)
cl:connect(80,"192.168.4.1")
tmr.alarm(2, 10000, 1, function()
cl.send("The string variable is: "..string.."")end)

但是,如果我想像上面的代码一样发送字符串变量,我会不断收到错误消息:

PANIC: unprotected error in call to Lua API (init.lua:26: bad argument #1 to 'send' (net.socket expected, got string))
PANIC: unprotected error in call to Lua API (bad argument #1 (Server/Socket expected))

它仅在发送数值变量时对我有用。有没有办法发送字符串变量?

谢谢, 柿子

【问题讨论】:

    标签: lua esp8266 nodemcu


    【解决方案1】:

    错误信息是send 调用的第一个参数应该是套接字,而不是字符串。

    您应该使用cl:send("value") 而不是cl.send("value"),因为第一个实际上是cl.send(cl, "value") 的缩写形式。

    【讨论】:

    • 非常感谢您的明确回答,它对我帮助很大。现在它可以工作了:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-25
    • 2016-09-20
    • 2020-02-17
    • 2013-02-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多