Archives par mot-clé : A4A

A4A est l’acronyme de « Ada for Automation », un cadriciel pour développer des applications d’automatisme évoluées dans le langage Ada.

A4A : Binding Ada pour le pilote des cartes Hilscher cifX

Bonjour,

Je travaille actuellement sur un binding Ada pour le pilote (cifX Device Driver) des cartes Hilscher cifX.

A ce jour, ce binding permet d’utiliser les cartes cifX depuis un programme en Ada pour les échanges cycliques, c’est à dire les échanges d’entrées et sorties avec la périphérie déportée sur les bus de terrain, ce qui est suffisant dans beaucoup d’applications d’automatisme.

Voilà un bout de code jeté à la va-vite pour vous donner une idée de la chose.

En Ada, lorsque l’on appelle une fonction comportant des paramètres, il est possible de les nommer, ce qui facilite la lecture et la compréhension ultérieure, et je trouve que ça les fait ressembler à des appels de fonctions en « Structured Text ».

Si l’on ôte toutes les traces, ce programme est très limité : il recopie quatre octets d’entrée dans quatre octets de sortie durant 60 secondes.

-----------------------------------------------------------------------
--                       Ada for Automation                          --
--                                                                   --
--                 Copyright (C) 2012, Stephane LOS                  --
--                                                                   --
-- This library is free software; you can redistribute it and/or     --
-- modify it under the terms of the GNU General Public               --
-- License as published by the Free Software Foundation; either      --
-- version 2 of the License, or (at your option) any later version.  --
--                                                                   --
-- This library 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 --
-- General Public License for more details.                          --
--                                                                   --
-- You should have received a copy of the GNU General Public         --
-- License along with this library; if not, write to the             --
-- Free Software Foundation, Inc., 59 Temple Place - Suite 330,      --
-- Boston, MA 02111-1307, USA.                                       --
--                                                                   --
-- As a special exception, if other files instantiate generics from  --
-- this unit, or you link this unit with other files to produce an   --
-- executable, this  unit  does not  by itself cause  the resulting  --
-- executable to be covered by the GNU General Public License. This  --
-- exception does not however invalidate any other reasons why the   --
-- executable file  might be covered by the  GNU Public License.     --
-----------------------------------------------------------------------


with Ada.Text_IO;

with A4A; use A4A;
with A4A.Protocols.HilscherX;

procedure Test_CifX is

   package cifX renames A4A.Protocols.HilscherX;

   Driver_Handle  : aliased cifX.Driver_Handle_Type;
   Channel_Handle : aliased cifX.Channel_Handle_Type;
   Result : DInt;

   Input_Data  : Byte_Array (0..9) := (others => 0);
   Output_Data : Byte_Array (0..9) := (others => 0);

   My_Timer : Integer := 10;

begin

   Ada.Text_IO.Put_Line (Item => "Test_CifX...");

   Ada.Text_IO.Put_Line (Item => "Initializing Linux Driver");
   Result := cifX.Linux_Driver_Init;
   if Result /= cifX.CIFX_NO_ERROR then
      cifX.Show_Error(Result);
   else

      Ada.Text_IO.Put_Line (Item => "Opening Driver");
      Result := cifX.Driver_Open (Driver_Handle'Access);
      if Result /= cifX.CIFX_NO_ERROR then
         cifX.Show_Error(Result);
      else

         Ada.Text_IO.Put_Line (Item => "Opening Channel");
         Result := cifX.Channel_Open
           (Driver_Handle          => Driver_Handle,
            Board_Name             => "cifX0",
            Channel_Number         => 0,
            Channel_Handle_Access  => Channel_Handle'Access);

         if Result /= cifX.CIFX_NO_ERROR then
            cifX.Show_Error(Result);
         else

            Ada.Text_IO.Put_Line (Item => "IO Exchange");
            loop
               Result := cifX.Channel_IO_Read
                 (Channel_Handle => Channel_Handle,
                  Area_Number    => 0,
                  Offset         => 0,
                  Data_Length    => 4, --Input_Data'Length,
                  Data_In        => Input_Data,
                  Time_Out       => 10);
               Ada.Text_IO.Put_Line (Item => "Channel_IO_Read");
               if Result /= cifX.CIFX_NO_ERROR then
                  cifX.Show_Error(Result);
               else
                  for Index in Input_Data'First .. 3 loop

                     Ada.Text_IO.Put ("Input_Data(" & Index'Img & ") : ");
                     Byte_Text_IO.Put (Input_Data(Index), Base => 16);
                     Ada.Text_IO.New_Line;

                     Output_Data(Index) := Input_Data(Index);
                  end loop;

                  Result := cifX.Channel_IO_Write
                    (Channel_Handle => Channel_Handle,
                     Area_Number    => 0,
                     Offset         => 0,
                     Data_Length    => 4, --Output_Data'Length,
                     Data_Out       => Output_Data,
                     Time_Out       => 10);
                  Ada.Text_IO.Put_Line (Item => "Channel_IO_Write");

                  if Result /= cifX.CIFX_NO_ERROR then
                     cifX.Show_Error(Result);
                  end if;
               end if;

               My_Timer := My_Timer - 1;
               exit when My_Timer <= 0;

               delay 1.0;
            end loop;

            Ada.Text_IO.Put_Line (Item => "Closing Channel");
            Result := cifX.Channel_Close (Channel_Handle);
            if Result /= cifX.CIFX_NO_ERROR then
               cifX.Show_Error(Result);
            end if;

         end if;

         Ada.Text_IO.Put_Line (Item => "Closing Driver");
         Result := cifX.Driver_Close (Driver_Handle);
         if Result /= cifX.CIFX_NO_ERROR then
            cifX.Show_Error(Result);
         end if;

      end if;

   end if;

   Ada.Text_IO.Put_Line (Item => "Deinitializing Linux Driver");
   cifX.Linux_Driver_Deinit;

end Test_CifX;

Cordialement,
Stéphane