Example #1
0
void incBusLoad (int channel, int flags, int load)
{
  canHandle handle;
  unsigned char msg[8] = "Kvaser!";
  int i, id = 100, dlc = sizeof(msg);
  canStatus stat = canERR_PARAM;
  handle  = canOpenChannel(channel, flags);
  if (handle < 0) {
    check("canOpenChannel", handle);
    return;
  }
  stat = canBusOff(handle);
  check("canBusOff", stat);
  stat = canSetBusParams(handle, bitrate, 0, 0, 0, 0, 0);
  check("canSetBusParams", stat);
  canBusOn(handle);
  for (i = 0; i < load; i++) {
    stat = canWrite(handle, id, &msg, dlc, 0);
    check("canWrite", stat);
  }
  stat = canWriteWait(handle, id, &msg, dlc, 0, -1);
  check("canWriteWait", stat);
  canBusOff(handle);
  canClose(handle);
}
Example #2
0
/**
 * Channels and their descriptors are numbered starting from zero.
 * So I need to increment by 1 the handle returned by CANLIB because
 * CANfestival CAN_HANDLEs with value zero are considered NOT VALID. 
 * 
 * The baud rate could be given directly as bit/s
 * or using one of the BAUD_* constants defined
 * in canlib.h
 */ 
CAN_HANDLE canOpen_driver(s_BOARD *board)
{
int fd0 = -1;
int channel, baud;
canStatus retval = canOK;

    fd0--;

    sscanf(board->busname, "%d", &channel);  

    baud = TranslateBaudRate(board->baudrate);

    if (baud == 0)
    {
        sscanf(board->baudrate, "%d", &baud);
    }

    fd0 = canOpenChannel(channel, canWANT_EXCLUSIVE|canWANT_EXTENDED);
    if (fd0 < 0)
    {
  	    fprintf(stderr, "canOpen_driver (Kvaser) : error opening channel %d\n", channel);
        return (CAN_HANDLE)(fd0+1);
    }
    canBusOff(fd0);

    /* values for tseg1, tseg2, sjw, noSamp and  syncmode
     * come from canlib example "simplewrite.c". The doc
     * says that default values will be taken if baud is one of
     * the BAUD_* values
     */ 
    retval = canSetBusParams(fd0, baud, 4, 3, 1, 1, 0);
    if (retval != canOK)
    {
  	    fprintf(stderr, "canOpen_driver (Kvaser) :  canSetBusParams() error, returned value = %d, baud=%d, \n", retval, baud);
        canClose((int)fd0);
        return (CAN_HANDLE)retval;
    }
    
    canSetBusOutputControl(fd0, canDRIVER_NORMAL);
    if (retval != canOK)
    {
  	    fprintf(stderr, "canOpen_driver (Kvaser) :  canSetBusOutputControl() error, returned value = %d\n", retval);
        canClose((int)fd0);
        return (CAN_HANDLE)retval;
    }



    
    retval = canBusOn(fd0);
    if (retval != canOK)
    {
  	    fprintf(stderr, "canOpen_driver (Kvaser) :  canBusOn() error, returned value = %d\n", retval);
        canClose((int)fd0);
        return (CAN_HANDLE)retval;
    }
    
    return (CAN_HANDLE)(fd0+1);

}
Example #3
0
void MainWindow::on_pushButtonBusOn_clicked()
{
    bool fail = false;
    canStatus stat;
    qDebug() debugprefix << "on_pushButtonBusOn_clicked()";

    if(busOn){
        canBusOff(canHandle);
        ui->pushButtonBusOn->setText("Go On Bus");
        busOn = false;
    }else{
        // Timings
        int br = (ui->comboBoxBaud->currentIndex()+1)*-1;
        stat = canSetBusParams(canHandle, br  , 0, 0, 0, 0, 0);
        if (stat < 0){
            char buf[128];
            canGetErrorText(stat,buf,128);
            qDebug() debugprefix << "canSetBusParams("<< br << ") " << buf;
            fail = true;
        }

        // Silent
        bool silent = ui->checkBox_Silent->isChecked();
        if(silent){
            stat = canSetBusOutputControl(canHandle, canDRIVER_SILENT);
        }else{
            stat = canSetBusOutputControl(canHandle, canDRIVER_NORMAL);
        }
        if (stat < 0){
            char buf[128];
            canGetErrorText(stat,buf,128);
            qDebug() debugprefix << "canSetBusOutputControl("<< silent << ") " << buf;
            fail = true;
        }

        if(fail == false){
            stat = canBusOn(canHandle);
            if (stat < 0){
                char buf[128];
                canGetErrorText(stat,buf,128);
                qDebug() debugprefix << "canBusOn("<< br << ") " << buf;
                fail = true;
                QMessageBox msgBox;
                msgBox.setText("Couldn't enter Bus-On state");
                msgBox.exec();
            }else{
                ui->pushButtonBusOn->setText("Go Off Bus");
                busOn = true;
            }
        }else{
            QMessageBox msgBox;
            msgBox.setText("Couldn't enter Bus-On state due to initialization errors");
            msgBox.exec();
        }
    }
}
Example #4
0
int main (int argc, char *argv[])
{
  canHandle h;
  int channel;
  int bitrate = BAUD_1M;

  errno = 0;
  if (argc != 3 || (channel = 0, errno) != 0) {
    printf("usage %s address byte1\n", argv[0]);
    exit(1);
  } else {
    printf("Sending a message on channel %d\n", channel);
  }


  /* Allow signals to interrupt syscalls(e.g in canReadBlock) */
  siginterrupt(SIGINT, 1);
  
  /* Open channel, set parameters and go on bus */

  //h = canOpenChannel(channel, canOPEN_EXCLUSIVE | canOPEN_REQUIRE_EXTENDED);
  h = canOpenChannel(channel, canOPEN_EXCLUSIVE);
  if (h < 0) {
    printf("canOpenChannel %d failed\n", channel);
    return -1;
  }

  canBusOff(h);
  check("canSetBusParams", canSetBusParams(h, bitrate, 4, 3, 2, 1, 0));
  // Work-around for Leaf bug
  check("canSetBusOutputControl", canSetBusOutputControl(h, canDRIVER_NORMAL));
  check("canBusOn", canBusOn(h));
  //OY 8/20/2014
  //unsigned char channel = atoi(argv[2]));
  msg[0] = 0;
  msg[2] = 1;
  msg[3] = 41;
  if (atoi(argv[2]) == 0)
  	msg[1] = 1;
  if (atoi(argv[2]) == 100)
  	msg[1] = 2;
		
  check("canWrite", canWrite(h, atoi(argv[1]), msg, 4, 0));      
  //printf("%d %d %d %d %d\n",msg[0], msg[1], msg[2], msg[3], channel); 
  check("canWriteSync", canWriteSync(h, 1000));

  
  //check("canBusOff", canBusOff(h));
  check("canClose", canClose(h));

  return 0;
}
Example #5
0
UNS8 canChangeBaudRate_driver( CAN_HANDLE fd0, char* baud)
{
int baudrate;
canStatus retval = canOK;


    baudrate = TranslateBaudRate(baud);
    if (baudrate == 0)
    {
        sscanf(baud, "%d", &baudrate);
    }


    fprintf(stderr, "%x-> changing to baud rate %s[%d]\n", (int)fd0, baud, baudrate); 
    
    canBusOff((int)fd0);

    /* values for tseg1, tseg2, sjw, noSamp and  syncmode
     * come from canlib example "simplewrite.c". The doc
     * says that default values will be taken if baud is one of
     * the BAUD_* values
     */ 
    retval = canSetBusParams((int)fd0, baudrate, 4, 3, 1, 1, 0);
    if (retval != canOK)
    {
  	    fprintf(stderr, "canChangeBaudRate_driver (Kvaser) :  canSetBusParams() error, returned value = %d, baud=%d, \n", retval, baud);
        canClose((int)fd0);
        return (UNS8)retval;
    }
    
    canSetBusOutputControl((int)fd0, canDRIVER_NORMAL);
    if (retval != canOK)
    {
  	    fprintf(stderr, "canChangeBaudRate_driver (Kvaser) :  canSetBusOutputControl() error, returned value = %d\n", retval);
        canClose((int)fd0);
        return (UNS8)retval;
    }
    
    retval = canBusOn((int)fd0);
    if (retval != canOK)
    {
  	    fprintf(stderr, "canChangeBaudRate_driver (Kvaser) :  canBusOn() error, returned value = %d\n", retval);
        canClose((int)fd0);
        return (UNS8)retval;
    }

    return 0;
}
Example #6
0
/**
 *
 * CAN_HANDLES must have value >=1 while CANLIB wants handles >= 0
 * so fd0 needs to be decremented before use.
 */ 
int canClose_driver(CAN_HANDLE fd0)
{
canStatus retval = canOK;

    fd0--;
    
    retval = canBusOff((int)fd0);
    if (retval != canOK)
    {
  	    fprintf(stderr, "canClose_driver (Kvaser) :  canBusOff() error, returned value = %d\n", retval);
        canClose((int)fd0);
        return retval;
    }
    
    retval = canClose((int)fd0);
    if (retval != canOK)
    {
  	    fprintf(stderr, "canClose_driver (Kvaser) :  canClose() error, returned value = %d\n", retval);
        return retval;
    }
    
    return retval;
}
Example #7
0
int CDeviceCAN::CloseCAN()
{
	(void)canBusOff(canhandle);
	canClose(canhandle);
	return STATUS_OK;
}
Example #8
0
int main(int argc, char *argv[])
{
  canStatus stat;
  canHandle hnd;
  int channelRx;
  int channelTx;

  if (argc != 3) {
    printUsageAndExit(argv[0]);
  }

  {
    char *endPtr = NULL;
    errno = 0;
    channelRx = strtol(argv[1], &endPtr, 10);
    if ( (errno != 0) || ((channelRx == 0) && (endPtr == argv[1])) ) {
      printUsageAndExit(argv[0]);
    }
    channelTx = strtol(argv[2], &endPtr, 10);
    if ( (errno != 0) || ((channelTx == 0) && (endPtr == argv[2])) ) {
      printUsageAndExit(argv[0]);
    }
  }

  canInitializeLibrary();

  hnd  = canOpenChannel(channelRx, canOPEN_REQUIRE_EXTENDED);
  if (hnd < 0) {
    printf("canOpenChannel %d", channelRx);
    check("", hnd);
    return -1;
  }

  stat = canBusOff(hnd);
  check("canBusOff", stat);
  if (stat != canOK) {
    goto ErrorExit;
  }
  stat = canSetNotify(hnd, callback, canNOTIFY_ERROR | canNOTIFY_STATUS, NULL);
  check("canSetNotify", stat);
  if (stat != canOK) {
    goto ErrorExit;
  }
  stat = canSetBusParams(hnd, bitrate, 0, 0, 0, 0, 0);
  check("canSetBusParams", stat);
  if (stat != canOK) {
    goto ErrorExit;
  }
  stat = canBusOn(hnd);
  check("canBusOn", stat);
  if (stat != canOK) {
    goto ErrorExit;
  }

  incBusLoad(channelTx, canOPEN_REQUIRE_EXTENDED, 0);
  testBusLoad(hnd);
  incBusLoad(channelTx, canOPEN_REQUIRE_EXTENDED, 100);
  testBusLoad(hnd);
  testBusLoad(hnd);
  incBusLoad(channelTx, canOPEN_REQUIRE_EXTENDED, 300);
  testBusLoad(hnd);
  testBusLoad(hnd);

ErrorExit:

  stat = canBusOff(hnd);
  check("canBusOff", stat);
  stat = canClose(hnd);
  check("canClose", stat);
  stat = canUnloadLibrary();
  check("canUnloadLibrary", stat);

  return 0;
}
Example #9
0
void PerformTest(int argc, char **argv)
{
    int handle1, handle2;
    int br, i;

    TIMECAPS tmcps;
    int needTimeEndPeriod = 0;
    
    switch (Bitrate) {
        case 1000000:
            br = canBITRATE_1M;
            break;
        case 500000:
            br = canBITRATE_500K;
            break;
        case 250000:
            br = canBITRATE_250K;
            break;
        case 125000:
            br = canBITRATE_125K;
            break;
        case 100000:
            br = canBITRATE_100K;
            break;
        case 62500:
            br = canBITRATE_62K;
            break;
        case 50000:
            br = canBITRATE_50K;
            break;
        case 20000:
            br = Bitrate;
            break;
        case 10000:
            br = Bitrate;
            break;
        case 5000:
            br = Bitrate;
            break;
        default:
            // qqq
            printf("Unsupported bitrate, choose 1000/500/250/125/100/62.5/50/20/10/5 k.\n");
            exit(1);
    }
    BitrateConverted = br;
    
    if (timeGetDevCaps(&tmcps, sizeof(TIMECAPS)) == TIMERR_NOERROR) {
      needTimeEndPeriod = 1;
      // Set resolution as low as possible
      timeBeginPeriod(tmcps.wPeriodMin);
      if (Verbose && tmcps.wPeriodMin >= 8)
        printf("PC-timer resolution set to %d\n", tmcps.wPeriodMin);
    } else {
      if (Verbose)
        printf("Couldn't set PC-timer resolution\n");
    }
    
    handle1 = InitCtrl(First, br);
    handle2 = InitCtrl(Second, br);

    for (i=0; i<LoopCount; i++) {

        if (Verbose) {
            int j;
            printf("\n*** Loop %d *** (", i);
            for (j=0; j<argc; j++) printf("%s ", argv[j]);
            printf(")\n");
            fflush(stdout);
        }
        
        canBusOff(handle1);
        canBusOff(handle2);            
        handle1 = ReInitialize(handle1, First, br);
        handle2 = ReInitialize(handle2, Second, br);
        timeStampTester(handle1, handle2);
        
        if (_kbhit() && _getch() == 27) {  // ESC hit?
          break;
        }
    }
    
    canClose(handle1);
    canClose(handle2);

    if (needTimeEndPeriod) {
      timeEndPeriod(tmcps.wPeriodMin); // reset resolution
    }
}