Devine De'Hara
Devine De'Hara

Reputation: 21

TypeError: encode() argument 'encoding' must be str, not int

I'm trying to control my Arduino in python with pyserial and get this error: TypeError: encode() argument 'encoding' must be str, not int

I'm really new to coding so I'm a little lost. What I'm trying to do is that I want to first choose "function" nr 1 which is "swipe" and I want to give it three arguments. First, it sais that "command" only takes two arguments at most so I tried to use 2/3 arguments just to make it work but now it complains that the arguments must be str... Anyone who can help me make it work??

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)")

Upvotes: 2

Views: 8728

Answers (1)

Red
Red

Reputation: 27577

You get the error because you are trying to encode a string with an integer.

Here is the list of all the encode that you can use.

Upvotes: 1

Related Questions