读取和写入和Lua串行端口在Windows

问题描述 投票:0回答:1

我的总体方案的目标是使用Lua创造更好的3D打印机校准GUI和我很幸运有GUI部分已经在工作。我试图读取和写入到一个COM端口我的Windows 10的计算机打印机的Arduino上。不过,我对串行通信难倒。目前,我有两个FTDI电缆连接在一起,并使用RealTerm(终端程序)进行测试它们之间可以沟通,所以我知道的接线是否正确。我使用ZeroBrane Studio进行开发,但我尚未安装舒服库。

到目前为止,我曾尝试以下解决方案:

1)尝试:嵌入一个powershell script to open the serial port

结果:无数据出来的端口,但不会产生错误

2)尝试:使用srdgame/librs232 library

结果:需要“RS232”的代码失败,因为它无法找到该文件。我安装了SRC的内容复制到同一目录作为我的Lua代码,但它很可能是不这样做的正确方法。

3)尝试:在Lua结果使用本地IO功能:我可以用这个方法,它是个好消息要发送的数据。不过,我没有看到一个方法来调整端口的波特率。进入设备管理器,并设置出渣都没有效果。它默认为115200。

码:

file=io.open("COM5","w")
io.output(file)
io.write("Hello world")

其他选项:我已经安装了luarocks,但不能把它安装在命令提示符中的任何库。 “错误:无法找到预期的文件ffi.lib,或ffi.dll,或libffi.dll为FFI - 你可能在你的系统中安装FFI和/或设置FFI_DIR变量”

如果任何解决方案都需要的库,我会喜欢一些指导文件什么地方去。

先感谢您!

PS:这里有一些进一步的引用,我调查了。

3)posix似乎仅是为Linux

4)lua-user.org Serial Communication wiki。我不明白的指令及其recommended library超出数据..

windows lua serial-port port communication
1个回答
0
投票

第一个可行的解决方案我是使用PowerShell脚本。它发生在从Lua参数,包括COM端口,波特率,并写了一个字符串。

首先,这里的Lua中。

writeThenReadCOMinLua.lua

local comPort = "COM2"
local baud = "9600"
local dataToWrite = "Hello. Is Anyone There?"
--run the powershell script with supplied params. Spaces are important.
local file = io.popen("powershell.exe -file ./liblocal/psLibs/writeAndReadCOM.ps1 
"..comPort.. " " .. baud .. " " .. dataToWrite)

--Wait for a reply (indefinitely)
local rslt = file:read("*a")
print("Response: " .. rslt)

而且,PowerShell脚本编写然后等待回复。

writeAndReadCOM.ps1

$nargs = $args.Count #args is the list of input arguments
$comPortName=$args[0] #This is the com port. It has zero spaces
$baud = $args[1] #this is the numberical baud rate
#the remainder of the arguments are processed below


#Combine argument 2,3,...,n with a space because of command prompt shortfalls to pass arguments with spaces
$dataToWrite = ""
For ($i=2; $i -le $nargs ; $i++) {
    $dataToWrite = "$($dataToWrite) $($args[$i])"
}

#$port= new-Object System.IO.Ports.SerialPort COM2,9600,None,8,one
$port= new-Object System.IO.Ports.SerialPort $comPortName,$baud,None,8,one

#open port if it's not yet open
IF ($port.IsOpen) {
    #already open
} ELSE {
    #open port
    $port.Open()
}

#write the data
$port.WriteLine($dataToWrite)

#wait for a response (must end in newline). This removes the need to have a hard coded delay
$line = $port.ReadLine()
Write-Host $line #send read data out

#if the response was multiple lines, then read the rest of the buffer. If the port was just opened.
while ($port.BytesToRead -ne 0) {
    $dataReturned = 1
    $line = $port.ReadLine()
    Write-Host $line #send read data out for the remainder of the buffer
}

$port.Close()

#IF ($dataReturned -eq 0) {'PS_NO_BYTES_TO_READ'}

有几件事情怎么回事。首先,LUA发送的串行数据可能有空格。不幸的是,这些全部由终端获得分成多个ARGS,所以后来PowerShell在,并将它们重新组合成一个字符串。其次,端口必须打开来读取或写入数据。如果被打开,在它发送的任何串行数据,该数据会丢失。

剩余的问题:我不能离开打开的端口,然后去做的东西在Lua和定期检查端口的新数据。这是不幸的,因为我使用的硬件有时发送数据而不的请求,或需要很长的时间与所有的数据回复(在迭代级别校准的情况下)。此外,每个我打开端口时,硬件重新启动。在这一点上,我也没有很好的解决办法。

这是我在尝试修复:创建三个独立的PowerShell脚本#1)开放的端口#2)读的是端口和返回零500ms内如果没有数据存在,否则所有的数据和#3)关闭答复。不幸的是,#2抛出约1#运行后连口被关闭的错误。我很乐意听到的一些想法,并会很乐意更新这个答案有任何解决方案。

非常感谢叶戈尔所有帮助至今。

© www.soinside.com 2019 - 2024. All rights reserved.