首页 文章

如何在我们手动停止之前使用Raspberry Pi运行python脚本

提问于
浏览
0

目前我正在与Raspberry Pi和Arduino合作 . 对于某些情况下的Arduino,如果我们开始运行脚本,那么它将一直运行直到我们手动停止它们 .

我想知道在使用Python时是否有办法对Raspberry Pi做同样的事情 . 对于Raspberry Pi,当我使用时,

sudo python myprogramme.py

它只运行一次我的代码,然后停止 . 有没有办法我们可以用命令行多次运行相同的代码,直到我们手动停止它们为止(例如,在MATLAB中,我们必须使用crtl z来停止运行的脚本)? (这可能是通过使用循环但我想知道我们是否可以在不使用循环的情况下这样做 . )希望我的查询有意义 . 我这样做的目的是将传感器的连续信息发送到我的系统 .

2 回答

  • 0

    根据你的评论...当你点击CTRL C时,脚本会获得键盘中断,你可以正常关闭 .

    你的代码:

    import smbus
    import time
    
    while True:
        try:
            # Get I2C bus
            bus = smbus.SMBus(1)
    
            # BMP280 address, 0x76(118)
            # Read data back from 0x88(136), 24 bytes
            b1 = bus.read_i2c_block_data(0x76, 0x88, 24)
            # ... and the rest of your code. 
            # add a short sleep here at the end...
            sleep(0.1)
        except KeyboardInterrupt:
        # quit
            sys.exit()
    
  • 0

    您可以将代码放在while语句中

    while True
        <your logic here>
    

    这将永远运行,直到你点击ctrl-C

    或者你可以做

    my_bool = True
    while my_bool
        <your logic here>
        my_bool = <check for input>
    

相关问题