用树莓pi3b控制伺服电机Python代码中的错误+

2024-06-07 05:11:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在构建一个自动垃圾桶使用树莓皮3B+与Android应用程序支持,我将使用伺服电机作为盖子的执行器,Android应用程序作为一种无线远程控制形式。一切都很顺利,直到我遇到一个问题:每当我试图在Android应用程序上按下按钮时,pythonshell程序在测试过程中就会出错。我使用了一个参考视频(https://www.youtube.com/watch?v=t8THp3mhbdA&t=1s)并彻底跟踪了所有内容,直到遇到了这个障碍。在

在我看来,不断出现的结果是:

Waiting for connection 
...connected from : 

其中,根据参考视频,假设结果为:

^{pr2}$

如您所见,IP地址、端口和“增加”文本不会出现,这意味着代码有问题。在

根据观看视频的人的一些评论,这段代码已经过时了,使用的是python2,现在最新的版本是python3,我们需要在条件中使用“.encode()”行。但是,作为一个对Python还不熟悉的人,我恐怕还不具备将其应用于代码的知识。在

以下是视频中使用的代码:

我已经将使用“”格式的文本字符串更改为(“”)格式,因为它在代码中也产生了一些错误,我立即更正了这些错误。在

如有任何帮助,我们将不胜感激,并提前感谢您!在


Tags: 代码文本应用程序视频远程格式错误按钮
1条回答
网友
1楼 · 发布于 2024-06-07 05:11:42

这是一个Python3版本,为了更好的清晰和良好的实践,编辑了一点:

import Servomotor
import RPi.GPIO as GPIO
import socket

# Setup the motor
Servomotor.setup()

# Declare the host address constant - this will be used to connect to Raspberry Pi
# First values is IP - here localhost, second value is the port
HOST_ADDRESS = ('0.0.0.0', 21567)

# Declare the buffer constant to control receiving the data
BUFFER_SIZE = 4096

# Declare possible commands
commands = 'Up', 'Down'

# Create a socket (pair of IP and port) object and bind it to the Raspberry Pi address
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(HOST_ADDRESS)

# Set the socket to listen to an incoming connection (1 at a time)
server_socket.listen(1)

# Never stop the server once it's running
while True:

    # Inform that the server is waiting for a connection
    print("Waiting for connection to the following address: {}...".format(HOST_ADDRESS))

    # Perform a blocking accept operation to wait for a client connection
    client_socket, client_address = server_socket.accept()

    # Inform that the client is connected
    print("Client with an address {} connected".format(client_address))

    # Keep exchanging data
    while True:

        try:

            # Receive the data (blocking receive)
            data = client_socket.recv(BUFFER_SIZE)

            # If 0-byte was received, close the connection
            if not data:
                break

            # Attempt to decode the data received (decode bytes into utf-8 formatted string)
            try:
                data = data.decode("utf-8").strip()
            except UnicodeDecodeError:
                # Ignore data that is not unicode-encoded
                data = None

            # At this stage data is correctly received and formatted, so check if a command was received
            if data == commands[0]:
                Servomotor.ServoUp()
                print("Increase: {}".format(Servomotor.cur_X))
            elif data == commands[1]:
                Servomotor.ServoDown()
                print("Decrease: {}".format(Servomotor.cur_X))
            elif data:
                print("Received invalid data: {}".format(data))

        # Handle possible errors
        except ConnectionResetError:
            break
        except ConnectionAbortedError:
            break
        except KeyboardInterrupt:
            break

    # Cleanup
    Servomotor.close()
    GPIO.cleanup()
    client_socket.close()

    # Inform that the connection is closed
    print("Client with an address {} disconnected.".format(client_address))

为了向您展示实际的代码,我在我的机器上托管了一个本地服务器,并使用Putty连接到它。以下是我输入的命令:

Putty commands

以下是服务器的输出(我已将伺服相关功能交换为打印语句):

^{pr2}$

相关问题 更多 >