简体   繁体   English

等待 TCP 连接时 LED 闪烁

[英]Blink LED while waiting for TCP connection

I want to blink LED on my Raspberry Pi TCP server when it waits for a connection to be established.当我的 Raspberry Pi TCP 服务器等待建立连接时,我想使 LED 闪烁。 After the transmission is opened, LED should stay on.打开传输后,LED 应保持亮起。

At this point my server looks like this:此时我的服务器看起来像这样:

connectioStatus = False
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host, port))
s.listen()
While True: 
    try:
        led_connection_indicator(connectioStatus)
        clientSocket, address = s.accept()
        connectioStatus = True
        while True:
        measure and send data
    except ConnectionResetError:
        handle error
        connectioStatus = False

and led_connection_indicator fucntion looks like this:和 led_connection_indicator 功能看起来像这样:

def led_indicator(status):
if status is True:
    GPIO.output(Led, GPIO.HIGH)
else:
    while status is False:
        GPIO.output(Led, GPIO.HIGH)
        sleep(1)
        GPIO.output(Led, GPIO.LOW)
        sleep(1)
        if connectionStatus is True: break

Of course, this is not working at all, the program is stuck in while loop.当然,这根本行不通,程序卡在 while 循环中。 At first I thought that placing GPIO.HIGH and GPIO.LOW in the main While server loop will solve this but the program stops at s.起初我认为将 GPIO.HIGH 和 GPIO.LOW 放在主 While 服务器循环中可以解决这个问题,但程序在 s 处停止。 Accept ().接受 ()。

Is this job for threads?这是线程的工作吗? I tried to use signals, but no luck我尝试使用信号,但没有运气

I wrote a simple TCP client in Qt for server.我在 Qt 中为服务器编写了一个简单的 TCP 客户端。 What is weird for me that after I connect from the client to the server, socket status in Qt, returns "QAbstractSocket:: ConnectedState" but no data is received.对我来说奇怪的是,在我从客户端连接到服务器后,Qt 中的套接字状态返回“QAbstractSocket:: ConnectedState”但没有接收到数据。

socket.accept is blocking. socket.accept 正在阻塞。 You probably want to use select.select() which allows you to specify a timeout.您可能想要使用 select.select() 它允许您指定超时。

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

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