Seite 1 von 1

Sockets verbindung schmeißt error !

Verfasst: Sonntag 23. September 2018, 19:46
von Elias177
Guten Tag,

ich habe eine Socket verbindung mit Python und Java hergestellt das auch alles super geht, aber wenn ich das script in ein anderes einfüge schmeißt es error !

Pythonscript:

Code: Alles auswählen

#!/usr/bin/env python
# -*- coding: utf8 -*-
#
#    Copyright 2014,2018 Mario Gomez <mario.gomez@teubi.co>
#
#    This file is part of MFRC522-Python
#    MFRC522-Python is a simple Python implementation for
#    the MFRC522 NFC Card Reader for the Raspberry Pi.
#
#    MFRC522-Python is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Lesser General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    MFRC522-Python is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Lesser General Public License for more details.
#
#    You should have received a copy of the GNU Lesser General Public License
#    along with MFRC522-Python.  If not, see <http://www.gnu.org/licenses/>.
#

import RPi.GPIO as GPIO
import MFRC522
import socket
import signal

continue_reading = True

# Capture SIGINT for cleanup when the script is aborted
def end_read(signal,frame):
    global continue_reading
    print "Ctrl+C captured, ending read."
    continue_reading = False
    GPIO.cleanup()

# Hook the SIGINT
signal.signal(signal.SIGINT, end_read)

# Create an object of the class MFRC522
MIFAREReader = MFRC522.MFRC522()

# Welcome message
print "Welcome to the MFRC522 data read example"
print "Press Ctrl-C to stop."

# This loop keeps checking for chips. If one is near it will get the UID and authenticate
while continue_reading:
 
    # Scan for cards
    (status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)

    # If a card is found
    if status == MIFAREReader.MI_OK:
        print "Card detected"
 
    # Get the UID of the card
    (status,uid) = MIFAREReader.MFRC522_Anticoll()
 
    # If we have the UID, continue
    if status == MIFAREReader.MI_OK:
 
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.connect(("localhost", 177))
        s.send("1234567890\n")
 
        # Print UID
        print "Card read UID: %s,%s,%s,%s" % (uid[0], uid[1], uid[2], uid[3])
   
 
        # This is the default key for authentication
        key = [0xFF,0xFF,0xFF,0xFF,0xFF,0xFF]
   
        # Select the scanned tag
        MIFAREReader.MFRC522_SelectTag(uid)

        # Authenticate
        status = MIFAREReader.MFRC522_Auth(MIFAREReader.PICC_AUTHENT1A, 8, key, uid)

        # Check if authenticated
        if status == MIFAREReader.MI_OK:
            MIFAREReader.MFRC522_Read(8)
            MIFAREReader.MFRC522_StopCrypto1()
        else:
            print "Authentication error"
Error:

Code: Alles auswählen

pi@raspberrypi:~/Desktop $ sudo python Read.py
  File "Read.py", line 69
    print "Card read UID: %s,%s,%s,%s" % (uid[0], uid[1], uid[2], uid[3])
                                                                        ^
IndentationError: unindent does not match any outer indentation level
pi@raspberrypi:~/Desktop $
Hat jemand eiine idee wie man es lösen kann ?

MFG

Elias177

Re: Sockets verbindung schmeißt error !

Verfasst: Sonntag 23. September 2018, 21:53
von __deets__
Einrückungen sind in Python wichtig. Das ist das aller erste, das dir jedes Python Anfängertutorial beibringt. Ich empfehle dir, da mal wenigstens ein paar Minuten rein zu schauen.

Re: Sockets verbindung schmeißt error !

Verfasst: Montag 24. September 2018, 07:10
von Sirius3
Ansonsten ist auch der restliche Code, den Du da kopiert hast, kaputt. Signal-Handling ist kompliziert und man sollte im Handler keine komplizierten Dinge machen. SIGINT wird von Python automatisch abgefangen und ein KeyboardInterrupt-Exception geworfen, die man einfach behandeln kann. Low-Level Socket-Programmierung ist kompliziert. Auch wenn auf den ersten Blick alles zu funktionieren scheint, muß man doch die Details beachten, zumindest sendall benutzen.

Re: Sockets verbindung schmeißt error !

Verfasst: Montag 24. September 2018, 13:42
von Elias177
__deets__ hat geschrieben: Sonntag 23. September 2018, 21:53 Einrückungen sind in Python wichtig. Das ist das aller erste, das dir jedes Python Anfängertutorial beibringt. Ich empfehle dir, da mal wenigstens ein paar Minuten rein zu schauen.
Okay aber ich sehe dort jetzt so mit dem einrücken kein Fehler (liegt vlt. daran das ich Java Dev bin) .

Re: Sockets verbindung schmeißt error !

Verfasst: Montag 24. September 2018, 14:03
von __blackjack__
@Elias177: Der im erstem Beitrag gezeigte Code hat den Fehler auch nicht. Kann es sein das der nicht 1:1 kopiert wurde und das Original vielleicht Tab-Zeichen enthält? Einrücken immer mit vier *Leerzeichen* pro Ebene. Tabs machen nur Ärger.

Re: Sockets verbindung schmeißt error !

Verfasst: Montag 24. September 2018, 14:35
von Elias177
okay aber der code ist orginial heruntergeladen

Re: Sockets verbindung schmeißt error !

Verfasst: Montag 24. September 2018, 14:38
von Elias177
auser das mit sockets*

Re: Sockets verbindung schmeißt error !

Verfasst: Montag 24. September 2018, 15:16
von __deets__
Wenn der Original Code schon den Fehler hat, dann ist der halt auch kaputt. Wenn nicht, dann machst du ihn kaputt. So oder: du hast einen Fehler, und du musst genug Python verstehen, um den auseinander sortieren zu koennen. Oder den Code zeigen, der den Fehler hat. Denn der gezeigte hat ihn nicht.

Re: Sockets verbindung schmeißt error !

Verfasst: Dienstag 25. September 2018, 16:17
von Elias177
__deets__ hat geschrieben: Montag 24. September 2018, 15:16 Wenn der Original Code schon den Fehler hat, dann ist der halt auch kaputt. Wenn nicht, dann machst du ihn kaputt. So oder: du hast einen Fehler, und du musst genug Python verstehen, um den auseinander sortieren zu koennen. Oder den Code zeigen, der den Fehler hat. Denn der gezeigte hat ihn nicht.
Das ist es ja der Code wirft fehler aber der code(der unten gezeigte) nicht.
Liegt es an dem Socket teil den ich eingefügt habe ?

Code(Original ohne Sockets):

Code: Alles auswählen

#!/usr/bin/env python
# -*- coding: utf8 -*-
#
#    Copyright 2014,2018 Mario Gomez <mario.gomez@teubi.co>
#
#    This file is part of MFRC522-Python
#    MFRC522-Python is a simple Python implementation for
#    the MFRC522 NFC Card Reader for the Raspberry Pi.
#
#    MFRC522-Python is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Lesser General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    MFRC522-Python is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Lesser General Public License for more details.
#
#    You should have received a copy of the GNU Lesser General Public License
#    along with MFRC522-Python.  If not, see <http://www.gnu.org/licenses/>.
#

import RPi.GPIO as GPIO
import MFRC522
import signal

continue_reading = True

# Capture SIGINT for cleanup when the script is aborted
def end_read(signal,frame):
    global continue_reading
    print "Ctrl+C captured, ending read."
    continue_reading = False
    GPIO.cleanup()

# Hook the SIGINT
signal.signal(signal.SIGINT, end_read)

# Create an object of the class MFRC522
MIFAREReader = MFRC522.MFRC522()

# Welcome message
print "Welcome to the MFRC522 data read example"
print "Press Ctrl-C to stop."

# This loop keeps checking for chips. If one is near it will get the UID and authenticate
while continue_reading:
    
    # Scan for cards    
    (status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)

    # If a card is found
    if status == MIFAREReader.MI_OK:
        print "Card detected"
    
    # Get the UID of the card
    (status,uid) = MIFAREReader.MFRC522_Anticoll()

    # If we have the UID, continue
    if status == MIFAREReader.MI_OK:

        # Print UID
        print "Card read UID: %s,%s,%s,%s" % (uid[0], uid[1], uid[2], uid[3])
    
        # This is the default key for authentication
        key = [0xFF,0xFF,0xFF,0xFF,0xFF,0xFF]
        
        # Select the scanned tag
        MIFAREReader.MFRC522_SelectTag(uid)

        # Authenticate
        status = MIFAREReader.MFRC522_Auth(MIFAREReader.PICC_AUTHENT1A, 8, key, uid)

        # Check if authenticated
        if status == MIFAREReader.MI_OK:
            MIFAREReader.MFRC522_Read(8)
            MIFAREReader.MFRC522_StopCrypto1()
        else:
            print "Authentication error"


Re: Sockets verbindung schmeißt error !

Verfasst: Dienstag 25. September 2018, 16:19
von __deets__
Natuerlich kann das daran liegen, das du was eingefuegt hast. Du kannst zB tabs und spaces gemischt haben. Du solltest deinen Editor so einstellen, das er niemals tabs erzeugt.

Re: Sockets verbindung schmeißt error !

Verfasst: Dienstag 25. September 2018, 17:22
von Elias177
habe den fehler behoben bekommen aber ich bekomme jetzt einen neuen.

Fehler:

Code: Alles auswählen

pi@raspberrypi:~/Desktop $ sudo python Read.py
  File "Read.py", line 25
    import MFRC522
    ^
IndentationError: unexpected indent
pi@raspberrypi:~/Desktop $

Re: Sockets verbindung schmeißt error !

Verfasst: Dienstag 25. September 2018, 17:26
von sls
Vor dem import-Statement gehört keine Einrückung (weder Tabs noch Leerzeichen)

Re: Sockets verbindung schmeißt error !

Verfasst: Dienstag 25. September 2018, 17:41
von Elias177
ist nicht Eingerückt

Re: Sockets verbindung schmeißt error !

Verfasst: Dienstag 25. September 2018, 17:42
von Elias177
haber aber jetzt den fehler

Code: Alles auswählen

pi@raspberrypi:~/Desktop $ sudo python Read.py
Traceback (most recent call last):
  File "Read.py", line 25, in <module>
    import MFRC522
ImportError: No module named MFRC522
pi@raspberrypi:~/Desktop $

Re: Sockets verbindung schmeißt error !

Verfasst: Dienstag 25. September 2018, 17:54
von sls
Das ist in Python mit zusätzlichen Modulen und Bibliotheken im Grunde nicht so viel anders wie in Java auch. Wo liegt denn die Datei Read.py, und in welchem Verzeichnis MFRC522?

Re: Sockets verbindung schmeißt error !

Verfasst: Dienstag 25. September 2018, 18:02
von Elias177
Read.py auf dem Desktop und MFRC522 ist in einem ordner auf dem Desktop

Re: Sockets verbindung schmeißt error !

Verfasst: Dienstag 25. September 2018, 18:04
von Elias177
habe es so weit geschaft habe die Read.py einfach in den MFRC522 ordner geschoben und jetzt geht alles =)

Danke an alle für die hilfe

MFG

Elias177