user-defined-functions - Xlwings + Pygatt:将对象从 Pygatt 存储到 Excel VBA
问题描述
我能够使用 xlwings 在 VBA 代码和 Python UDF 之间发送接收数据。
我在 Python UDF 中使用 Pygatt 与 BLE 设备进行通信。
我将信息存储在 Excel 中的一张工作表中,并根据需要在 Python UDF 中访问它。
我的问题是,我无法存储适配器的值并在另一个 UDF 中使用:-
adapter = pygatt.BGAPIBackend(serial_port=str(comPort))
adapter.start()
我的想法是将适配器值存储在 Excel 工作表中的一个单元格中,然后在另一个 UDF 中访问它。
以下是我的UDF:-
#Function to open BLE connection based on the comport number
@xw.func
@xw.sub
def ComPortSelect(strP):
wb = xw.Book.caller()
comPort = str(strP)
comPort = 'COM'+comPort
adapter = pygatt.BGAPIBackend(serial_port=str(comPort))
adapter.start()
listOfDevices = adapter.scan()
wb.sheets['Sheet1'].range('A1').value = adapter **<----- Gives error**
#Function to use the *adapter* and execute other Pygatt functions
@xw.func
@xw.sub
def ConnectToSelectedDevice(device):
adapter = wb.sheets['Sheet1'].range('A1').value **<--**
deviceHandle = adapter.connect(device)
.
.
.
我如何存储所需的对象并在其他 UDF 中使用它?有没有其他正确的方法来实现这一目标?
非常感谢提前!!!
解决方案
我使用全局变量并在 Python UDF 之间共享它。
我继续使用这个解决方案。
推荐阅读
- java - 保护包名称免受攻击
- android - 如何确定哪个属性会更改 AlertDialog 内弹出菜单的文本颜色?
- php - 如何使用 php 版本 8 安装 laravel 7
- visual-studio - VS2019 给了我这个:未解析的外部符号 _main 在函数“int __cdecl invoke_main(void)”(?invoke_main@@YAHXZ) 中引用
- azure - Azure SQL 数据库从 Azure 标准订阅转移到开发/测试订阅
- python - 带有数据库数据的 Django 动态 URL
- database - 在 Joomla 域上运行 WP 子文件夹
- c# - 如何使用 Windows 身份验证在 ASP.NET Core 5 中操作声明
- python - 即使提供了超时,Python 请求模块也会卡住很长时间
- angular - 如何使用 ngx-mask 屏蔽输入“(500)-000-0000”?