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 :

Commander un module Relais HID USB avec Delphi


Sujet :

Delphi

  1. #1
    Membre à l'essai
    Profil pro
    Inscrit en
    Avril 2007
    Messages
    32
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2007
    Messages : 32
    Points : 22
    Points
    22
    Par défaut Commander un module Relais HID USB avec Delphi
    Bonjour,
    Je souhaite commander un module 2 relais HW 343 avec une application Delphi en mode HID USB.
    Pour le moment j'arrive à lire dans le module.
    Nom : RelUsb1.jpg
Affichages : 593
Taille : 43,0 Ko
    Je n'ai pas trouvé le data sheet de ce module, la seule information que j'ai trouvé est que le PC doit envoyer les codes de commande suivants :
    00 FF 01 01 -> Relais1 ON
    00 FD 01 00 -> Relais1 OFF
    00 FE 02 02 -> Relais2 ON
    00 FD 02 00 -> Relais2 OFF
    Je cherche une personne qui à déjà réussi à actionner ces relais avec Delphi.

  2. #2
    Membre expert
    Avatar de pprem
    Homme Profil pro
    MVP Embarcadero - formateur&développeur Delphi, PHP et JS
    Inscrit en
    Juin 2013
    Messages
    1 876
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Loiret (Centre)

    Informations professionnelles :
    Activité : MVP Embarcadero - formateur&développeur Delphi, PHP et JS
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Juin 2013
    Messages : 1 876
    Points : 3 611
    Points
    3 611
    Par défaut
    Bonjour
    Rien vu passer sur ce sujet, mais peut-être que tu auras plus de chances en regardant les nombreuses ressources produites par Boian Mitov de mitov.com même s'il utilise essentiellement les composants qu'il a conçus pour les traitements et échanges d'informations. Il a forcément déjà dû se poser la question du HID.

  3. #3
    Membre à l'essai
    Profil pro
    Inscrit en
    Avril 2007
    Messages
    32
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2007
    Messages : 32
    Points : 22
    Points
    22
    Par défaut
    Bonjour,
    Merci pour le "tuyau".
    Bon WE.
    Pierre

  4. #4
    Membre à l'essai
    Profil pro
    Inscrit en
    Avril 2007
    Messages
    32
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2007
    Messages : 32
    Points : 22
    Points
    22
    Par défaut
    Bonjour,
    Pour avancer, j'ai téléchargé Device Monitoring Studio pour espionner la liaison USB entre le programme (exe) fournis avec le relais et la carte Relais.
    Contrairement à ce que j'ai écrit au post1 la commande pour Relais1 ON est $FF $01 $00 $00 $00 $00 $00 $00.

    Voici le rapport Pour Relais1 ON
    Commande Relais1 ON.docx

    Avec la procedure TForm1.BtEnvoiClick j'arrive à visualiser ce qui se passe dans Packet 0, 1 et 3, mais après plus rien.

    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
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
     unit U_RWUsb_2;
    interface
    uses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls, Buttons,  ExtCtrls, ComCtrls,  AntHIDDeviceController;
    type
    TReport = packed record
      ReportID: byte; 
      Data: array [0..64] of byte;
    end;
     
       TCmdInfo = record
        ID: Integer;
        Flags: Byte;
        Changed: Byte;
      end;
     
      TForm1 = class(TForm)
        HidCtrl1: TAntHidDeviceController;
        M1: TMemo;
        BtInfo: TButton;
        BtEnvoi: TButton;
        procedure HidCtrl1DeviceChange(Sender: TObject);
        procedure BtInfoClick(Sender: TObject);
        procedure BtEnvoiClick(Sender: TObject);
     
      private
        { Déclarations privées }
      public
        { Déclarations publiques }
      CurrentDevice: TAntHidDevice;
      Dev:TAntHidDevice;
       published
     
      end;
    var
      Form1: TForm1;
      MyDevice: TAntHidDevice;
      TabOut: array[0..64] of Byte;
    implementation
     
    uses U_Info_2;
     
    {$R *.dfm}
     
    Const
    //  MyVendorID = $16C0;   // Carte ES
    //  MyProductID = $05DF;
        MyVendorID = $16C0;   // Carte Relais
        MyProductID = $05DF;
     
    procedure TForm1.HidCtrl1DeviceChange(Sender: TObject);
    var
    Devtrouve:Boolean;
    begin
    try
      Dev.Free;
      DevTrouve:=false;
      While HidCtrl1.CheckOut(Dev) do begin
       M1.Lines.Add(Dev.ProductName);
       if Dev.ProductName = 'USBRelay2' then begin
       //if Dev.ProductName = 'Es_Carte_1' then begin
     
        CurrentDevice:= Dev;
        DevTrouve:=true;
       end;
      end;
       if DevTrouve then begin
       end
        else begin
       if MessageDlg('Carte non branchée ou hors tension'+chr(13)+
       'Brancher la carte et choisir OUI'+ Chr(13)+
       'Pour quitter choisir NON',
        mtWarning	, [mbYes, mbNo], 0) = mrYes then begin
         Dev.Free;
         Application.Restore;
         HidCtrl1.DeviceChange;
         end
        else
         Form1.Close;
        end;
    except
     Application.MessageBox('Erreur','ATTENTION !',MB_OK);
     end;
    end;
     
    procedure TForm1.BtInfoClick(Sender: TObject);
    begin
    F_Info.Show;
    end;
     
    procedure TForm1.BtEnvoiClick(Sender: TObject);
    var
     Buf: array [0..64] of Byte;
     Written: Cardinal;
     ToWrite: Cardinal;
    begin
     if Assigned(CurrentDevice) then begin
      Buf[0] := StrToIntDef( '0', 0);
      ToWrite := CurrentDevice.Caps.OutputReportByteLength;
      Buf[1]:= $FF;
      Buf[2]:= $01;
      Buf[3]:= $00;
      Buf[4]:= $00;
      Buf[5]:= $00;
      Buf[6]:= $00;
      Buf[7]:= $00;
      Buf[8]:= $00;
     //if not CurrentDevice.WriteFile(Buf, ToWrite, Written) then //Err := GetLastError;
      CurrentDevice.WriteFile(Buf, ToWrite, Written);
     end;
     end;
    end.
    Je bloque sur ce qu'il faut envoyer à partir de Packet 3.
    Qui peu m'aider à comprendre comment établir cette liaison HID USB ?

  5. #5
    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
    As tu pensé utiliser ce relais via le port série ?
    Normalement tu as cette possibilité, tu dois avoir un port de com virtuel dans le gestionnaire de périphérique, ce serait plus simple.

  6. #6
    Membre à l'essai
    Profil pro
    Inscrit en
    Avril 2007
    Messages
    32
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2007
    Messages : 32
    Points : 22
    Points
    22
    Par défaut
    Bonjour et merci pour la réponse.
    J'ai ces 2 relais dont un est commandé par un port USB HID l'autre en Port Com virtuel (bien plus facile à exploiter).
    Je ne vois pas comment commander le premier en Com ?

    Nom : Relais_Usb.jpg
Affichages : 463
Taille : 20,8 Ko

  7. #7
    Membre à l'essai
    Profil pro
    Inscrit en
    Avril 2007
    Messages
    32
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2007
    Messages : 32
    Points : 22
    Points
    22
    Par défaut
    Bonjour à tous,
    A force de chercher, j'ai trouvé la source du programme EXE qui permet d'actionner ces relais.
    En compilant cette source, je bloque sur l'erreur suivante "Opérateur non applicable à ce type d'opérateur" à la ligne (pB + 1)^ := Mode;

    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
     function THidRlyForm.SendCmd(Relay, Mode: Byte): String;
    var
      pB: pByte;
      Dev:  TAntHidDevice;
      Status: Boolean;
    begin
      Dev := TAntHidDevice(lbBoards.Items.Objects[lbBoards.ItemIndex]);
      try
        pB := AllocMem(10);
    
         pB^      := 0;     
        (pB + 1)^ := Mode; //erreur suivante "Opérateur  non applicable à ce type d'opérateur"   :arf:
        (pB + 2)^ := Relay;  
    
        Status := Dev.SetFeature(pB^, 9);
    Qui pourrait me dire où est mon erreur ?

    Le code complet :
    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
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
     
    unit RelayCtlU;
     
    interface
     
    uses
      Windows, Messages, SysUtils, Variants,Classes, Graphics,
      Controls, Forms, Dialogs, StdCtrls,  ExtCtrls, Buttons,
     Spin, ComCtrls, AntHIDDeviceController;
     
    type
      TRLButtons = record
        Buttons: array [0..15] of TSpeedButton;
      end;
     
      THidRlyForm = class(TForm)
        Panel2: TPanel;
        Panel1: TPanel;
        lbBoards: TListBox;
        Panel3: TPanel;
        Log: TMemo;
        PC: TPageControl;
        tsChannelButtons: TTabSheet;
        tsAllOn: TTabSheet;
        BtnPan: TPanel;
        Label1: TLabel;
        buAllOn: TButton;
        buAllOff: TButton;
        seRelNo: TSpinEdit;
        buOn: TButton;
        buOff: TButton;
        Panel4: TPanel;
        sb1On: TSpeedButton;
        sb1Off: TSpeedButton;
        sb2On: TSpeedButton;
        sb2Off: TSpeedButton;
        sb3On: TSpeedButton;
        sb3Off: TSpeedButton;
        sb4On: TSpeedButton;
        sb4Off: TSpeedButton;
        Sb5On: TSpeedButton;
        sb5Off: TSpeedButton;
        sb6On: TSpeedButton;
        sb6Off: TSpeedButton;
        sb7On: TSpeedButton;
        sb7Off: TSpeedButton;
        sb8On: TSpeedButton;
        sb8Off: TSpeedButton;
        Panel5: TPanel;
        Panel6: TPanel;
        HidCtl: TAntHidDeviceController;
        procedure FormActivate(Sender: TObject);
        function HidCtlEnumerate(HidDev: TAntHidDevice; const Idx: Integer): Boolean;
        procedure HidCtlDeviceChange(Sender: TObject);
        procedure buAllOnClick(Sender: TObject);
        procedure sb1OnClick(Sender: TObject);
        procedure PCChange(Sender: TObject);
      private
        RlTtl: TPanel;
        function GetButtonStatus: Byte;
        function SendCmd(Relay, Mode: Byte): String;
        procedure InitButtons(var ButtonList: TRLButtons);
        { Private declarations }
      public
        { Public declarations }
      end;
     
    var
      HidRlyForm: THidRlyForm;
     
    implementation
     
    {$R *.dfm}
     
    const
      DEVICENAME = 'USBRelay'; // Base name for report ProductName
      Cmds:       array [0..3] of Byte = ({On}  $FF, {Off}  $FD,  {All ON} $FE, {All OFF} $FC);
      SubCmds:    array [0..1] of Byte = ({Off} $FD, {On}   $FF);
     
    procedure THidRlyForm.InitButtons(var ButtonList: TRLButtons);
    begin
      ButtonList.Buttons[ 0] := sb1On;
      ButtonList.Buttons[ 1] := sb1Off;
      ButtonList.Buttons[ 2] := sb2On;
      ButtonList.Buttons[ 3] := sb2Off;
      ButtonList.Buttons[ 4] := sb3On;
      ButtonList.Buttons[ 5] := sb3Off;
      ButtonList.Buttons[ 6] := sb4On;
      ButtonList.Buttons[ 7] := sb4Off;
      ButtonList.Buttons[ 8] := sb5On;
      ButtonList.Buttons[ 9] := sb5Off;
      ButtonList.Buttons[10] := sb6On;
      ButtonList.Buttons[11] := sb6Off;
      ButtonList.Buttons[12] := sb7On;
      ButtonList.Buttons[13] := sb7Off;
      ButtonList.Buttons[14] := sb8On;
      ButtonList.Buttons[15] := sb8Off;
    end;
     
    function THidRlyForm.GetButtonStatus: Byte;
    var
      Btns:  TRLButtons;
      Dev: TAntHidDevice;
      pB: pByte;
      Status: Boolean;
      Sb, i: Byte;
    begin
      if lbBoards.Count > 0 then
      begin
        Dev := TAntHidDevice(lbBoards.Items.Objects[lbBoards.ItemIndex]);
     
        try
          pB := AllocMem(10);
     
           pB^      := 0;
          (pB + 1)^ := 0; :arf::arf::arf:
          (pB + 2)^ := 0;
     
          Status := Dev.GetFeature(pB^, 9);
          if Status  then
          begin
            Sb := (pB + 8)^;
            InitButtons(Btns);
     
            for i := 0 to 7 do
            begin
              Btns.Buttons[(i * 2)    ].Down :=     Boolean(Sb and 1);
              Btns.Buttons[(i * 2) + 1].Down := not Boolean(Sb and 1);
              Sb := Sb shr 1;
            end;
          end;
        finally
          FreeMem(pB);
        end;
      end;
    end;
     
    procedure THidRlyForm.buAllOnClick(Sender: TObject);
    begin
      Log.Lines.Add(SendCmd(seRelNo.Value, Cmds[TWinControl(Sender).Tag - 1]));
    end;
     
    procedure THidRlyForm.FormActivate(Sender: TObject);
    var
      i: Integer;
    begin
      HidCtlDeviceChange(Self);
      PC.TabIndex := 0;
    end;
     
    // free all board objects, clear board list and re-enumerate boards
    procedure THidRlyForm.HidCtlDeviceChange(Sender: TObject);
    var
      i: Integer;
    begin
      for i := 0 to lbBoards.Items.Count - 1 do
        TAntHidDevice(lbBoards.Items.Objects[i]).Free;
     
      lbBoards.Clear;
      Log.Clear;
     
      Log.Lines.Add(Format('-- %s --', [DateTimeToStr(Now)]));
      HidCtl.Enumerate;
     
    //  Select the first board
      if lbBoards.Count = 0 then
        Log.Lines.Add('No relay boards present')
      else
        lbBoards.ItemIndex := lbBoards.Count - 1;
     
      BtnPan.Enabled := (lbBoards.Count > 0);
    end;
     
    function THidRlyForm.HidCtlEnumerate(HidDev: TAntHidDevice; const Idx: Integer): Boolean;
    var
      Bd: Integer;
    begin
    //  Show all USB devices in the log
      if HidDev.ProductName <> '' then
        Log.Lines.Add(HidDev.ProductName)
      else
        Log.Lines.Add(Format('VID=%4.4x, PID=%4.4x', [HidDev.Attributes.VendorID, HidDev.Attributes.ProductID]));
     
    //  Select only the devices we are interested in
      if Copy(HidDev.ProductName, 1, Length(DEVICENAME)) = DEVICENAME then
      begin
        Bd := lbBoards.Count;
        Bd := lbBoards.Items.Add(Format('%d - %s', [Bd, HidDev.ProductName]));
        HidCtl.CheckOutByIndex(HidDev, Idx);
        lbBoards.Items.Objects[Bd] := HidDev;
      end;
    //  Application.ProcessMessages;
      Result := True;
    end;
     
    procedure THidRlyForm.PCChange(Sender: TObject);
    var
      Stat: Byte;
      i, j: Integer;
    begin
      if PC.TabIndex = 1 then
        GetButtonStatus;
    end;
     
    procedure THidRlyForm.sb1OnClick(Sender: TObject);
    var
      Ch: Integer;
      On: Byte;
    begin
      Ch := ((TWinControl(Sender).Tag - 1) shr 1) + 1;
      On := (TWinControl(Sender).Tag and 1);
     
      Log.Lines.Add(SendCmd(Ch, SubCMds[On]));
    end;
     
    function THidRlyForm.SendCmd(Relay, Mode: Byte): String;
    var
      pB: pByte;
      Dev:  TAntHidDevice;
      Status: Boolean;
    begin
     { Dev := TAntHidDevice(lbBoards.Items.Objects[lbBoards.ItemIndex]);
      try
        pB := AllocMem(10);
     
         pB^      := 0;
        (pB + 1)^ := Mode;         :arf:
        (pB + 2)^ := Relay;
     
        Status := Dev.SetFeature(pB^, 9);
     
        if Status then
          Result := Format('%2.2x %2.2x %2.2x', [(pB + 0)^, (pB + 1)^, (pB + 2)^])
        else
          Result := 'Command not sent';
     
        Status := Dev.GetFeature(Pb^, 9);
        if Status then
          Result := Format('%s - %2.2x', [Result, (pB + 8)^]);
      finally
        FreeMem(pB);
      end;}
    end;
     
    end.

  8. #8
    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 301
    Points
    11 301
    Billets dans le blog
    6
    Par défaut
    Bonjour,
    Inc(PB) doit passer en faisant pointer sur l'élément suivant.
    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 !

  9. #9
    Membre à l'essai
    Profil pro
    Inscrit en
    Avril 2007
    Messages
    32
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2007
    Messages : 32
    Points : 22
    Points
    22
    Par défaut
    Bonjour,
    Merci pour cette solution qui fonctionne.

Discussions similaires

  1. Commande de relais par USB (sans driver – HID)
    Par gaby277 dans le forum Contribuez
    Réponses: 6
    Dernier message: 22/07/2022, 18h48
  2. Réponses: 1
    Dernier message: 09/07/2012, 11h56
  3. monter un clé USB avec la commande mount
    Par adilsoft dans le forum Shell et commandes GNU
    Réponses: 4
    Dernier message: 26/12/2009, 23h54
  4. hex et usb avec delphi
    Par Souhail_gsm dans le forum API, COM et SDKs
    Réponses: 0
    Dernier message: 20/04/2009, 20h17
  5. application modulable avec Delphi .NET
    Par skywaukers dans le forum Delphi .NET
    Réponses: 6
    Dernier message: 06/08/2007, 20h09

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