Esempio n. 1
0
tcCanVector::tcCanVector(tcTimerCan* pTimerCan,tcParamCan* pParamCan):tcCanUUDT(pTimerCan,pParamCan)
{
  ASSERT(pParamCan != NULL);
  if (!strcmp(pParamCan->CanCardName,cCanCardVECTORCanAC2PciXPort0))
  {
    pParamCan->Port = 0x0600;
  }
  else if (!strcmp(pParamCan->CanCardName,cCanCardVECTORCanAC2PciXPort1))
  {
    pParamCan->Port = 0x0601;
  }
  else if (!strcmp(pParamCan->CanCardName,cCanCardVECTORCanCardXPort0))
  {
    pParamCan->Port = 0x0200;
  }
  else if (!strcmp(pParamCan->CanCardName,cCanCardVECTORCanCardXPort1))
  {
    pParamCan->Port = 0x0201;
  }
  else if (!strcmp(pParamCan->CanCardName,cCanCardVECTORCanCardXLPort0))
  {
    pParamCan->Port = 0x0F00;
  }
  else if (!strcmp(pParamCan->CanCardName,cCanCardVECTORCanCardXLPort1))
  {
    pParamCan->Port = 0x0F01;
  }
  else if (!strcmp(pParamCan->CanCardName,cCanCardVECTORCanBoardXLPort0))
  {
    pParamCan->Port = 0x1900;
  }
  else if (!strcmp(pParamCan->CanCardName,cCanCardVECTORCanBoardXLPort1))
  {
    pParamCan->Port = 0x1901;
  }
  else if (!strcmp(pParamCan->CanCardName,cCanCardVECTORCanCaseXLPort0))
  {
    pParamCan->Port = 0x1500;
  }
  else if (!strcmp(pParamCan->CanCardName,cCanCardVECTORCanCaseXLPort1))
  {
    pParamCan->Port = 0x1501;
  }
  memcpy(&m_ParamCan,pParamCan,sizeof(m_ParamCan));

  m_Arret=cFaux;
  m_ThreadID=0;
  int gHwType                 = pParamCan->Port>>8;           // HWTYPE
  int gHwChannel              = (pParamCan->Port&0xFF)%2;     // 2 ports par carte
  int gHwIndex                = (pParamCan->Port&0xFF)/2;     // N° de carte
  Vstatus Statut;
  VsetAcceptance acc;
  Vaccess gPermissionMask     = 0;
  m_gChannelMask              = 0;
  m_gPortHandle               = INVALID_PORTHANDLE;
//  m_iErrorFrameTimeoutTimerID = cNumeroTempoInvalide;

  m_NbCarteChargee++;

  //  On initialise le Driver
  Statut = ncdOpenDriver();
  if (Statut!=VSUCCESS)
  {
    char Buffer[256];
    sprintf(Buffer,"tcCanVector::tcCanVector> Can driver failed (%04X)",Statut);
    SetMsg(cErrorMsg,Buffer);
    return;
  }
  m_CartePresente = cVrai;

  // On cherche le masque du canal Cancardx d'index 0 et de canal Port
  m_gChannelMask = ncdGetChannelMask(gHwType,gHwIndex,gHwChannel);
  if (m_gChannelMask==0) 
  {
    char Buffer[256];
    sprintf(Buffer,"tcCanVector::tcCanVector> %s not found",m_ParamCan.CanCardName);
    SetMsg(cErrorMsg,Buffer);
    return;
  }
  gPermissionMask = m_gChannelMask;
  // On ouvre le port
  Statut = ncdOpenPort(&m_gPortHandle,"JCAE",m_gChannelMask,m_gChannelMask,&gPermissionMask,pParamCan->QueueRead);
  if (Statut!=VSUCCESS)
  {
    char Buffer[256];
    sprintf(Buffer,"tcCanVector::tcCanVector> Can port failed (%04X)",Statut);
    SetMsg(cErrorMsg,Buffer);
    return;
  }
  if (gPermissionMask!=m_gChannelMask)
  {
    SetMsg(cErrorMsg,"tcCanVector::tcCanVector> Can card configuration unauthorized");
    return;
  }
  // On configure la vitesse
  Statut = ncdSetChannelBitrate(m_gPortHandle,m_gChannelMask,pParamCan->BaudRate);
  if (Statut!=VSUCCESS)
  {
    char Buffer[256];
    sprintf(Buffer,"tcCanVector::tcCanVector> Baudrate configuration failed (%04X)",Statut);
    SetMsg(cErrorMsg,Buffer);
    return;
  }
  // Inhiber les notifications TX et TXRQ
  Statut = ncdSetChannelMode(m_gPortHandle,m_gChannelMask,0,0);
  if (Statut!=VSUCCESS)
  {
    char Buffer[256];
    sprintf(Buffer,"tcCanVector::tcCanVector> Tx and TXRQ disabling failed (%04X)",Statut);
    SetMsg(cErrorMsg,Buffer);
    return;
  }
  // On configure les masques d'acceptances
  acc.mask = pParamCan->MaskStd; // relevant=1
  acc.code = pParamCan->CodeStd;
  Statut = ncdSetChannelAcceptance(m_gPortHandle,m_gChannelMask,&acc);
  if (Statut!=VSUCCESS)
  {
    char Buffer[256];
    sprintf(Buffer,"tcCanVector::tcCanVector> STD Mask id configuration failed (%04X)",Statut);
    SetMsg(cErrorMsg,Buffer);
    return;
  }
  acc.mask = 0x80000000 | pParamCan->MaskXtd; // relevant=1
  acc.code = 0x80000000 | pParamCan->CodeXtd;
  Statut = ncdSetChannelAcceptance(m_gPortHandle,m_gChannelMask,&acc);
  if (Statut!=VSUCCESS)
  {
    char Buffer[256];
    sprintf(Buffer,"tcCanVector::tcCanVector> XTD Mask id configuration failed (%04X)",Statut);
    SetMsg(cErrorMsg,Buffer);
    return;
  }
  // On vide la queue de réception
  Statut = ncdFlushReceiveQueue(m_gPortHandle);
  if (Statut!=VSUCCESS)
  {
    char Buffer[256];
    sprintf(Buffer,"tcCanVector::tcCanVector> Reception queue erasing failed (%04X)",Statut);
    SetMsg(cErrorMsg,Buffer);
    return;
  }
  // On vide la queue de transmission
  Statut = ncdFlushTransmitQueue(m_gPortHandle,m_gChannelMask);
  if (Statut!=VSUCCESS)
  {
    char Buffer[256];
    sprintf(Buffer,"tcCanVector::tcCanVector> Emission queue erasing failed (%04X)",Statut);
    SetMsg(cErrorMsg,Buffer);
    return;
  }
  // On crée l'évènement pour recevoir
  m_gEventHandle = CreateEvent(NULL, FALSE, FALSE, NULL);
  Statut = ncdSetNotification(m_gPortHandle,(unsigned long*)&m_gEventHandle, 1);
  if (Statut!=VSUCCESS)
  {
    char Buffer[256];
    sprintf(Buffer,"tcCanVector::tcCanVector> Reception event configuration failed (%04X)",Statut);
    SetMsg(cErrorMsg,Buffer);
    return;
  }
  // On initialise le corps du thread
  m_hThread = CreateThread( NULL,0,CorpsThread,(LPVOID)this,0,&m_ThreadID);
  if(m_hThread==INVALID_HANDLE_VALUE)
  {
    SetMsg(cErrorMsg,"tcCanVector::tcCanVector> Reception thread creation failed");
    return;
  }
  // On active le canal
  Statut = ncdActivateChannel(m_gPortHandle,m_gChannelMask);
  if (Statut!=VSUCCESS)
  {
    char Buffer[256];
    if(Statut == VERR_PORT_IS_OFFLINE)
    {
        sprintf(Buffer,"tcCanVector (Error : %04X) > !!!!!!!!!!!!!!!!! Please, Plug the CAN Cable !!!!!!!!!!!!!!!!!",Statut);
    }
    else
    {
        sprintf(Buffer,"tcCanVector::tcCanVector> Channel activation failed (%04X)",Statut);
    }
    SetMsg(cErrorMsg,Buffer);
    return;
  }

  m_StatutReq = VSUCCESS;
  m_InitOk=cVrai;

}
Esempio n. 2
0
///////////////////////////////////////////////////////////////////////
// main()
//---------------------------------------------------------------------
//
int main(int argc, char *argv[])
{
  Vstatus       vErr;
  int           end, i;
  unsigned int  u;
  unsigned int lastRxCount;
  unsigned int time, lastTime;


  printf(
         "CANcount (Built at " __DATE__ " " __TIME__ ")\n"
        );

  // parse the command line
  for (i=1; i<argc; i++) {
    int tmpL;

    if (_stricmp(argv[i], "-h") == 0) {
      usage();
    }
    else if (_stricmp(argv[i], "-v") == 0) {
      gHwType = HWTYPE_VIRTUAL;
    }
    else if (sscanf(argv[i], "-hw%d", &tmpL) == 1) {
      gHwType = tmpL;
    }
    else if (_stricmp(argv[i], "-2") == 0) {
      gHwChannel = 1;
      printf("Using channel 2.\n");
    }
    else if (_stricmp(argv[i], "-l") == 0) {
      gLogEvents = 1;
      printf("Logging activated\n");
    }
    else if (sscanf(argv[i], "-b%u", &u) == 1) {
      if (u>5000 && u<=1000000)
        gBitRate = u;
      else
        usage();
    }
    else if (sscanf(argv[i], "-f%s", &gLogFileName) == 1) {
      gLogFile = fopen(gLogFileName, "wb");
    }
    else {
      usage();
    }
  }
  if (gHwType < 0) usage();
  
  printf("Hardware = %u\n", gHwType);
  printf("Channel = %u\n", gHwChannel+1);
  printf("Bitrate = %u BPS\n", gBitRate);
  if (gLogFile) printf("Logfile = %s\n", gLogFileName);
  printf("\n");

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

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

  // create a thread
  threadHandle = CreateThread(0, 0x1000, thread, 0, 0, &threadId);
  SetThreadPriority(threadHandle, THREAD_PRIORITY_NORMAL);

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

  // main loop
  end = 0;
  lastTime = GetTickCount();
  lastRxCount = 0;

  while (!end) {
    Sleep(500);
    time = GetTickCount();
    if (time != lastTime) {
      printf("RX = %u msg/s, OVERFLOWS = %u    \r",
             ((gRxCount-lastRxCount)*1000)/(time-lastTime),
             gRxOverflows);
      lastTime = time;
      lastRxCount = gRxCount;
    }

    // check keyboard
    if (_kbhit()) {
      switch (_getch()) {
        case 27:
          end = 1;
          break;
        default:
          break;
      }
    }
  }


error:
  // stop thread
  if (threadHandle) {
    threadRuning = 0;
    WaitForSingleObject(threadHandle, 5000);
  }

  ncdDeactivateChannel(gPortHandle, gChannelMask);
  CleanUp();
  if (gLogFile) fclose(gLogFile);
  return 0;

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

}
Esempio n. 3
0
//********************************************************************//
// Reinit can card needed for BUS OFF by example
//********************************************************************//
void tcCanVector::ReinitCard()
{
  m_CanCardMutex.Lock();
  //---- Deactivate channel previously loaded ----
  ncdDeactivateChannel(m_gPortHandle,m_gChannelMask);
  ncdClosePort(m_gPortHandle);
  m_gPortHandle = INVALID_PORTHANDLE;
  ncdCloseDriver();

  //---- Initialise channel -----
  int gHwType                 = m_ParamCan.Port>>8;           // HWTYPE
  int gHwChannel              = (m_ParamCan.Port&0xFF)%2;     // 2 ports par carte
  int gHwIndex                = (m_ParamCan.Port&0xFF)/2;     // N° de carte
  Vstatus Statut;
  VsetAcceptance acc;
  Vaccess gPermissionMask     = 0;
  m_gPortHandle               = INVALID_PORTHANDLE;

  Statut = ncdOpenDriver();
  if (Statut!=VSUCCESS)
  {
    char Buffer[256];
    sprintf(Buffer,"tcCanVector::tcCanVector> Can driver failed (%04X)",Statut);
    SetMsg(cErrorMsg,Buffer);
    return;
  }

  // On cherche le masque du canal Cancardx d'index 0 et de canal Port
  m_gChannelMask = ncdGetChannelMask(gHwType,gHwIndex,gHwChannel);
  if (m_gChannelMask==0) 
  {
    char Buffer[256];
    sprintf(Buffer,"tcCanVector::tcCanVector> %s not found",m_ParamCan.CanCardName);
    SetMsg(cErrorMsg,Buffer);
    return;
  }
  gPermissionMask = m_gChannelMask;
  // On ouvre le port
  Statut = ncdOpenPort(&m_gPortHandle,"JCAE",m_gChannelMask,m_gChannelMask,&gPermissionMask,m_ParamCan.QueueRead);
  if (Statut!=VSUCCESS)
  {
    char Buffer[256];
    sprintf(Buffer,"tcCanVector::tcCanVector> Can port failed (%04X)",Statut);
    SetMsg(cErrorMsg,Buffer);
    return;
  }
  if (gPermissionMask!=m_gChannelMask)
  {
    SetMsg(cErrorMsg,"tcCanVector::tcCanVector> Can card configuration unauthorized");
    return;
  }
  // On configure la vitesse
  Statut = ncdSetChannelBitrate(m_gPortHandle,m_gChannelMask,m_ParamCan.BaudRate);
  if (Statut!=VSUCCESS)
  {
    char Buffer[256];
    sprintf(Buffer,"tcCanVector::tcCanVector> Baudrate configuration failed (%04X)",Statut);
    SetMsg(cErrorMsg,Buffer);
    return;
  }
  // Change Ack
  Statut = ncdSetChannelOutput(m_gPortHandle,m_gChannelMask,(m_ParamCan.Spy==1)?OUTPUT_MODE_SILENT:OUTPUT_MODE_NORMAL);
  if (Statut!=VSUCCESS)
  {
    char Buffer[256];
    sprintf(Buffer,"tcCanVector::tcCanVector> spy configuration failed (%04X)",Statut);
    SetMsg(cErrorMsg,Buffer);
    return;
  }
  // Inhiber les notifications TX et TXRQ
  Statut = ncdSetChannelMode(m_gPortHandle,m_gChannelMask,0,0);
  if (Statut!=VSUCCESS)
  {
    char Buffer[256];
    sprintf(Buffer,"tcCanVector::tcCanVector> Tx and TXRQ disabling failed (%04X)",Statut);
    SetMsg(cErrorMsg,Buffer);
    return;
  }
  // On configure les masques d'acceptances
  acc.mask = m_ParamCan.MaskStd; // relevant=1
  acc.code = m_ParamCan.CodeStd;
  Statut = ncdSetChannelAcceptance(m_gPortHandle,m_gChannelMask,&acc);
  if (Statut!=VSUCCESS)
  {
    char Buffer[256];
    sprintf(Buffer,"tcCanVector::tcCanVector> STD Mask id configuration failed (%04X)",Statut);
    SetMsg(cErrorMsg,Buffer);
    return;
  }
  acc.mask = 0x80000000 | m_ParamCan.MaskXtd; // relevant=1
  acc.code = 0x80000000 | m_ParamCan.CodeXtd;
  Statut = ncdSetChannelAcceptance(m_gPortHandle,m_gChannelMask,&acc);
  if (Statut!=VSUCCESS)
  {
    char Buffer[256];
    sprintf(Buffer,"tcCanVector::tcCanVector> XTD Mask id configuration failed (%04X)",Statut);
    SetMsg(cErrorMsg,Buffer);
    return;
  }
  // On vide la queue de réception
  Statut = ncdFlushReceiveQueue(m_gPortHandle);
  if (Statut!=VSUCCESS)
  {
    char Buffer[256];
    sprintf(Buffer,"tcCanVector::tcCanVector> Reception queue erasing failed (%04X)",Statut);
    SetMsg(cErrorMsg,Buffer);
    return;
  }
  // On vide la queue de transmission
  Statut = ncdFlushTransmitQueue(m_gPortHandle,m_gChannelMask);
  if (Statut!=VSUCCESS)
  {
    char Buffer[256];
    sprintf(Buffer,"tcCanVector::tcCanVector> Emission queue erasing failed (%04X)",Statut);
    SetMsg(cErrorMsg,Buffer);
    return;
  }
  // On crée l'évènement pour recevoir
  m_gEventHandle = CreateEvent(NULL, FALSE, FALSE, NULL);
  Statut = ncdSetNotification(m_gPortHandle,(unsigned long*)&m_gEventHandle, 1);
  if (Statut!=VSUCCESS)
  {
    char Buffer[256];
    sprintf(Buffer,"tcCanVector::tcCanVector> Reception event configuration failed (%04X)",Statut);
    SetMsg(cErrorMsg,Buffer);
    return;
  }
  // On active le canal
  Statut = ncdActivateChannel(m_gPortHandle,m_gChannelMask);
  if (Statut!=VSUCCESS)
  {
    char Buffer[256];
    sprintf(Buffer,"tcCanVector::tcCanVector> Channel activation failed (%04X)",Statut);
    SetMsg(cErrorMsg,Buffer);
    return;
  }
  m_StatutReq = VSUCCESS;

  m_CanCardMutex.Unlock();
}