繁体   English   中英

从3个ADC读取数据的同时接收Python中的UDP消息

[英]Receiving UDP messages in Python while reading data from 3 ADCs

嗨,我是新来的,Python也新来的。 我正在尝试让Raspberry Pi控制按摩浴缸的温度,同时能够手动和远程(使用UDP)覆盖其“决定”。 基本上,我有3个AD转换器通过GPIO将数据发送到我的RPi,该GPIO包括太阳能加热器的温度,太阳光的数量和Spa温度,这将自动控制spa泵。 我有2条代码,它们彼此独立运行良好:

我可以使用以下代码每30秒读取一次ADC :(在代码的前面定义了read_bottom_sensor和read_top_sensor作为SPI位冲击的一部分)

while True:
        bottom_sensor = read_bottom_sensor(bottom_sensor_adc, SPICLK, SPIMOSI, SPIMISO, SPICS)
        current_spa_temp = bottom_sensor
        if DEBUG:
                print "Spa Temp = ", current_spa_temp



        top_sensor = read_top_sensor(top_sensor_adc, SPICLK, SPIMOSI, SPIMISO, SPICS)
        current_solar_heater_temp = top_sensor
        if DEBUG:
                print "Solar Heater Temp = ", current_solar_heater_temp



        if bottom_sensor + 10 < top_sensor:
                GPIO.output(PUMPRLY, True)
                print "The Pump is ON"
        else:
                GPIO.output(PUMPRLY, False)
                print "The Pump is OFF"
        time.sleep(30)

我还可以使用以下代码通过UDP通过手机打开/关闭连接到GPIO引脚#7的泵继电器(PUMPRLY):

while True:
    data, addr = sock.recvfrom(64)

    if data == b'7H':
        GPIO.output(7, True)

    elif data == b'7L':
        GPIO.output(7, False)

到现在为止还挺好。 问题是,当我将2组合在一起时,ADC的采样停止并等待接收任何UDP,然后才继续向下处理代码。 即我的代码停在看到“数据,addr = sock.recvfrom(64)”语句的位置。

我是否需要某种形式的多次中断,并且该如何执行才能使ADC采样每30秒继续进行,同时仍然能够独立接收UDP。 我认为它应该像这样简单:

while True:
        bottom_sensor = read_bottom_sensor(bottom_sensor_adc, SPICLK, SPIMOSI, SPIMISO, SPICS)
        current_spa_temp = bottom_sensor
        if DEBUG:
                print "Spa Temp = ", current_spa_temp



        top_sensor = read_top_sensor(top_sensor_adc, SPICLK, SPIMOSI, SPIMISO, SPICS)
        current_solar_heater_temp = top_sensor
        if DEBUG:
                print "Solar Heater Temp = ", current_solar_heater_temp



        if bottom_sensor < top_sensor + 10:
                GPIO.output(PUMPRLY, True)
                print "The Pump is ON"
        else:
                GPIO.output(PUMPRLY, False)
                print "The Pump is OFF"


        data, addr = sock.recvfrom(64)

            if data == b'7H':
                GPIO.output(7, True)

            elif data == b'7L':
                GPIO.output(7, False)
        time.sleep(30)

但这没有用。 仅供参考UDP的时间并不重要。 我不介意它是否等待30秒才能执行接收到的UDP消息,只要它不中断ADC的采样即可。 请记住,我是编程的新手,我没有拿出上面的代码,我只是复制并更改了它。

使用select()以便仅从main()循环中的就绪套接字读取。 Select()是久经考验的标准库函数,并且可以在Linux和Windows上正常工作。 http://docs.python.org/2/library/select.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM