IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Python Discussion :

Récupérer une trame qui transite par un port USB


Sujet :

Python

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éclairé
    Homme Profil pro
    Touche à tout
    Inscrit en
    Mai 2017
    Messages
    479
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Allier (Auvergne)

    Informations professionnelles :
    Activité : Touche à tout

    Informations forums :
    Inscription : Mai 2017
    Messages : 479
    Par défaut Récupérer une trame qui transite par un port USB
    Bonjour,

    J'aimerai savoir si je peux récupérer la trame envoyé par un appareil branché sur mon PC en USB?

    J'aimerai faire un script comme si j'utilisais 'tera term' pour ne pas le citer.

    Merci par avance pour vos retour.

  2. #2
    Expert éminent
    Homme Profil pro
    Architecte technique retraité
    Inscrit en
    Juin 2008
    Messages
    21 741
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Manche (Basse Normandie)

    Informations professionnelles :
    Activité : Architecte technique retraité
    Secteur : Industrie

    Informations forums :
    Inscription : Juin 2008
    Messages : 21 741
    Par défaut
    Salut,

    Peut être avec pyserial?

    - W
    Architectures post-modernes.
    Python sur DVP c'est aussi des FAQs, des cours et tutoriels

  3. #3
    Expert confirmé
    Avatar de fred1599
    Homme Profil pro
    Lead Dev Python
    Inscrit en
    Juillet 2006
    Messages
    4 062
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Meurthe et Moselle (Lorraine)

    Informations professionnelles :
    Activité : Lead Dev Python
    Secteur : Arts - Culture

    Informations forums :
    Inscription : Juillet 2006
    Messages : 4 062
    Par défaut
    Voir ICI !

  4. #4
    Membre éclairé
    Homme Profil pro
    Touche à tout
    Inscrit en
    Mai 2017
    Messages
    479
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Allier (Auvergne)

    Informations professionnelles :
    Activité : Touche à tout

    Informations forums :
    Inscription : Mai 2017
    Messages : 479
    Par défaut
    J'ai installé la bibliothèque Pyserial. Elle a l'air complète (enfin il faut voir à l'utilisation :-)).

    Donc avec cette bibliothèque, je peux récupérer toute la trame si j'ai 1,2,3, 4 lignes voir plus?

  5. #5
    Expert éminent
    Homme Profil pro
    Architecte technique retraité
    Inscrit en
    Juin 2008
    Messages
    21 741
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Manche (Basse Normandie)

    Informations professionnelles :
    Activité : Architecte technique retraité
    Secteur : Industrie

    Informations forums :
    Inscription : Juin 2008
    Messages : 21 741
    Par défaut
    Citation Envoyé par Supernatural Voir le message
    Donc avec cette bibliothèque, je peux récupérer toute la trame si j'ai 1,2,3, 4 lignes voir plus?
    Pour l'instant, vous avez juste de quoi lire et écrire des octets sur un port série.
    Pour le reste, il va falloir coder.

    - W
    Architectures post-modernes.
    Python sur DVP c'est aussi des FAQs, des cours et tutoriels

  6. #6
    Membre éclairé
    Homme Profil pro
    Touche à tout
    Inscrit en
    Mai 2017
    Messages
    479
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Allier (Auvergne)

    Informations professionnelles :
    Activité : Touche à tout

    Informations forums :
    Inscription : Mai 2017
    Messages : 479
    Par défaut
    J'ai trouvé un code qui correspond à mes besoins, je le poste si jamais quelqu'un est intéressé:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
     
    import serial
    import time # Optional (if using time.sleep() below)
     
    connected = False
    port = 'COM10'
    baud = 9600
     
    try:
        print('Port.......'+port)
        print('Baudrate...'+str(baud))
        ser = serial.Serial(port, baud, timeout=0)
     
        while (True):
            # NB: for PySerial v3.0 or later, use property `in_waiting` instead of function `inWaiting()` below!
            if (ser.inWaiting()>0): #if incoming bytes are waiting to be read from the serial input buffer
                data_str = ser.read(ser.inWaiting()).decode('ascii') #read the bytes and convert from binary array to ASCII
                print(data_str, end='') #print the incoming string without putting a new-line ('\n') automatically after every print()
            #Put the rest of your code you want here
            time.sleep(0.01) # Optional: sleep 10 ms (0.01 sec) once per loop to let other threads on your PC run during this time. 
    except:
        print('Error '+port+ ' is not connected')

  7. #7
    Membre éclairé
    Homme Profil pro
    Touche à tout
    Inscrit en
    Mai 2017
    Messages
    479
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Allier (Auvergne)

    Informations professionnelles :
    Activité : Touche à tout

    Informations forums :
    Inscription : Mai 2017
    Messages : 479
    Par défaut
    Je reviens vers vous car j'ai un soucis au niveau de la gestion de la trame... Je voudrais qu'une fois que j'ai récupéré mes x lignes de ma trame, j'arrête la scrutation pour traiter mes données...

    Pour mémoire le code:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    import serial
    import time # Optional (if using time.sleep() below)
     
    connected = False
    port = 'COM10'
    baud = 9600
     
    try:
        print('Port.......'+port)
        print('Baudrate...'+str(baud))
        ser = serial.Serial(port, baud, timeout=0)
     
        while (True):
            # NB: for PySerial v3.0 or later, use property `in_waiting` instead of function `inWaiting()` below!
            if (ser.inWaiting()>0): #if incoming bytes are waiting to be read from the serial input buffer
                data_str = ser.read(ser.inWaiting()).decode('ascii') #read the bytes and convert from binary array to ASCII
                print(data_str, end='') #print the incoming string without putting a new-line ('\n') automatically after every print()
            #Put the rest of your code you want here
            time.sleep(0.01) # Optional: sleep 10 ms (0.01 sec) once per loop to let other threads on your PC run during this time. 
    except:
        print('Error '+port+ ' is not connected')

Discussions similaires

  1. Récupérer les requêtes qui passent par ODBC
    Par Danger dans le forum VB 6 et antérieur
    Réponses: 3
    Dernier message: 13/07/2009, 14h13
  2. [RegEx] Récupérer une chaine de caractéres par strpos
    Par bdptaki dans le forum Langage
    Réponses: 11
    Dernier message: 21/04/2009, 11h43
  3. sed: remplacer une ligne qui commence par Version
    Par lili2704 dans le forum Linux
    Réponses: 8
    Dernier message: 21/07/2008, 14h26
  4. émuler une touche du clavier par le port //
    Par zileg dans le forum Audio
    Réponses: 2
    Dernier message: 12/04/2007, 16h59
  5. [v2][VB.NET][Port-Série] Récupérer une trame ?
    Par burnedsoul dans le forum VB.NET
    Réponses: 4
    Dernier message: 30/11/2005, 16h46

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo