首页 文章

通过TCP连接Lua发送字符串变量

提问于
浏览
2

我在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))

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

谢谢,Kaki

1 回答

  • 3

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

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

相关问题