Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
File io 如何在lua中读取串行端口_File Io_Lua_Serial Port - Fatal编程技术网

File io 如何在lua中读取串行端口

File io 如何在lua中读取串行端口,file-io,lua,serial-port,File Io,Lua,Serial Port,我是lua的新手,正在尝试从端口ttyACM0接收数据,我可以通过以下方式写入端口: wserial = io.open("/dev/ttyACM0","w") wserial:write("hellloooo") wserial:flush() 我想既然我可以像写文件一样写它,我就可以像读文件一样读它。但是,当我试图阅读它(使用下面的代码)时,我只是在一个无限循环中结束 rserial=io.open("/dev/ttyACM0","r") while chaine==nil do ch

我是lua的新手,正在尝试从端口ttyACM0接收数据,我可以通过以下方式写入端口:

wserial = io.open("/dev/ttyACM0","w")
wserial:write("hellloooo")
wserial:flush()
我想既然我可以像写文件一样写它,我就可以像读文件一样读它。但是,当我试图阅读它(使用下面的代码)时,我只是在一个无限循环中结束

rserial=io.open("/dev/ttyACM0","r")
while chaine==nil do
  chaine=rserial:read()
  rserial:flush()
end
print(chaine)

因此,我的问题是我做错了什么,我如何读取端口ttyACM0?

这里有两种行缓冲:一种在C库中,您可以避免使用
:读取(1)
,如注释中提到的@Advert,另一种在终端驱动程序本身中。您可以使用
stty
命令行实用程序(
stty-F/dev/ttyACM0-icanon
)或例如使用(未测试的代码)禁用终端驱动程序中的输入行缓冲:


Lua中还有一个用于处理串行端口的选项,最新未发布版本的具有用于处理串行端口的代码(但在默认版本中已禁用)。

我不会回答您的错误,但基于一个1回答,我为Windows环境做了以下操作:

-- 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()

不是很好,因为必须从文件中获取响应,但它可以工作。

使用
io.read()
将一直读取,直到收到换行符为止。也许你不会寄任何东西给它?看看这个(大约在中途,有一些关于
io.read
)。我会尝试
:read(“*all”)
,或者,如果您知道消息的长度,
:read(10)
(10个字符)。您可以随时执行
:读取(1)
以获得一个字节,然后决定是否需要更多。感谢您的回复,我尝试过它不起作用。你还有其他建议吗?嗯,检查一下你有什么收获怎么样
cat/dev/ttyACM0
好的,所以我尝试了我能想到的一切,但你是对的,当我执行cat/dev/ttyACM0时,我什么都没有得到,但我尝试了用python读写文件,并且我可以成功地读取该文件。所以我想知道我是否可以将python与lua集成?它说tcgetattr得到一个nil值。@lana:“得到一个nil值”是什么意思?Is
fd
nil
(由于
assert(p.fileno(rserial))
,这不应该发生在代码中,
p.tcgetattr(fd)
返回
nil(如果是
assert`还应该给出
tcgetattr
失败的具体原因),或者Is
p.tcgetattr
nil(在这种情况下,您的LuapoFix模块太旧了)?我得到的错误是:
lua:trial.lua:18:尝试调用字段“tcgetattr”(一个空值)堆栈回溯:trial.lua:18:在主块中[C]:?
第18行是
本地保存的\u tcattr=assert(p.tcgetattr(fd))
@lana:Ok,这是第3种情况(LuapoFix模块太旧了)。您需要从今年5月开始安装版本32。答案中已经有指向github存储库的链接。不过,我建议您安装Lua模块。
-- 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()