【问题标题】:Read and Write to Serial Port with Lua on Windows在 Windows 上使用 Lua 读写串行端口
【发布时间】:2019-07-03 08:54:23
【问题描述】:

我的总体计划目标是使用 Lua 创建一个更好的 3D 打印机校准 GUI,幸运的是我的 GUI 部分已经在工作。我正在尝试将 Windows 10 计算机上的 COM 端口读写到打印机的 Arduino。但是,我被串行通信难住了。目前,我有两条 FTDI 电缆连接在一起,并且可以使用 RealTerm(终端程序)在它们之间进行通信以进行测试,所以我知道接线是正确的。我正在使用 ZeroBrane Studio 进行开发,但还不习惯安装库。

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

  1. 尝试:嵌入powershell script to open the serial port

结果:端口没有数据出来,但没有产生错误

  1. 尝试:使用srdgame/librs232 library

结果:代码中的 require "rs232" 失败,因为它找不到文件。我将 SRC 的内容安装到与我的 Lua 代码相同的目录中,但很可能这不是正确的做法。

  1. 尝试:在 Lua 中使用原生 io 函数

结果:我能够使用这种方法发送数据,这是个好消息。但是,我看不到调整端口波特率的方法。进入设备管理器并修改设置没有效果。默认为 115200bps。

代码:

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

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

如果有任何解决方案需要库,我希望得到一些关于哪些文件去哪里的指导。

提前谢谢你!

PS:以下是我调查的一些进一步的参考资料。

  1. posix 似乎只适用于 linux

  2. lua-user.org Serial Communication wiki。我不明白说明,他们的recommended library 数据不足..

【问题讨论】:

  • io.open("COM5:","wb") 在 Windows 上会更正确
  • 设置com口参数:os.execute[[mode COM5: baud=9600 parity=N data=8 stop=1]]
  • 感谢推荐!我一直在浏览 Microsoft 在 System.IO.Ports 上的文档,但还没有找到读取 powershell 打印的任何值的方法。如果 Lua 调用了一个 powershell 脚本,并且 powershell 脚本进行了任何打印,那么它就会显示在 Lua 的控制台中。
  • 要读取程序的标准输出,请使用file=io.popen("program args", "rb"); s=file:read"*a"; file:close()
  • 太棒了!到目前为止,Powershell 一直运行良好。我唯一的问题是打开端口会删除之前发送的任何数据,并且我无法在一个脚本中打开端口(当用户启动程序时)然后再读取另一个(连续)。我已附上我的 powershell 脚本作为答案。为了避免这种情况,我尝试使用 io.read,但它总是读取 Lua 控制台。 fileIn = io.open("COM2:","rb"); io.input(fileIn); print(io.read("*line"));

标签: windows lua serial-port port communication


【解决方案1】:

我拥有的第一个可行的解决方案是使用 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发送的串口数据可能有空格。不幸的是,这些都被终端分成多个参数,因此powershell然后将它们重新组合成一个字符串。 其次,必须打开端口才能读取或写入数据。如果在打开之前发送了任何串行数据,则该数据将丢失。

遗留问题:我不能让端口保持打开状态,然后去 Lua 中做一些事情并定期检查端口是否有新数据。这是不幸的,因为我使用的硬件有时会在没有请求的情况下发送数据,或者需要很长时间才能回复所有数据(在迭代级别校准的情况下)。此外,每次我打开端口时,硬件都会重新启动。在这一点上,我没有好的解决方案。

这是我的修复尝试:创建三个单独的 powershell 脚本 #1) 打开端口 #2) 读取端口并在 500 毫秒内返回 nil 如果不存在数据,否则回复所有数据并 #3) 关闭它。不幸的是,即使在 #1 运行之后,#2 也会引发关于端口被关闭的错误。我很想听听一些想法,并很乐意用任何解决方案更新这个答案。

非常感谢 Egor 迄今为止提供的所有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-03
    • 2014-10-01
    • 2023-03-12
    • 2011-02-08
    相关资源
    最近更新 更多