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

Delphi Discussion :

Composant de communication Delphi via une machine port RS232C


Sujet :

Delphi

  1. #1
    Nouveau Candidat au Club
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Avril 2020
    Messages
    44
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Bénin

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Avril 2020
    Messages : 44
    Points : 0
    Points
    0
    Par défaut Composant de communication Delphi via une machine port RS232C
    J'ai essayé de télécharger ComPort Library mais ça ne marche pas

  2. #2
    Membre averti Avatar de franckcl
    Homme Profil pro
    Developpeur Delphi
    Inscrit en
    Septembre 2004
    Messages
    516
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Developpeur Delphi
    Secteur : High Tech - Électronique et micro-électronique

    Informations forums :
    Inscription : Septembre 2004
    Messages : 516
    Points : 443
    Points
    443
    Par défaut
    Bonjour !

    Moi j'utilise CreateFile de l'API windows.

    Exemple de code que j'utilise pour ouvrir le port:
    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
     
    //----------------------------------------------------------------------------
    Function  TCom.OpenCom :Boolean;
    Var
      PCharPort : Array[0..100] of Char;
      c : THandle;
     S:string;
    Begin
     S:=Name;
     if length(Name)>4 then S:='\\.\'+Name; // if the com port>9 then insert this string before: '\\.\'
     StrPCopy(PCharPort,UpperCase(S));  // The Name is the com port
     c:=0;
     Cid := CreateFile(PCharPort, GENERIC_READ or GENERIC_WRITE, 0, nil,OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, c);
     // to avoid a crash at the end, recall the function....
     if (Cid=INVALID_HANDLE_VALUE)then Cid:=CreateFile(PCharPort,GENERIC_READ or GENERIC_WRITE,0,nil,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,c);
     Result:=(Cid <> INVALID_HANDLE_VALUE);
    End;
    Ensuite pour le configurer (à adapter bien sur car j'ai copier/coller mon 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
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
     
    Function TCom.SetupCom:boolean;
    Var
      DCB : TDCB;
      flag : longint;
    Begin
     result:=false;
     if ComIsOpen then
     begin
       if GetCommState(Cid, dcb) then
       begin
         dcb.BaudRate := strtoint(Baud);
         dcb.ByteSize := strtoint(DataSize);
         case lowercase(parity)[1] of
         'n' : dcb.Parity :=0;
         'o' : dcb.Parity :=1;
         'e' : dcb.Parity :=2;
          else dcb.Parity :=3;
         end;
         dcb.StopBits := strtoint(NbStop)-1;
         dcb.Flags := 0;
     
         //08/12/2016: Ajout ---------------------------------------
         (* flag bitmap
                b0: fBinary (1)
                b1: fParity (2)
                b2: fOutxCtsFlow (4)
                b3: fOutxDsrFlow (8)
                b4,5: fDtrControl (x10,x20)
                b6: fDsrSensitivity (x40)
                b7: fTXContinueOnXoff (x80)
                b8: fOutX (x100)
                b9: fInX  (x200)
                b10:fErrorChar (x400)
                b11:fNull (x800)
                b12,13:fRtsControl (x1000,x2000)
                b14:fAbortOnError (x4000)
                b15..31: fDummy2
          *)
         flag := $5081; // fbinary(1) +  TxContinueOnXOff($80) + RtsControlEnabled($1000) + AbordOnError($4000)
         if (dcb.Parity<>0)  then flag := flag + 2; // dcb_ParityCheck;
         if (CTS<>0) then flag := flag + 4; // dcb_OutxCtsFlow;
         if (DSR<>0) then flag := flag + 8; // dcb_OutxDsrFlow;
         flag := flag OR DTR;
         if (CTS<>0) or (DSR<>0) or (DTR<>0) then dcb.flags := flag;
         //08/12/2016: fin Ajout-------------------------------------
     
         SetCommState(Cid, dcb);
       end;
       result := (Cid <> INVALID_HANDLE_VALUE);
     end;
    End;
    Fermeture:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
    Procedure TCom.CloseCom;
    Begin
      if ComIsOpen then CloseHandle(cid);
      Cid := INVALID_HANDLE_VALUE;
    End;
    Envoi de trame:
    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
     
    Function TCom.SendFrame(TxData:PByte;len:integer):boolean;
    Var
      lw : longword;
      Ok : Boolean;
    Begin
      if AppIsStopped then
      Begin
        result:=true;
        exit;
      End;
     
      if (FormAnalyser<>nil) then  TCom_FormAnalyser(FormAnalyser).LogFrame(CurrentModule,false,ByteArrayToStr(TxData,len,' '));
      PurgeComm(cid, PURGE_TXABORT or PURGE_RXABORT or PURGE_TXCLEAR or PURGE_RXCLEAR);
      lw := 0;
      Ok := true;
      if ComIsOpen then Ok:=winprocs.WriteFile(cid, Pansichar(@TxData[0])^, longword(len), longword(lw), nil);
      if not(ok) then closecom;
      inc(CounterFrame);
      ShowTx(false);
      result:= (lw=longword(len)) and Ok;
    End;
    Lecture de trame:
    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
    23
    24
    25
    26
    27
     
    function  TCom.ReadFrame(RxData:PByte;ExpectedLen:integer):integer;  // This function is implemented only in the com port and in the socket
    Var
      Status : TComStat;
      n,dwErrorCode,cbCharsRead : dword;
    Begin
      result:=0;
      n := 0;
      if AppIsStopped then exit;
     
      try
        ClearCommError(cid, dwErrorCode, @status);
        n := Status.Cbinque;
        if (n>0) then ShowRx;
      except
      end;
      if (n > 0) then  // if chars are ready to read
      Begin
        if integer(n)>ExpectedLen then n:=dword(ExpectedLen);
        result := n;
        try
          if ReadFile(cid, RxData^, n, cbCharsRead, nil) then result:=cbCharsRead;
          if (FormAnalyser<>nil) and (result>0) then TCom_FormAnalyser(FormAnalyser).LogFrame(CurrentModule,true,ByteArrayToStr(RxData,result,' '));
        except
        end;
      End;
    End;

  3. #3
    Expert confirmé Avatar de sergio_is_back
    Homme Profil pro
    Responsable informatique, développeur tout-terrain
    Inscrit en
    Juin 2004
    Messages
    1 084
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 55
    Localisation : France, Puy de Dôme (Auvergne)

    Informations professionnelles :
    Activité : Responsable informatique, développeur tout-terrain
    Secteur : High Tech - Électronique et micro-électronique

    Informations forums :
    Inscription : Juin 2004
    Messages : 1 084
    Points : 5 604
    Points
    5 604
    Par défaut
    Citation Envoyé par Pascal AFATONDJI Voir le message
    J'ai essayé de télécharger ComPort Library mais ça ne marche pas
    Je m'en suis servi pas mal de fois, j'ai jamais eu de problème avec...

    Avec quelle version de Delphi ?
    T'as un problème d'installation du package ou d'utilisation ?

  4. #4
    Nouveau Candidat au Club
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Avril 2020
    Messages
    44
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Bénin

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Avril 2020
    Messages : 44
    Points : 0
    Points
    0
    Par défaut
    Delphi7 et j'ai de difficulté a créé une connection de communication entre mon application avec une machine de contrôle de facturation qui a un port RS232C.

  5. #5
    Expert confirmé Avatar de sergio_is_back
    Homme Profil pro
    Responsable informatique, développeur tout-terrain
    Inscrit en
    Juin 2004
    Messages
    1 084
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 55
    Localisation : France, Puy de Dôme (Auvergne)

    Informations professionnelles :
    Activité : Responsable informatique, développeur tout-terrain
    Secteur : High Tech - Électronique et micro-électronique

    Informations forums :
    Inscription : Juin 2004
    Messages : 1 084
    Points : 5 604
    Points
    5 604
    Par défaut
    Citation Envoyé par Pascal AFATONDJI Voir le message
    Delphi7 et j'ai de difficulté a créé une connection de communication entre mon application avec une machine de contrôle de facturation qui a un port RS232C.
    Delphi 7 OK
    Mais tu répond pas à la question : C'est à l'installation du package ou à l'utilisation
    Si c'est à l'utilisation quel est exactement ton problème

    Sans plus de précision, on ne pourra pas t'aider...

  6. #6
    Nouveau Candidat au Club
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Avril 2020
    Messages
    44
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Bénin

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Avril 2020
    Messages : 44
    Points : 0
    Points
    0
    Par défaut
    Je veux envoyer de message à la machine à partir de delphi

  7. #7
    Expert confirmé Avatar de sergio_is_back
    Homme Profil pro
    Responsable informatique, développeur tout-terrain
    Inscrit en
    Juin 2004
    Messages
    1 084
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 55
    Localisation : France, Puy de Dôme (Auvergne)

    Informations professionnelles :
    Activité : Responsable informatique, développeur tout-terrain
    Secteur : High Tech - Électronique et micro-électronique

    Informations forums :
    Inscription : Juin 2004
    Messages : 1 084
    Points : 5 604
    Points
    5 604
    Par défaut
    Citation Envoyé par Pascal AFATONDJI Voir le message
    Je veux envoyer de message à la machine à partir de delphi
    Pas plus clair...

    Pour envoyer une chaine avec ce composant on fait comme ça :

    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
    23
     
    type
      TForm3 = class(TForm)
        ComPort1: TComPort;
      private
        { Déclarations privées }
      public
        { Déclarations publiques }
      end;
     
    var
      Form3: TForm3;
     
    implementation
     
    {$R *.dfm}
     
    procedure TForm3.FormCreate(Sender: TObject);
    begin
        ComPort1.Open;
        ComPort1.WriteStr('TOTO');
     
    end;
    Après il faut déjà que les paramètres de communication et le port soient bien configurés (et ça je ne peux pas le faire pour toi, les paramètres dépendent de la machine connectée sur le port série ) :

    Nom : Comport.jpg
Affichages : 557
Taille : 39,6 Ko

  8. #8
    Nouveau Candidat au Club
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Avril 2020
    Messages
    44
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Bénin

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Avril 2020
    Messages : 44
    Points : 0
    Points
    0
    Par défaut
    Merci Mr pour votre aide
    C'est bon j'ai fait la configuration et tout ça marche mais le soucis est que comment envoyé une commande décris dans le protocole de communication à la machine. Merci

  9. #9
    Modérateur
    Avatar de tourlourou
    Homme Profil pro
    Biologiste ; Progr(amateur)
    Inscrit en
    Mars 2005
    Messages
    3 858
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 61
    Localisation : France, Yvelines (Île de France)

    Informations professionnelles :
    Activité : Biologiste ; Progr(amateur)

    Informations forums :
    Inscription : Mars 2005
    Messages : 3 858
    Points : 11 299
    Points
    11 299
    Billets dans le blog
    6
    Par défaut
    Citation Envoyé par sergio_is_back Voir le message

    Pour envoyer une chaine avec ce composant on fait comme ça :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    ComPort1.WriteStr('TOTO');
    Si tu remplaces 'TOTO' par une commande décrite dans le protocole, ça devrait le faire puisque tu dis que tout le reste fonctionne...
    Delphi 5 Pro - Delphi 11.3 Alexandria Community Edition - CodeTyphon 6.90 sous Windows 10 ; CT 6.40 sous Ubuntu 18.04 (VM)
    . Ignorer la FAQ Delphi et les Cours et Tutoriels Delphi nuit gravement à notre code !

  10. #10
    Membre averti Avatar de franckcl
    Homme Profil pro
    Developpeur Delphi
    Inscrit en
    Septembre 2004
    Messages
    516
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Developpeur Delphi
    Secteur : High Tech - Électronique et micro-électronique

    Informations forums :
    Inscription : Septembre 2004
    Messages : 516
    Points : 443
    Points
    443
    Par défaut
    Citation Envoyé par Pascal AFATONDJI Voir le message
    comment envoyé une commande décris dans le protocole de communication à la machine. Merci
    Quelle machine est-ce ? quel modèle ? quel marque ?

Discussions similaires

  1. Réponses: 2
    Dernier message: 08/03/2019, 21h51
  2. Composant de communication Delphi et ECU Voiture
    Par nahil dans le forum Delphi
    Réponses: 1
    Dernier message: 23/03/2017, 11h00
  3. Transmission de données via une liaison port USB
    Par samuela dans le forum MATLAB
    Réponses: 0
    Dernier message: 10/08/2014, 21h51
  4. Réponses: 7
    Dernier message: 24/04/2009, 12h39
  5. [TP] Commande d'une machine via un port parallèle
    Par alaedin dans le forum Turbo Pascal
    Réponses: 1
    Dernier message: 03/06/2007, 21h45

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