Ankündigung

Einklappen
Keine Ankündigung bisher.

Python Problem mit Sockets

Einklappen
X
 
  • Filter
  • Zeit
  • Anzeigen
Alles löschen
neue Beiträge

    Python Problem mit Sockets

    Hallo,

    ich möchte für meinen NAD Verstärker ein Plugin erstellen. Ich habe den Verstärker mittels RS232-Ethernet Converter am Netzwerk und kann ihn auch prima mittels TCP Client auf Windows steuern.
    Da ich mich eher bei PHP wohl fühle und ich nicht wirklich Programmierer bin (lang, lang ist's her) habe ich erstmal ein kleines PHP Script versucht.
    PHP-Code:
    <?php
    error_reporting
    (E_ALL);
    ob_implicit_flush();

    $addr "192.168.2.14";
    $socket20108;

    $client stream_socket_client("tcp://$addr:$socket"$errno$errorMessage);

    if (
    $client === false) {
        throw new 
    UnexpectedValueException("Failed to connect: $errorMessage");
    }
    fwrite($clientchr(13) . "Main.Volume-" chr(13));
    echo 
    stream_get_contents($client10);
    fclose($client);
    Das tut soweit auch tadellos und im nächsten Schritt wollte ich das dann nach Python migrieren.
    Code:
    #!/usr/bin/env python3
    
    import sys
    import socket
    
    if sys.hexversion < 0x03020000:
        print("Sorry your python interpreter ({0}.{1}) is too old. Please update to 3.2 or newer.".format(sys.version_info[0], sys.version_info[1]))
        exit()
        
    print("Starting...")
    host_ip, server_port = "192.168.2.14", 20108
    #separator = chr(10) + chr(13)
    separator = "\n\r"
    ENCODING = "ASCII"
    cmd = separator + "Main.Volume-" + separator
    cmd_encoded = cmd.encode(ENCODING)
    #cmd_encoded = b'\n\rMain.Volume-\n\r'
    
    # Initialize a TCP client socket using SOCK_STREAM
    try:
        print("Initializing conncection...")
        tcp_client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        tcp_client.settimeout(10)
        tcp_client.connect((host_ip, server_port))
    except Exception as e:
        print ('Could not connect to {}:{}: {}'.format(self._host, self._port, e))
    
    # Send data
    recv_encoded = ""
    list_ascii_send=[]
    list_ascii_recv=[]
    try:
        print("Sending data...")
        tcp_client.sendall(cmd_encoded)
        print("Receiving data...")
        recv_encoded = tcp_client.recv(16)
        list_ascii_send=[ord(i) for i in cmd_encoded.decode(ENCODING)]
        list_ascii_recv=[ord(i) for i in recv_encoded.decode(ENCODING)]
    except Exception as e:
        print ('Could not receive data: {}'.format(e))
    
    finally:
        tcp_client.close()
    
    
    print ("Bytes Sent:     {}".format(cmd_encoded))
    print ("Bytes Received: {}".format(recv_encoded))
    print ("Bytes ASCII S:  {}".format(list_ascii_send))
    print ("Bytes ASCII R:  {}".format(list_ascii_recv))
    Der Output des Scripts ist wie folgt:
    Code:
     Starting... Starting...
      Initializing conncection...
      Sending data...
      Receiving data...
      Bytes Sent:     b'\n\rMain.Volume-\n\r'
      Bytes Received: b'\n\rMain.Volume-\n\r'
      Bytes ASCII S:  [10, 13, 77, 97, 105, 110, 46, 86, 111, 108, 117, 109, 101, 45, 10, 13]
      Bytes ASCII R:  [10, 13, 77, 97, 105, 110, 46, 86, 111, 108, 117, 109, 101, 45, 10, 13]
    Wenn ich das Script starte und nichts weiter mache, bricht es mit einem Timeout ab (es wartet vergeblich auf Data from Stream; aber da kommt nichts, weil der Befehl falsch ist). Wenn ich dann während der Laufzeit des Scripts einen Befehl über das PHP Script abgebe, empfängt er den Response und stellt ihn entsprechend dar (s.o.). Da die Befehle in PHP und Python dieselben sind (Main.Volume-) sehen die Strings jeweils gleich aus. Heisst das, dass das Encoding richtig ist?

    Beim PHP Script hatte ich selbes Problem, bis ich "<CR>" in chr(13) geändert habe (Im Windows Client nutzte ich eben <CR>).

    Nun eben die spannende Frage, was ich hier noch probieren kann, damit ich weiterkomme.
    Sollte dieses test Script funktionieren, werde ich das ganze dann in ein NAD Modul für Smarthome.py einbauen.

    Schonmal Danke für Eure Mühe-
    Christian

    EDIT: Hier nochmal die Beschreibung zu den RS232 Codes http://nadelectronics.com/products/h...ding-downloads
    Zuletzt geändert von hhhc; 17.04.2016, 09:55.
    ++ Der ultimative ETS Schnellkurs ++
    KNX und die ETS vom Profi lernen
    www.ets-schnellkurs.de

    #2
    Beantworte das mal selbst: Es tut jetzt durch sehr viel Trial-and-Error...

    PHP-Code:
    #!/usr/bin/env python3

    import sys
    import socket

    if sys.hexversion 0x03020000:
        print(
    "Sorry your python interpreter ({0}.{1}) is too old. Please update to 3.2 or newer.".format(sys.version_info[0], sys.version_info[1]))
        exit()
        
    print(
    "Starting...")
    host_ipserver_port "192.168.2.14"20108
    separator 
    chr(13)
    ENCODING "ASCII"
    cmd separator "Main.Power=On" separator
    cmd_encoded 
    cmd.encode(ENCODING)

    # Initialize a TCP client socket using SOCK_STREAM
    try:
        print(
    "Initializing conncection...")
        
    tcp_client socket.socket(socket.AF_INETsocket.SOCK_STREAM)
        
    tcp_client.settimeout(10)
        
    tcp_client.connect((host_ipserver_port))
    except Exception as e:
        print (
    'Could not connect to {}:{}: {}'.format(self._hostself._porte))

    # Send data
    recv_encoded ""
    list_ascii_send=[]
    list_ascii_recv=[]
    try:
        print(
    "Sending data...")
        
    tcp_client.sendall(cmd_encoded)
        print(
    "Receiving data...")
        
    recv_encoded tcp_client.recv(16)
        
    list_ascii_send=[ord(i) for i in cmd_encoded.decode(ENCODING)]
        
    list_ascii_recv=[ord(i) for i in recv_encoded.decode(ENCODING)]
    except Exception as e:
        print (
    'Could not receive data: {}'.format(e))

    finally:
        
    tcp_client.close()


    print (
    "Bytes Sent:     {}".format(cmd_encoded))
    print (
    "Bytes Received: {}".format(recv_encoded))
    print (
    "Bytes ASCII S:  {}".format(list_ascii_send))
    print (
    "Bytes ASCII R:  {}".format(list_ascii_recv)) 
    ++ Der ultimative ETS Schnellkurs ++
    KNX und die ETS vom Profi lernen
    www.ets-schnellkurs.de

    Kommentar

    Lädt...
    X