使用Go而不是Python在Matlab中使用DLL。

huangapple go评论123阅读模式
英文:

Using Matlab DLL in Go instead Python

问题

我正在使用由Matlab Simulink/Codegen生成的DLL模型。

这段代码在Python中运行得很好:

  1. import ctypes
  2. real_T = ctypes.c_double
  3. # 导入DLL
  4. dll = ctypes.windll.LoadLibrary("./simulink.dll")
  5. # 模拟
  6. dll.initialize() # 初始化
  7. for i in range(10):
  8. mdlInput = real_T.in_dll(dll, "my_custom_input")
  9. mdlInput.value = i
  10. # 步进
  11. dll.step() # 步进
  12. # 获取输出
  13. mdlOutput = real_T.in_dll(dll, "my_custom_output")
  14. print(mdlOutput.value)
  15. dll.terminate() # 结束模拟

我只想将上面的代码翻译成Go语言以提高性能。问题是syscall.syscall不识别real_T (C.Double)类型,无法返回double而不是指针。

在Go语言中如何设置"my_custom_input"的值并读取"my_custom_output"的值?

英文:

I'm using a DLL model generated by Matlab Simulink/Codegen.

The code works pretty well in Python:

  1. import ctypes
  2. real_T = ctypes.c_double
  3. # Importa a DLL
  4. dll = ctypes.windll.LoadLibrary("./simulink.dll")
  5. # Simulate
  6. dll.initialize() # Init
  7. for i in range(10):
  8. mdlInput = (real_T).in_dll(dll, "my_custom_input")
  9. mdlInput.value = i
  10. # Step
  11. dll.step() # step
  12. # get output
  13. mdlOutput = (real_T).in_dll(dll, "my_custom_output")
  14. print(mdlOutput.value)
  15. dll.terminate() # end of simulation

I just want to translate the code above to Go to increase performance. The problem is that syscall.syscall does not recognize the real_T (C.Double) type to return a double instead of a pointer.

How do I set the "my_custom_input" value and read the "my_custom_output" value in Go?

答案1

得分: 1

刚刚发现了一种使用LazyDll的新解决方案。
完整的代码如下:

  1. package main
  2. import (
  3. "log"
  4. "syscall"
  5. "unsafe"
  6. )
  7. func main() {
  8. dll := syscall.NewLazyDLL(".\\simulink.dll") // 使用LazyDll加载DLL
  9. dll.NewProc("initialize").Call() // 调用DLL中的initialize方法
  10. // 现在获取自定义输入的指针
  11. customInput := (*float64)(unsafe.Pointer(dll.NewProc("my_custom_input").Addr()))
  12. // 更新自定义输入的值
  13. *customInput = 1.23
  14. // 调用DLL中的step方法
  15. dll.NewProc("step").Call() // 调用DLL中的step方法
  16. // 现在获取自定义输出的指针并显示其值
  17. customOutput := (*float64)(unsafe.Pointer(dll.NewProc("my_custom_output").Addr()))
  18. // 打印输出的值
  19. log.Print(*customOutput)
  20. // 终止DLL的执行
  21. dll.NewProc("terminate").Call() // 调用DLL中的terminate方法
  22. }

以上是要翻译的内容。

英文:

Just found a new solution using LazyDll.
The complete code as:

  1. package main
  2. import (
  3. "log"
  4. "syscall"
  5. "unsafe"
  6. )
  7. func main() {
  8. dll = syscall.NewLazyDLL(".\\simulink.dll") // Load DLL as LazyDll
  9. dll.NewProc("initialize").Call() // Call initialize method from dll
  10. // Now get pointer to custom input
  11. customInput := (*float64)(unsafe.Pointer(dll.NewProc("my_custom_input").Addr()))
  12. // Update custom Input Value
  13. *customInput := 1.23
  14. // Call the step method from dll
  15. dll.NewProc("step").Call() // Call step method from dll
  16. // Now get pointer to custom output and show it's value
  17. customOutput := (*float64)(unsafe.Pointer(dll.NewProc("my_custom_output").Addr()))
  18. // Print the value of output
  19. log.Print(*customOutput)
  20. // Terminate the dll execution
  21. dll.NewProc("terminate").Call() // Call terminate method from dll
  22. }

huangapple
  • 本文由 发表于 2023年3月8日 00:26:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75664650.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定