2016-11-20 1 views
1

Guten Tag alle,Valueerror: „Port“ muss keine oder eine Zeichenfolge sein, nicht <class 'int'>

Ich versuche, Befehle an meine usb angeschlossen Sperr-Steuerung zu erteilen. Der Code läuft bis dieser Linie fein:

Connected ports: 
['COM6'] 
Please enter the port number that has port connected. 
i.e. if COM3 is shown, enter 3 when ask to enter port number. 
If the above shows [] than no connected port is found and errors will follow 
Please enter the port number: 6 

und dem folgenden Fehler kam:

ValueError: "port" must be None or a string, not <class 'int'> 

Ich erhalte die folgende Fehlermeldung:

Traceback (most recent call last): 
    File "[...]", line 44, in <module> 
    serial_connection() #executes the function defined above 

    File "[...]", line 29, in serial_connection 
    ser.port = COMPORT - 1 #counter for port name starts at 0 

    File "[...]", line 260, in port 
    raise ValueError('"port" must be None or a string, not {}'.format(type(port))) 

Unten ist der Code:

#this library is required as it helps establish the serial connection 
#Using pyserial Library to establish connection 
import serial #not being used because it was already called in portRead 
from portRead import * 

#Global Variables 
ser = 0 

#this will print out the port that can be used; ports that are connected. i.e. if COM3 is shown, enter 3 when ask to enter port number 
#if shown [] then no usb is connected to a port and errors will follow 
if __name__ == '__main__': 
    print ("Connected ports: ") #print means printing information on screen 
    print(serial_ports()) #function derived from portRead 
    print("Please enter the port number that has port connected. \n " 
      "i.e. if COM3 is shown, enter 3 when ask to enter port number. \n If the above shows [] than no connected port is found and errors will follow") 

#defining the serial connection 
def serial_connection(): 
    while True: #the while loop is used to continuously ask you to issue a new command until you decide to quit the program 
     ser = serial.Serial() 
     ser.baudrate = 38400 #Suggested rate in Southco documentation, both locks and program MUST be at same rate 
     COMPORT = int(input("Please enter the port number: ")) #this refers to which port your usb is inserted into 
     ser.port = COMPORT - 1 #counter for port name starts at 0 

     print ('Port %d entered Stephan '%COMPORT) 
     #timeout in seconds 
     ser.timeout = 10 
     ser.open() 
     command = input("please type command - open1/close1, or open2/close2. \n" 
        "If already open and you try to open no action will be taken and vice-versa...: \n ") 
     #call the serial_connection() function 
     ser.write(("%s\r\n"%command).encode('ascii')) #Southco locks receives and sends commands in ASCII 


serial_connection() #executes the function defined above 

Jede Hilfe würde geschätzt werden, wie ich bin um die Lösung dieses Problems arbeitet

+0

Ich nehme an, Sie Port einstellen sollte ' "COM {}" -Format (COMPORT)' statt der Zahl.. –

+0

Danke für Ihren Kommentar. Wo würde ich das einbeziehen? – John

+0

Sie könnten wahrscheinlich nur 'int' aus' int (input ("...")) ' –

Antwort

2

Sie den Port Namen in ser.port als String, nicht nur die Anzahl angeben muß (wie ich denke, es PRN sein könnte, nicht nur COMx).

fix Vorschlag:

COMPORT = int(input("Please enter the port number: ")) #this refers to which port your usb is inserted into 
ser.port = "COM{}".format(COMPORT) 

Überprüfung der doc: https://pythonhosted.org/pyserial/shortintro.html#opening-serial-ports

+0

NVM über meinen vorherigen Kommentar. Es klappt. – John

Verwandte Themen