RE:从putty到Excel VBA的I / O重定向 [英] RE: I/O redirection from putty to Excel VBA
问题描述
大家好
我使用putty通过串行线与设备通信。我想要做的是在EXCEL vba中实现它。
我有一些vba开发的经验,但这看起来有点复杂!
我已经能够通过shell(..)命令启动putty并打开com端口。我还发现我必须将std I / O从putty重定向到vba。这就是我被困住的地方,我在网上找到的东西到目前为止还没有成功。感谢任何帮助。
谢谢
下面的代码调用ping命令并执行IO to excel(txtSaida控制):
选项显式
私有声明函数CreatePipe Lib" kernel32" (_
phReadPipe As Long,_
phWritePipe As Long,_
lpPipeAttributes As Any,_
ByVal nSize As Long)As Long
Private Declare函数ReadFile Lib" kernel32" (_
ByVal hFile As Long,_
ByVal lpBuffer As String,_
ByVal nNumberOfBytesToRead As Long,_
lpNumberOfBytesRead As Long,_
ByVal lpOverlapped As Any)作为Long
私有声明函数CreateProcessA Lib" kernel32" (_
ByVal lpApplicationName As Long,_
ByVal lpCommandLine As String,_
lpProcessAttributes As Any,_
lpThreadAttributes As Any,_
ByVal bInheritHandles As Long,_
ByVal dwCreationFlags As Long,_
ByVal lpEnvironment As Long,_
ByVal lpCurrentDirectory As Long,_
lpStartupInfo作为STARTUPINFO,_
lpProcessInformation As PROCESS_INFORMATION)As Long
私有声明函数CloseHandle Lib" kernel32" (ByVal hHandle As Long)As
私有类型SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type
私有类型STARTUPINFO
cb As Long
lpReserved As Long
lpDesktop As Long
lpTitle As Long
dwX As Long
dwY As Long
dwXSize As Long
dwYSize As Long
dwXCountChars As Long
dwYCountChars As Long
dwFillAttribute As Long
dwFlags As Long
wShowWindow As Integer
cbReserved2 As Integer
lpReserved2 As Long
hStdInput As Long
hStdOutput As Long
hStdError As Long
End Type
Private Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessID As Long
dwThreadID As Long
End Type
Private Const NORMAL_PRIORITY_CLASS =& H20&
Private Const STARTF_USESTDHANDLES =& H100&
Private Const STARTF_USESHOWWINDOW =& H1
Private BufferRecebido As String
公共函数ExecutarComando(可选szComando As String)As String
Dim pi作为PROCESS_INFORMATION
Dim si作为STARTUPINFO
Dim sa作为SECURITY_ATTRIBUTES
Dim lRetorno As Long,hPipeLeitura As Long
Dim hPipeEscrita As Long
Dim lBytesLidos As Long
Dim Buffer As String * 256
sa.nLength = Len(sa)
sa.bInheritHandle = 1&
sa.lpSecurityDescriptor = 0&
lRetorno = CreatePipe(hPipeLeitura,hPipeEscrita,sa,0)
si.cb = Len(si)
si.dwFlags = STARTF_USESTDHANDLES或STARTF_USESHOWWINDOW
si.hStdOutput = hPipeEscrita
si.hStdError = hPipeEscrita
lRetorno = CreateProcessA(0&,szComando,sa,sa,1&,NORMAL_PRIORITY_CLASS,0&,0&,si,pi)
CloseHandle hPipeEscrita
BufferRecebido = vbNullString
Do
lRetorno = ReadFile(hPipeLeitura,Buffer,256,lBytesLidos,0&)
BufferRecebido = BufferRecebido&左(Buffer,lBytesLidos)
循环而lRetorno<> 0
CloseHandle pi.hProcess:CloseHandle pi.hThread:CloseHandle hPipeLeitura
ExecutarComando = BufferRecebido
End Function
Private Sub UserForm_Initialize()
txtSaida.Text = ExecutarComando(" ping 8.8.8.8")
End Sub根据需要进行调整。
Hi all
I am using putty to communicate to a device over the serial line. What I would like to do is to implement this in EXCEL vba.
I have some experience of vba development but this looks a bit complicated!
I have been able to launch putty and open the com port through the shell(..) command. I have also found out that I have to redirect the std I/O from putty to vba. This is where I am stuck and what I have found online hasn't worked so far. Any help is appreciated.
Thanks
The code below call a ping command and retrive the IO to excel(txtSaida control):
Option Explicit Private Declare Function CreatePipe Lib "kernel32" ( _ phReadPipe As Long, _ phWritePipe As Long, _ lpPipeAttributes As Any, _ ByVal nSize As Long) As Long Private Declare Function ReadFile Lib "kernel32" ( _ ByVal hFile As Long, _ ByVal lpBuffer As String, _ ByVal nNumberOfBytesToRead As Long, _ lpNumberOfBytesRead As Long, _ ByVal lpOverlapped As Any) As Long Private Declare Function CreateProcessA Lib "kernel32" ( _ ByVal lpApplicationName As Long, _ ByVal lpCommandLine As String, _ lpProcessAttributes As Any, _ lpThreadAttributes As Any, _ ByVal bInheritHandles As Long, _ ByVal dwCreationFlags As Long, _ ByVal lpEnvironment As Long, _ ByVal lpCurrentDirectory As Long, _ lpStartupInfo As STARTUPINFO, _ lpProcessInformation As PROCESS_INFORMATION) As Long Private Declare Function CloseHandle Lib "kernel32" (ByVal hHandle As Long) As Long Private Type SECURITY_ATTRIBUTES nLength As Long lpSecurityDescriptor As Long bInheritHandle As Long End Type Private Type STARTUPINFO cb As Long lpReserved As Long lpDesktop As Long lpTitle As Long dwX As Long dwY As Long dwXSize As Long dwYSize As Long dwXCountChars As Long dwYCountChars As Long dwFillAttribute As Long dwFlags As Long wShowWindow As Integer cbReserved2 As Integer lpReserved2 As Long hStdInput As Long hStdOutput As Long hStdError As Long End Type Private Type PROCESS_INFORMATION hProcess As Long hThread As Long dwProcessID As Long dwThreadID As Long End Type Private Const NORMAL_PRIORITY_CLASS = &H20& Private Const STARTF_USESTDHANDLES = &H100& Private Const STARTF_USESHOWWINDOW = &H1 Private BufferRecebido As String Public Function ExecutarComando(Optional szComando As String) As String Dim pi As PROCESS_INFORMATION Dim si As STARTUPINFO Dim sa As SECURITY_ATTRIBUTES Dim lRetorno As Long, hPipeLeitura As Long Dim hPipeEscrita As Long Dim lBytesLidos As Long Dim Buffer As String * 256 sa.nLength = Len(sa) sa.bInheritHandle = 1& sa.lpSecurityDescriptor = 0& lRetorno = CreatePipe(hPipeLeitura, hPipeEscrita, sa, 0) si.cb = Len(si) si.dwFlags = STARTF_USESTDHANDLES Or STARTF_USESHOWWINDOW si.hStdOutput = hPipeEscrita si.hStdError = hPipeEscrita lRetorno = CreateProcessA(0&, szComando, sa, sa, 1&, NORMAL_PRIORITY_CLASS, 0&, 0&, si, pi) CloseHandle hPipeEscrita BufferRecebido = vbNullString Do lRetorno = ReadFile(hPipeLeitura, Buffer, 256, lBytesLidos, 0&) BufferRecebido = BufferRecebido & Left(Buffer, lBytesLidos) Loop While lRetorno <> 0 CloseHandle pi.hProcess: CloseHandle pi.hThread: CloseHandle hPipeLeitura ExecutarComando = BufferRecebido End Function Private Sub UserForm_Initialize() txtSaida.Text = ExecutarComando("ping 8.8.8.8") End SubAdapt as needs.
这篇关于RE:从putty到Excel VBA的I / O重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!