【发布时间】:2017-11-01 18:08:35
【问题描述】:
我有一个使用 Python 绑定通过 Firmata 协议连接到 arduino 的中继板。使用 pyfirmata (https://github.com/tino/pyFirmata) 进行通信没有问题。
relaisboard 有 16 个 relais。每组 3 个继电器为一个通道。每个通道都连接到被测设备的输入或输出。这只是为了粗略描述一下releboard的目的。
您可以在下面找到代码的骨架。
#!/usr/bin/env python
__version__ = '0.1'
# Fault Injection Unit
# Power is connected to Fault Bus 1
# Ground is connected to Fault Bus 2
from pyfirmata import Arduino
class FaultInsertionBoard(object):
def __init__ (self, comPort = 'COM3'):
"""Initalize the Fault insertion Board
Open communication with host via serial port
Arguments:
comPort -- The serial port used to connect the board to the host.
"""
self.board = Arduino(comPort)
class Channel(object):
def __init__ (self, aChannel):
""" Create a Channel"""
pass
def NoFault():
""" Set the channel to the "No fault" condition
No Fault condition is:
-- DUT channel connected to the testing sistem
-- DUT channel disconnected from the Fault bus 1
-- DUT channel disconnected from the Fault bus 2
"""
pass
def OpenCircuit():
""" Set the channel to the "Open Circuit fault" condition
Open Circuit fault condition is:
-- DUT channel disconnected from the testing sistem
-- DUT channel disconnected from the Fault bus 1
-- DUT channel disconnected from the Fault bus 2
"""
pass
def ShortToGround():
""" Set the channel to the "Short to Ground fault" condition
Open Circuit fault condition is:
-- DUT channel disconnected from the testing sistem
-- DUT channel disconnected from the Fault bus 1
-- DUT channel connected to the Fault bus 2
"""
pass
def ShortToPower():
""" Set the channel to the "Short to Ground fault" condition
Open Circuit fault condition is:
-- DUT channel disconnected from the testing sistem: channel relay is open
-- DUT channel connected to the Fault bus 1: Fault Bus 1 relay is closed
-- DUT channel disconnected from the Fault bus 2: Fault Bus 1 relay is open
"""
pass
def main():
FaultBoard = FaultInsertionBoard('COM3')
VoutSensor = FaultBoard.Channel(0)
IOutSensor = FaultBoard.Channel(1)
VoutSensor.NoFault()
IOutSensor.NoFault()
VoutSensor.ShortToGround()
IOutSensor.ShortToPower()
if __name__ == "__main__":
main()
地点:
-
FaultInsertionBoard是Arduino类的简单包装器Firmata。 -
Channel(n)标识n-th 组三个继电器 -
NoFault、ShortToPower、ShortToGround是各种配置 每个通道的三个继电器(与实际无关) 配置)。
现在的问题是:我对用 C 编写的嵌入式固件有很好的体验,而用 Python 编写的固件要少得多。显然上面的代码是不正确的。
有人可以建议我一个类框架来获得上述功能吗?换句话说,如何编写 Python 代码来驱动上述继电器?
PS:或者我可以这样写:
FaultBoard = FaultInsertionBoard('COM3')
FaultBoard.Channel(0).NoFault()
但我认为它不那么优雅和清晰。
【问题讨论】:
-
你没有显示
FaultInsertionBoard的代码;您希望如何获得帮助? -
你贴的代码是不是不能用?
-
对不起,我的错。我以为我放了代码。请参阅上面我编辑的问题。
-
不要定义嵌套类。它在 Python 中毫无用处。如果这只是一个缩进错误,请修复它。
-
所以您要求我们为您实现您的课程?