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

📄 ccplog.c

📁 ccp
💻 C
📖 第 1 页 / 共 2 页
字号:
          printf("*** UNKNOWN COMMAND ***\n");
          break;
        }
    }



}

void ccpPrintEvent( BYTE *com ) {

  int i;

  if (com[0]!=0xFE) return;

  for (i=1;i<8;i++) {
    if (com[i]==0) {
      printf("\n");
      return;
    } else {
      printf("%c",com[i]);
    }
  }
}

void ccpPrintResponce( unsigned int time, BYTE *com ) {


  if (com[0]!=0xFF) return;

  printf("+%7gms: ",time/100.0);

  if (com[1]) {
    printf("ERROR: '%s' !!! ",ccpErrorStr(com[1]));
  }

  switch (gLastCmd) {

    case CC_BUILD_CHKSUM:
      {
        #define csum *(DWORD*)&com[4]
        SWAP_MOTOROLA(&csum,4)        ;
        printf("size = %02Xh, sum = %08Xh\n",com[3],csum);
      }
      break;

    case CC_EXCHANGE_ID:
      {
        printf("len = %u, resource availability mask=%02Xh, resource protection mask=%02Xh\n",com[3],com[5],com[6]);
      }
      break;

    case  CC_GET_CCP_VERSION:
      {
        printf("version = V%u.%u\n",com[3],com[4]);
        gCCPVersion = com[3]*100+com[4];
      }
      break;

    case CC_SHORT_UPLOAD:
    case CC_UPLOAD:
      {
        printf("data = {%02Xh,%02Xh,%02Xh,%02Xh,%02Xh}\n",com[3],com[4],com[5],com[6],com[7]);
      }
      break;

    case CC_GET_DAQ_SIZE:
      {
        if (com[3]) {
          printf("size = %u, firstPid = %u\n",com[3],com[4]);
        } else {
          printf("size = 0\n");
        }
      }
      break;

    case CC_GET_CAL_PAGE:
      {
        #define calpage *(DWORD*)&com[4]
        SWAP_MOTOROLA(&calpage,4);
        printf("addr = %u:%08Xh\n",com[3],calpage);
      }
      break;

    case CC_GET_S_STATUS:
      {
        printf("status = %02Xh, %02Xh, %02Xh\n",com[3],com[4],com[5]);
      }
      break;

    case CC_GET_SEED:
      {
        printf("protection status=%u, seed = %02X%02X%02X%02X\n",com[3],com[4],com[5],com[6],com[7]);
      }
      break;

    case CC_UNLOCK:
      {
        printf("current resource protection mask=%02X\n",com[3]);
      }
      break;

    default:
      {
        printf("%02X %02X %02X %02X %02X %02X %02X %02X \n",com[0],com[1],com[2],com[3],com[4],com[5],com[6],com[7]);
      }
      break;


   }
}

///////////////////////////////////////////////////////////////////////
// InitDriver ()
//---------------------------------------------------------------------
// Initializes the CAN driver interface

static Vstatus InitDriver(void)
{
  Vstatus vErr;
  VsetAcceptance acc;

  // Open the driver
  vErr = ncdOpenDriver ();
  if (vErr) goto error;

  // Get channel configuration of CCPlog
  vErr = ncdGetApplConfig("CCPlog",0,&gHwType,&gHwIndex,&gHwChannel);
  if (vErr) {
    vErr=ncdSetApplConfig("CCPlog",0,gHwType,gHwIndex,gHwChannel);
    if (vErr) {
      printf("ERROR: Registration failed\n");
      goto error;
    }
  }

  // Select the channel
  gChannelMask = ncdGetChannelMask(gHwType,gHwIndex,gHwChannel);
  if (gChannelMask==0) {
    printf("ERROR: No hardware channel assigned to CCPlog\n");
    goto error;
  }

  // Open a port
  vErr = ncdOpenPort(&gPortHandle,"CCPlog",gChannelMask,gBitRate>0?gChannelMask:0,&gPermissionMask,1024);
  if (vErr) goto error;

  // If permission to initialize
  if (gPermissionMask) {

    // Set the bus timing
    vErr = ncdSetChannelBitrate(gPortHandle,gPermissionMask,gBitRate);
    if (vErr) goto error;

    // Reset the clock
    vErr = ncdResetClock(gPortHandle);
    if (vErr) goto error;
  }

  // Open the acceptance filter for all standard ids
  acc.mask = 0; // relevant=1
  acc.code = 0;
  vErr = ncdSetChannelAcceptance(gPortHandle,gChannelMask,&acc);
  if (vErr) goto error;

  return VSUCCESS;


error:

  printf("ERROR: %s\n",ncdGetErrorString(vErr));

  if (gPortHandle!=INVALID_PORTHANDLE) {
    ncdClosePort(gPortHandle);
    gPortHandle = INVALID_PORTHANDLE;
  }

  return vErr;
}                                                  // end InitDriver ()



///////////////////////////////////////////////////////////////////////
// CleanUp()
//---------------------------------------------------------------------
// close the port and the CAN driver

static Vstatus CleanUp(void)
{
  ncdClosePort(gPortHandle);
  gPortHandle = INVALID_PORTHANDLE;
  ncdCloseDriver();
  return VSUCCESS; // No error handling
}                                                    // ..end CleanUp()


///////////////////////////////////////////////////////////////////////
// main()
//---------------------------------------------------------------------
//

// help
static void usage( void ) {

  printf(
    "\nUsage:\n"
    "  CCPlog [options]\n"
    "\n"
    "  Options:\n"
    "    -tx    Set output verbosity to x\n"
    "            1 - Print commands\n"
    "            2 - Print commands and responses\n"
    "            3 - Print commands, responses and all other can messages\n"
    "    -bx    Set bitrate to x (Default is not initialized)\n"
    "    -crox  Set CRO id to x (decimal)\n"
    "    -dtox  Set DTO id to x (decimal)\n"
    "    -verx  Set version number to x (default is 200)\n"
    "    -m     Set byte order to Motorola\n"
    "  Keys:\n"
    "    0-3    Set output verbosity to x\n"
    "    s      Print Statistics\n"
    );

    exit(0);
}

int main( int argc, char *argv[])
{
  Vstatus       vErr;
  Vevent        *pEvent =0, event;
  int           end = 0, i;
  HANDLE        h = 0;
  unsigned long b;

  printf(
    "CCPlog - The CCP Logging Utility\n"
    "Vector Informatik GmbH, 1997-2001, Za\n\n"
    "Build " __DATE__ " " __TIME__ "\n"
  );

  // commandline
  for (i=1; i<argc; i++) {
      char c;
      if (strcmp(argv[i], "-h") == 0) {
          usage();
      } else if (strcmp(argv[i], "-m") == 0) {
          gMotorola = 1;
      } else if (strcmp(argv[i], "-l") == 0) {
          gLogging = 1;
      } else if (strcmp(argv[i], "-o") == 0) {
          gOffline = 1;
      } else if (sscanf(argv[i], "-b%u", &b) == 1) {
          gBitRate = b;
      } else if (sscanf(argv[i], "-cro%u", &b) == 1) {
          gCroId = b;
      } else if (sscanf(argv[i], "-dto%u", &b) == 1) {
          gDtoId = b;
      } else if (sscanf(argv[i], "-ver%u", &b) == 1) {
          gCCPVersion = b;
      } else if (sscanf(argv[i], "-t%c", &c) == 1) {
          gDebugLevel = c-'0';
          printf("Output verbosity level = %u\n",gDebugLevel);
      } else {
          usage();
      }
  }

  printf("\n");
  if (gBitRate) printf("Bitrate = %u bps\n",gBitRate);
  if (gCroId) printf("CRO Id  = %u (%04Xh)\n",gCroId,gCroId);
  if (gDtoId) printf("DTO Id  = %u (%04Xh)\n",gDtoId,gDtoId);
  if (gLogging) printf("LOG file = CCPLOG.LOG\n");
  if (gOffline) printf("Offline Mode\n");

  // Create the log file
  if (gLogging || gOffline) {
    if (gLogging) gLogFile = fopen("CCPLOG.LOG","wb");
    if (gOffline) gLogFile = fopen("CCPLOG.LOG","rb");
    if (gLogFile==0) {
      printf("ERROR: Could not open file CCPLOG.LOG!\n");
      goto error;
    }
  }

  if (!gOffline) {

    // initialize the CAN driver
    vErr = InitDriver();
    if (vErr) goto error;

    // create a synchronisation object
    h = CreateEvent(NULL, FALSE, FALSE, NULL);
    vErr = ncdSetNotification(gPortHandle, &h, 1);
    if (vErr) goto ncdError;

    // channel on bus
    vErr = ncdActivateChannel(gPortHandle,gChannelMask);
    if (vErr) goto ncdError;
  }

  // main loop
  initStat();
  while (!end) {

    // wait for receive event
    if (h) WaitForSingleObject(h,100);

    // receive
    for (;;) { // read all events

      if (gOffline) {
        if (0==fread(&event,1,sizeof(Vevent),gLogFile)) {
          end = 1;
          break;
        }
        pEvent = &event;
      }

      else {
        vErr = ncdReceive1(gPortHandle,&pEvent);
        if (vErr&&vErr!=VERR_QUEUE_IS_EMPTY) goto ncdError;
        if (vErr==VERR_QUEUE_IS_EMPTY) break;
      }

      // screen output
      if (gDebugLevel>=4&&pEvent->tag!=V_TIMER) {
        printf("%s\n",ncdGetEventString(pEvent));
      }

      // check for the ccp cro or dto message
      if (pEvent->tag==V_RECEIVE_MSG) {

        gMsgCount++;

        // Logging
        if (gLogging) fwrite(pEvent,1,sizeof(Vevent),gLogFile);

        // Autodetect CRO and DTO identifiers
        if (gCroId==0) {
          if (pEvent->tagData.msg.data[0]==CC_CONNECT) {
            gCroId = pEvent->tagData.msg.id;
            printf("Autodetect CRO Id  = %u (%04Xh)\n",gCroId,gCroId);
          }
        }
        if (gDtoId==0 && gCroId!=0) {
          if (pEvent->tagData.msg.data[0]==0xFF) {
            gDtoId = pEvent->tagData.msg.id;
            printf("Autodetect DTO Id  = %u (%04Xh)\n",gDtoId,gDtoId);
          }
        }

        // Check for queue overruns
        if (pEvent->tagData.msg.flags&MSGFLAG_OVERRUN) {
          ncdFlushReceiveQueue(gPortHandle);
          gLastCmd = 0;
          gLastTime = 0;
          printf("Queue overflow, messages lost\n");
        }

        // CCP command
        if (pEvent->tagData.msg.id==gCroId) {
          gCroCount++;
          gLastCmd = pEvent->tagData.msg.data[0];
          gLastTime = pEvent->timeStamp;
          if (gDebugLevel>=1) ccpPrintCommand(pEvent->timeStamp,&pEvent->tagData.msg.data[0]);
        }

        // CCP responce
        else if (pEvent->tagData.msg.id==gDtoId) {
          gDtoCount++;
          if (pEvent->tagData.msg.data[0]==0xFF) {
            if (gLastCmd&&gLastTime) setStat(gLastCmd,pEvent->timeStamp-gLastTime);
            if (gDebugLevel>=2) ccpPrintResponce(pEvent->timeStamp-gLastTime,&pEvent->tagData.msg.data[0]);
            gLastCmd = 0;
            gLastTime = 0;
          }
          else if (pEvent->tagData.msg.data[0]==0xFE) {
            ccpPrintEvent(&pEvent->tagData.msg.data[0]);
          }
        }

        // Other
        else if (gDebugLevel==3 && gLastCmd) {
          printf("%s\n",ncdGetEventString(pEvent));
        }

      } // V_RECEIVE_MSG

    } // for

    // Keyboard
    if (kbhit()) {
      int key;
      key = getch();
      switch (key) {
        case '0':
        case '1':
        case '2':
        case '3':
        case '4':
          gDebugLevel = key-'0';
          break;
        case 's':
        case 'S':
          printStat();
          break;
        case 'h':
        case 'H':
          usage();
          break;
       case 27:
          end = 1;
          break;
        default:
          break;
      }
    }

  } // while (!end)


error:

  if (!gOffline) {
    ncdDeactivateChannel(gPortHandle, gChannelMask);
    CleanUp();
  }
  if (gLogging || gOffline) fclose(gLogFile);
  return 0;

ncdError:
  printf("ERROR: %s\n",ncdGetErrorString(vErr));
  goto error;

}



⌨️ 快捷键说明

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