⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 example.c

📁 PROFIBUS-dp PROGRAMS 主从站源程序
💻 C
字号:
/****************************************************************************

  File:           example.c

-----------------------------------------------------------------------------
  Created       : A. Beck,  Hilscher GmbH
  Date          : 28-Jul-2001

  Project       : example of accessing a DBM File with the DBM embedded API

-----------------------------------------------------------------------------

  Description:
    - reading a given DBM file
    - checking for which hardware it was made for
    - screening the device data sets of the file

  Functions:

       void main( int , char * );

-----------------------------------------------------------------------------

  Todo:

-----------------------------------------------------------------------------

  Changes:

    Name   Date          Version  Description
    -----------------------------------------------------------------------
    Ab     28-Jul-2001    1.000    created

****************************************************************************/


#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <conio.h>

#pragma pack (1)

#include "..\dbm_embd.src\dbm_embd.h"


/* buffer to hold the DBM file */
unsigned char abDbmBuffer[65535];

void main( int argc, char *argv[] )
{
  char   szSourceFile[32];
  FILE*  pFileHandle;
  unsigned short usRealSetLength;

  unsigned char  bHandleToSetGlobal;
  DBM_SET_GLOBAL tSetGlobal;

  unsigned char  bHandleToDeviceDataSets;
  unsigned short usNumOfDeviceDataSets;

  unsigned short usCounterA;
  unsigned short usCounterB;

  static unsigned char abDataSet[1024];

  /* check first the program arguments */
    if( --argc != 1)
  {
    printf("Wrong Parameter! Usage: main.exe dbmfilename.dbm");
    exit(1);
  }
  else
  {
    strcpy( szSourceFile, argv[1] );
  }

  if( (pFileHandle = fopen(szSourceFile,"r+b")) == NULL )
  {
    printf("Can't open source file!");
    exit(1);
  }

  /* read now the file to the internal buffer */
  fread(&abDbmBuffer,sizeof(abDbmBuffer),1,pFileHandle);
  fclose(pFileHandle);


  /******************************************************/
  /*          now start with the DBM-ACCESS             */
  /******************************************************/

  /* -------------------------------------------------------------- */
  /* initialization of the DBM structure need to be called first    */
  /* -------------------------------------------------------------- */
  /* Hint:                                                          */
  /* the file on disk contains 44 bytes file header                 */
  /* a dbm file on hardware does not contain this file header       */
  /* so the dbm file offset need to be corrected                    */

  if( dbm_init( (void *)&abDbmBuffer[44] ) != DBM_OK )
  {
    printf("More than 32 DBM-tables found in the DBM file!");
    exit(1);
  }

  /* ------------------------------------------------------------------ */
  /* as next read in the table GLOBAL where you can get the bus type    */
  /* ------------------------------------------------------------------ */

  if( dbm_get_num( "GLOBAL", &bHandleToSetGlobal ) != DBM_OK )
  {
    printf("Can't get handle to table GLOBAL.");
    exit(1);
  }
  else
  {
    if( dbm_fast_read (bHandleToSetGlobal,1,sizeof(DBM_SET_GLOBAL),(void*)&tSetGlobal,&usRealSetLength) != DBM_OK )
    {
      printf("Can't read table GLOBAL.");
      exit(1);
    }
    else
    {

      /* ---------------------------------------------------------------------- */
      /*   now select the bus type and read out the device parameter data sets  */
      /* ---------------------------------------------------------------------- */

      if( !memcmp( "CANopen ", &tSetGlobal.szSegmentName, 8) )
      {
        /* this DBM-File is a CANopen Master DBM - File */

        if( dbm_get_num("NODES",&bHandleToDeviceDataSets) != DBM_OK )
        {
          printf("Can't get handle to device parameter data sets.");
          exit(1);
        }

        if( dbm_get_num_of_set(bHandleToDeviceDataSets,&usNumOfDeviceDataSets) != DBM_OK )
        {
          printf("Can't get the number of device parameter data sets.");
          exit(1);
        }
      }
      else
      if( !memcmp( "PROFIBUS", &tSetGlobal.szSegmentName, 8)|| 
          !memcmp( "PB_COMBI", &tSetGlobal.szSegmentName, 8) 
        )   
      {
        /* this DBM-File is a PROFIBUS-DP DBM - File */
        if( dbm_get_num("SL_DP",&bHandleToDeviceDataSets) != DBM_OK )
        {
          printf("Can't get handle to device parameter data sets.");
          exit(1);
        }

        if( dbm_get_num_of_set(bHandleToDeviceDataSets,&usNumOfDeviceDataSets) != DBM_OK )
        {
          printf("Can't get the number of device parameter data sets.");
          exit(1);
        }
      }
      else
      if( !memcmp( "DNM     ", &tSetGlobal.szSegmentName, 8) )
      {
        /* this DBM-File is a DeviceNet Master DBM-File */
          if( dbm_get_num("DEVICES",&bHandleToDeviceDataSets) != DBM_OK )
        {
          printf("Can't get handle to device parameter data sets.");
          exit(1);
        }

        if( dbm_get_num_of_set(bHandleToDeviceDataSets,&usNumOfDeviceDataSets) != DBM_OK )
        {
          printf("Can't get the number of device parameter data sets.");
          exit(1);
        }
      }
      else
      {
        printf("Unknown segment name in DBM file.");
        exit(1);
      }


      printf("Found %d Device Parameter Data set(s)\n",usNumOfDeviceDataSets);


      for( usCounterA = 1; usCounterA <= usNumOfDeviceDataSets; usCounterA++ )
      {
        if( ( dbm_fast_read( bHandleToDeviceDataSets,
                             usCounterA,
                             sizeof( abDataSet ),
                             &abDataSet[0],
                             &usRealSetLength ) ) != DBM_OK )
        {
          printf("Can't read the device parameter data set.");
          exit(1);
        }

        printf("\nFound the device parameter data set of device: %d\n", abDataSet[0]);

        for( usCounterB = 1 ; usCounterB < usRealSetLength; usCounterB++ )
        {
          printf("%02XH ",abDataSet[usCounterB]);
        }

        printf("\nPress any Key to read next data set");
        while(!kbhit());
        getch();
      }
    }
  }

  printf("\n\nFinished in reading the DBM file, exiting now");

  exit(0);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -