TypeError:encode()参数“encoding”必须是str,而不是int

2024-10-01 05:05:24 发布

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

我试图用pyserial在python中控制我的Arduino,但出现以下错误: TypeError:encode()参数“encoding”必须是str,而不是int

我对编码很陌生,所以我有点迷路了。 我想做的是首先选择“function”nr1,也就是“swipe”,然后给它三个参数。首先,它说“command”最多只接受两个参数,所以我尝试使用2/3个参数来让它工作,但现在它抱怨参数必须是str。。。 有谁能帮我让它工作

import time
import serial
strComPort = '/dev/ttyACM0'
strComBaud = 9600

cmdSerial = serial.Serial(strComPort, strComBaud)
time.sleep(2) #sec

while True:
    command= input("Enter command number in list: \n1: Swipe \n2: tap\n3: Double tap\n4: Press\n5: Drag\n6: Flick right\n7: Flick left")

    if (command == '1'):
        x = int(input("From which deg?: "))
        y = int(input("To which deg? :"))
        spd = int(input("Speed of swipe? :"))
        cmdSerial.write(command.encode(x, y, spd))
        time.sleep(1)
    
    if (command == '2'):
        cmdSerial.write(command.encode())
        time.sleep(1)

    if (command == '3'):
        cmdSerial.write(command.encode())
        time.sleep(1)

    elif (command == '4'):
        cmdSerial.write(command.encode())
        time.sleep(1)

    elif (command == '5'):
        x = int(input("From which deg?: "))
        y = int(input("To which deg? :"))
        cmdSerial.write(command.encode(x, y))
        time.sleep(1)

    elif (command == '6'):
        cmdSerial.write(command.encode())
        time.sleep(1)

    elif (command == '7'):
        cmdSerial.write(command.encode())
        time.sleep(1)

    elif (command == 'q'):
        print("Exiting...")
        break

    else:
        print("Only number between 1-6 or 'q' (exit)")

Tags: whichinput参数iftimesleepcommandencode