在 Windows 上使用 Lua 读取和写入串行端口
Read and Write to Serial Port with Lua on Windows
我的总体计划目标是使用 Lua 创建一个更好的 3D 打印机校准 GUI,幸运的是我的 GUI 部分已经可以工作了。我正在尝试将 Windows 10 计算机上的 COM 端口读写到打印机的 Arduino。但是,我被串行通信难住了。目前,我有两条连接在一起的 FTDI 电缆,可以使用 RealTerm(一个终端程序)在它们之间进行通信以进行测试,所以我知道接线是正确的。我正在使用 ZeroBrane Studio 进行开发,但还不习惯安装库。
到目前为止,我尝试了以下解决方案:
结果:没有数据从端口出来,但没有产生错误
结果:代码中要求“rs232”失败,因为它找不到文件。我将 SRC 的内容安装到与我的 Lua 代码相同的目录中,但是这可能不是正确的方法。
- 尝试:在 Lua
中使用本机 io 函数
结果:我能够使用这种方法发送数据,这是个好消息。但是,我没有看到调整端口波特率的方法。进入设备管理器并修改设置没有任何效果。默认为 115200bps。
代码:
file = io.open("COM5","wb")
io.output(file)
io.write("Hello world!")
其他选项:
我已经安装了 luarocks,但无法让它在命令提示符下安装任何库。 “错误:找不到 FFI 的预期文件 ffi.lib、ffi.dll 或 libffi.dll——您可能必须在系统 and/or 中安装 FFI 设置 FFI_DIR变量
如果有任何解决方案需要库,我希望得到一些关于文件放在哪里的指导。
提前致谢!
PS:这是我调查过的一些进一步的参考资料。
posix 似乎只适用于 linux
lua-user.org Serial Communication wiki. I did not understand the instructions and their recommended library 数据不足..
我的第一个可行解决方案是使用 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 毫秒内 return nil 如果不存在数据,否则回复所有数据和#3)关闭它。不幸的是,即使在#1 为 运行 之后,#2 也会抛出有关端口被关闭的错误。我很想听听一些想法,并且很乐意用任何解决方案更新这个答案。
非常感谢 Egor 迄今为止提供的所有帮助。
我的总体计划目标是使用 Lua 创建一个更好的 3D 打印机校准 GUI,幸运的是我的 GUI 部分已经可以工作了。我正在尝试将 Windows 10 计算机上的 COM 端口读写到打印机的 Arduino。但是,我被串行通信难住了。目前,我有两条连接在一起的 FTDI 电缆,可以使用 RealTerm(一个终端程序)在它们之间进行通信以进行测试,所以我知道接线是正确的。我正在使用 ZeroBrane Studio 进行开发,但还不习惯安装库。
到目前为止,我尝试了以下解决方案:
结果:没有数据从端口出来,但没有产生错误
结果:代码中要求“rs232”失败,因为它找不到文件。我将 SRC 的内容安装到与我的 Lua 代码相同的目录中,但是这可能不是正确的方法。
- 尝试:在 Lua 中使用本机 io 函数
结果:我能够使用这种方法发送数据,这是个好消息。但是,我没有看到调整端口波特率的方法。进入设备管理器并修改设置没有任何效果。默认为 115200bps。
代码:
file = io.open("COM5","wb")
io.output(file)
io.write("Hello world!")
其他选项: 我已经安装了 luarocks,但无法让它在命令提示符下安装任何库。 “错误:找不到 FFI 的预期文件 ffi.lib、ffi.dll 或 libffi.dll——您可能必须在系统 and/or 中安装 FFI 设置 FFI_DIR变量
如果有任何解决方案需要库,我希望得到一些关于文件放在哪里的指导。
提前致谢!
PS:这是我调查过的一些进一步的参考资料。
posix 似乎只适用于 linux
lua-user.org Serial Communication wiki. I did not understand the instructions and their recommended library 数据不足..
我的第一个可行解决方案是使用 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 毫秒内 return nil 如果不存在数据,否则回复所有数据和#3)关闭它。不幸的是,即使在#1 为 运行 之后,#2 也会抛出有关端口被关闭的错误。我很想听听一些想法,并且很乐意用任何解决方案更新这个答案。
非常感谢 Egor 迄今为止提供的所有帮助。