Home
Categories
Dictionary
Glossary
Download
Project Details
Changes Log
What Links Here
FAQ
License

Basic C tutorial



The first tutorial presented the coding and configuration of a very simple system with two modules. In this C tutorial, one of the modules will be reimplemented in C, using a basic C module.

Overview

In the first tutorial, we had two modules, both of which implemented in Java:
  • The PublishModule module increments or decrements a value cyclically
  • The EventModule module allows to click on a toggle to set if the first module should increment or decrement the value, and shows the value
We will reimplement the PublishModule in C.

Architecture

The first tutorial had two applications coded in Java:
  • The first PublishModule which:
    • Increment or decrement the value
    • Publish cyclically the value
    • Listen to the toggle event to set if the value should increment or decrement
  • The second EventModule which:
    • Subscribe to the published value and show this value
    • Show a toggle and sends an event when the user clicks on this toggle

tuto1java
We will reimplement the PublishModule in C:
tutoC

XML configuration

Only the applications.xml XML file has to be modified to switch from a Java module implementation to a basic C module implementation.

Applications definition

We will change the applications.xml XML file, by modifying the PublishModule configuration:
      <application name="publishAppli">
         <modules>
            <cModule name="PublishModule">
               <cImplementation path="PublishModule" />
               <interfaces>
                  <eventReceived service="event"/>
                  <cyclic service="published" frequency="200ms" attach="attach"/>
               </interfaces>
            </cModule>
         </modules>
      </application>
As you can see, we specified that the implementation of the PublishModule is in the PublishModule.dll dll, in the same directory as the application.xml file.

Develop the PublishModule in C

Create the CodeBlocks project

We will first create a new PublishModule project in Visual Studio. See the Setting a CodeBlocks project for a C module for more information on how to do it.

Define the header

We can see that:
  • The module is notified from a boolean reception, hence the startNotify(long service), endNotify(), andreceiveBoolean(char* dataName, int value) functions must be defined
  • An int value is modified and a service is invoked, hence the setIntInvoker(void (*intInvokerInst)(char*, int)) function callback must be defined
  • There is a cyclic service, hence a publish(long service) function must be defined


We have therefore the following header:
      __declspec(dllexport) void start(void (*startInvoke)(long), void (*endInvoke));

      __declspec(dllexport) void setIntInvoker(void (*intInvokerInst)(char*, int));

      __declspec(dllexport) void end();

      __declspec(dllexport) void startNotify(long service);

      __declspec(dllexport) void receiveInt(char* data, int value);

      __declspec(dllexport) void receiveBoolean(char* data, int value);

      __declspec(dllexport) void endNotify(long service);

      __declspec(dllexport) void publish(long service);

      static int count = 0;
      static int step = 1;
      static void (*startInvokeInst)(long);
      static void (*endInvokeInst)();
      static void (*intInvokerInst)(char*, int);

Define the source

      #include <stdio.h>
      #include "module1.h"

      void start(void (*startInvoke)(long), void (*endInvoke)()) {
        startInvokeInst = startInvoke;
        endInvokeInst = endInvoke;
      }

      void end() {
      }

      void setIntInvoker(void (*intInvoker)(char*, int)) {
        intInvokerInst = intInvoker;
      }

      void startNotify(long service) {
      }

      void receiveBoolean(char* data, int value) {
        if (value) {
          step = -1;
        } else {
          step = 1;
        }
      }

      void endNotify(long service) {
      }

      void publish(long service) {
        count += step;
        startInvokeInst(2);
        intInvokerInst("value", count);
        endInvokeInst();
      }

See also


Categories: tutorials

Copyright 2017-2020 Dassault Aviation. All Rights Reserved. Documentation and source under the LGPL v3 licence