Beispiele in C unter Linux

Die Beispielanwendungen sind im Archiv der Treiberdateien enthalten. Um Ihnen einen ersten eindruck über die Ansteuerung zu verschaffen haben wir hier exemplarisch eines der Beispiele aufgeführt.

Beispiel für PCIOPTOREL16 / PCIOPTO16IO

 
// (1) Install the PCI driver
// (2) Add file "qlib.c" to your makefile.
//

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include "../qlib/qlib.h"


#include <sys/select.h>

int kbhit(void)
{
  struct timeval tv;
  fd_set read_fd;

  /* Do not wait at all, not even a microsecond */
  tv.tv_sec=0;
  tv.tv_usec=0;

  /* Must be done first to initialize read_fd */
  FD_ZERO(&read_fd);

  /* Makes select() ask if input is ready:
   * 0 is the file descriptor for stdin    */
  FD_SET(0,&read_fd);

  /* The first parameter is the number of the
   * largest file descriptor to check + 1. */
  if(select(1, &read_fd, NULL, NULL, &tv) == -1)
    return 0;   /* An error occured */

  /*    read_fd now holds a bit map of files that are
   * readable. We test the entry for the standard
   * input (file 0). */
  if(FD_ISSET(0,&read_fd))
    /* Character pending on stdin */
    return 1;

  /* no characters were pending */
  return 0;
}

int main(int argc, char **argv)
{
        ULONG handle;
        char ch;
	int j;
        
	//
        // This sample works for PCIOPTOREL16 and PCIOPTO16IO cards
        //

        //
        // Open the USB Module
        //

        handle = QAPIExtOpenCard(PCIOPTOREL16,0);

        if ( handle == 0 )
           {
              handle = QAPIExtOpenCard(PCIOPTO16IO,0);
           }

        //
        // If there are no modules terminate application
        //

        if ( handle == 0 )
           {
            printf("No supported PCI Card found!\n");
            return FALSE;
           }

        // Ok, we found a QUANCOM PCI Card

        // ---------------------------------------------------------------------------
        // PART 1: Setting the outputs
        //
        // The following constants can be used to program the outputs:
        // ---------------------------------------------------------------------------

        #define OUT1        0x1
        #define OUT2        0x2
        #define OUT3        0x4
        #define OUT4        0x8
        #define OUT5        0x10
        #define OUT6        0x20
        #define OUT7        0x40
        #define OUT8        0x80
        #define OUT9        0x100
        #define OUT10       0x200
        #define OUT11       0x400
        #define OUT12       0x800
        #define OUT13       0x1000
        #define OUT14       0x2000
        #define OUT15       0x4000
        #define OUT16       0x8000

        ULONG lines = 0;

        //
        // Reset all lines to "Low"
        //

        printf("Reset all lines to 'Low' ( Press return to continue ):\n");

        QAPIExtWriteDO16(handle, 0, lines, 0);

        ch = getchar();

        //
        // Set the outputs OUT4,OUT10 and OUT15 to "High" ( 16-Bit )
        //

        printf("Set OUT4,OUT10 and OUT15 to 'High' ( Press return to continue ):\n");

        lines = OUT4 + OUT10 + OUT15;

        QAPIExtWriteDO16(handle, 0, lines, 0);

        ch = getchar();

        //
        // Set the output OUT1, OUT4,OUT10 and OUT15 to "High" ( 16-Bit )
        //

        printf("Set OUT1, OUT4,OUT10 and OUT15 to 'High' ( Press return to continue ):\n");

        lines = OUT1 + OUT4 + OUT10 + OUT15;

        QAPIExtWriteDO16(handle, 0, lines, 0);

        ch = getchar();

        //
        // Reset line OUT10 to "Low"
        //

        printf("Reset line OUT10 to 'Low' ( Press return to continue ):\n");

        QAPIExtWriteDO1(handle, 10 - 1, FALSE, 0);

        ch = getchar();

        //
        // Set line OUT5 to "High"
        //

        printf("Set line OUT5 to 'High' ( Press return to continue ):\n");

        QAPIExtWriteDO1(handle, 5 - 1, TRUE, 0);

        ch = getchar();

        //
        // Reset all lines to "Low"
        //

        printf("Reset all to 'Low' ( Press return to continue ):\n");

        lines = 0;

        QAPIExtWriteDO16(handle, 0, lines, 0);

        ch = getchar();


        // ---------------------------------------------------------------------------
        // PART 2: Reading the inputs ( and detect changed inputs )
        //
        // This part reads the state of the input lines.
        //
        // The following constants can be used to program the inputs:
        // ---------------------------------------------------------------------------

        int i;

        while (!kbhit())
                {

                        // read the current state from the inputs


                        lines = QAPIExtReadDI16(handle, 0, 0);
                        printf("\n--------------------------------------------------------------------------\n");

                        printf("Current input states\n");
                        printf("IN1  IN2  IN3  IN4  IN5  IN6  IN7  IN8  IN9 IN10 IN11 IN12 IN13 IN14 IN15\n");

                        for (i=0;i<15;i++)
                            {
                               if ( lines & 1<<i)
                                  {
                                     printf(" 1   ");
                                  }
                               else
                                  {
                                     printf(" 0   ");
                                  }
                             }

                        // read the state change ff from the input lines

                        lines = QAPIExtSpecial(handle, JOB_READ_IN_FFS, 0, 0);

                        printf("\n--------------------------------------------------------------------------");

                        printf("\nInput change detection 1 = Input has changed its state since last reading\n");
                        printf("IN1  IN2  IN3  IN4  IN5  IN6  IN7  IN8  IN9 IN10 IN11 IN12 IN13 IN14 IN15\n");
                        for (i=0;i<15;i++)
                                {
                                        if ( lines & 1<<i)
                                                {
                                                        printf(" 1   ");
                                                }
                                        else
                                                {
                                                        printf(" 0   ");
                                                }
                                }

                        printf("\n--------------------------------------------------------------------------\n");

                        printf("Press return to stop reading the inputs every 5 seconds ...\n");

                        for (j=0;(j<5) && !kbhit();j++)
                                {
                                        sleep(1);
                                }

                }

                ch = getchar();

                // ---------------------------------------------------------------------------
                // PART 3: Enabling and disabling the timeout control
                //
                // The timeout control is a security feature. If the application
                // crashes or fails to set the outputs in a predefined time
                // the module resets the outputs to 'low'. This is to prevent that
                // connected devices ( i.e. motors, heaters, lamps, ... )
                // continue to run when the software has lost control.
                // ---------------------------------------------------------------------------

                printf("Testing timeout function ...\n");

                printf("Set OUT1,OUT2,OUT5,OUT10 and OUT15 to 'high'\n");

                // writing to OUTx pins

                QAPIExtWriteDO16(handle, 0, OUT1+OUT2+OUT5+OUT10+OUT15, 0);

                printf("Setting timout time to 16s\n");

                // first disable timeout

                QAPIExtSpecial(handle, JOB_DISABLE_TIMEOUT, 0, 0);

                // now, set timeout time

                lines = QAPIExtSpecial(handle, JOB_SET_WATCHDOG_TIME, 8, 0);

                printf("Enabling the timout control.\n");

                // reset timeout status bit, this may be set by previous testing

                QAPIExtSpecial(handle, JOB_RESET_TIMEOUT_STATUS, 0, 0);

                // enable timeout control

                QAPIExtSpecial(handle, JOB_ENABLE_TIMEOUT, 0, 0);

                // this part waits for 25s without any write to the usb module
                // after 16 seconds the hardware watchdog on the board
                // sets all outputs to 'low'

                printf("Now we wait for 25 seconds. This simulates a crash !\n");

                for (j=0;j<25;j++)
                        {
                                sleep(1);
                                printf(".");
                        }

                printf("\nAll OUT Pins should be reset now ! Press a key to continue !");

                ch = getchar();

                ULONG status = QAPIExtSpecial(handle, JOB_READ_TIMEOUT_STATUS, 0, 0);

                if ( status )
                        printf("\nTimeout flag was set\n");
                else
                        printf("\nNo timeout flag set\n");

                // first disable timeout

                QAPIExtSpecial(handle, JOB_DISABLE_TIMEOUT, 0, 0);

                // reset timeout status bit

                QAPIExtSpecial(handle, JOB_RESET_TIMEOUT_STATUS, 0, 0);

                printf("\nReady ! Press a key to continue !");

                ch = getchar();

                QAPIExtCloseCard(handle);

  return 0;
}