vendredi 18 octobre 2019

Keep getting else/elif invalid syntax error

Currently I'm writing a code where I want to ask motor values from motor controller using raspberry pi. However my code is throwing InvalidSyntax Error in else and elif statements. I've already read about if and elif statements in python, but I can't figure out mistake by myself. The code is below:

def motor_values():

  while 1:
      command_1 = '?A '+str(1)+' \r' #Asking first motor current
      command_2 = '?A '+str(2)+' \r' #Asking second motor current
      counter = 0
      #First motor current
      if counter = 0:
        ser.write(command_1.encode()) #string to bytes
        data_1 = ser.readline().decode().strip() #bytes to string
        #Checking if data is received and extracting current value
        if 'A=' in data_1:
            value_1 = int(data_1.split('=')[1])
            print("Motor Current(Channel 1): " + str((value_1) + " Ampers")
            counter += 1
        else:
            print("Message is not received")
      #Second motor current
      elif counter == 1:
        ser.write(command_2.encode()) #string to bytes
        data_2 = ser.readline().decode().strip() #bytes to string
        if 'A=' in data_2:
            value_2 = int(data_2.split('=')[1])
            print("Motor Current(Channel 2): " + str((value_2) + " Ampers")
            counter += 1
        else:
            print("Message is not received")
      else:
        counter = 0

Aucun commentaire:

Enregistrer un commentaire