Exemplo n.º 1
0
void pcan_transmit::init(int argc, char **argv)
{
    /*!
				 *\brief The init() function is based on the middle part of the main() function of the transmitest program.
				 *
				 * Compared to the transmitest program a lot of cases have been removed.
				 * Therefore, only the PCAN-USB adapter is, and standard messages are, supported.
				 *
				 * */




	int   nExtended = CAN_INIT_TYPE_ST;
	int nType;
	__u32 dwPort;
	__u16 wIrq;
	char *ptr;
	__u16 wBTR0BTR1 = 0x011C;
	// parameter wBTR0BTR1
	// bitrate codes of BTR0/BTR1 registers
	//#define CAN_BAUD_1M     0x0014  //   1 MBit/s
	//#define CAN_BAUD_500K   0x001C  // 500 kBit/s
    //#define CAN_BAUD_250K   0x011C  // 250 kBit/s
	//#define CAN_BAUD_125K   0x031C  // 125 kBit/s
	//#define CAN_BAUD_100K   0x432F  // 100 kBit/s
	//#define CAN_BAUD_50K    0x472F  //  50 kBit/s
	//#define CAN_BAUD_20K    0x532F  //  20 kBit/s
	//#define CAN_BAUD_10K    0x672F  //  10 kBit/s
	//#define CAN_BAUD_5K     0x7F7F  //   5 kBit/s
	const char  *szDevNode = DEFAULT_NODE;
	bool bDevNodeGiven = false;
	bool bTypeGiven = false;
	char txt[VERSIONSTRING_LEN];




   /* QApplication a(argc, argv);
    QWidget *mainWindow=new QWidget;
    mainWindow->setWindowTitle("Change the Data Value");
    QSlider *slider= new QSlider(Qt::Horizontal);
    QSpinBox *spinner= new QSpinBox;

    slider->setRange(0,100);
    spinner->setRange(0,100);

    connect(ui->slider, SIGNAL(valueChanged(double)), SLOT(onSliderValueChanged(double)));


double slidValue= ui->Slider->value();



    //string msg[]= "ID, LEN, ss";*/


    
    
    
    // decode command line arguments
		for (int i = 1; i < argc; i++)
		{
		    char c;

		    ptr = argv[i];

		    while (*ptr == '-')
		      ptr++;

		    c = *ptr;
		    ptr++;

		    if (*ptr == '=')
		      ptr++;

		    switch(tolower(c))
		    {
		      case '?':
		      case 'h':
		    	hlpMsg();
		    	do_exit(errno, h);
		        break;
		      case 'f':
		        szDevNode = ptr;
		        bDevNodeGiven = true;
		        break;
		      case 'b':
		        wBTR0BTR1 = (__u16)strtoul(ptr, NULL, 16);
		        break;
		      default:
		         errno = EINVAL;
		         do_exit(errno, h);;
		        break;
		    }
		}




		  /* open CAN port */
		   if ((bDevNodeGiven) || (!bDevNodeGiven && !bTypeGiven))
		   {
		     h = LINUX_CAN_Open(szDevNode, O_RDWR);
		     if (!h)
		     {
		       printf("pcan_transmit: can't open %s\n", szDevNode);
		       do_exit(errno, h);;
		     }
		   }
		   else {
		     // please use what is appropriate
		     // HW_DONGLE_SJA
		     // HW_DONGLE_SJA_EPP
		     // HW_ISA_SJA
		     // HW_PCI
		     h = CAN_Open(nType, dwPort, wIrq);
		     if (!h)
		     {
		       printf("pcan_transmit: can't open %s device.\n", getNameOfInterface(nType));
		       do_exit(errno, h);;
		     }
		   }
		   /* clear status */
		     CAN_Status(h);
		     // get version info
		     errno = CAN_VersionInfo(h, txt);
		     if (!errno)
		       printf("pcan_transmit: driver version = %s\n", txt);
		     else {
		       perror("pcan_transmit: CAN_VersionInfo()");
		       do_exit(errno, h);;
		     }
		     // init to a user defined bit rate
		     if (wBTR0BTR1)
		     {
		       errno = CAN_Init(h, wBTR0BTR1, nExtended);
		       if (errno)
		       {
		         perror("pcan_transmit: CAN_Init()");
		         do_exit(errno, h);;
		       }
		     }

}
int main(int argc, char *argv[])
{
  char *ptr;
  int i;
  int nType = HW_PCI;
  __u32 dwPort = 0;
  __u16 wIrq = 0;
  __u16 wBTR0BTR1 = 0;
  __u32 dwMaxTimeInterval = 0, dwMaxLoop = 0;
  char *filename = NULL;
  const char *szDevNode = DEFAULT_NODE;
  bool bDevNodeGiven = false;
  bool bTypeGiven = false;
  parser MyParser;
  char txt[VERSIONSTRING_LEN];

  errno = 0;

  current_release = CURRENT_RELEASE;
  disclaimer("transmitest");

  init();

  // decode command line arguments
  for (i = 1; i < argc; i++) 
  {
    char c;

    ptr = argv[i];

    if (*ptr == '-') 
    {
      while (*ptr == '-')
        ptr++;

      c = *ptr;
      ptr++;

      if (*ptr == '=')
      ptr++;

      switch(tolower(c)) 
      {
        case 'f':
          szDevNode = ptr;
          bDevNodeGiven = true;
          break;
        case 't':
          nType = getTypeOfInterface(ptr);
          if (!nType) 
          {
            errno = EINVAL;
            printf("transmitest: unknown type of interface\n");
            goto error;
          }
          bTypeGiven = true;
          break;
        case 'p':
          dwPort = strtoul(ptr, NULL, 16);
          break;
        case 'i':
          wIrq   = (__u16)strtoul(ptr, NULL, 10);
          break;
        case 'e':
          nExtended = CAN_INIT_TYPE_EX;
          break;
        case '?': 
        case 'h':
          hlpMsg();
          goto error;
          break;
        case 'b':
          wBTR0BTR1 = (__u16)strtoul(ptr, NULL, 16);
          break;
        case 'r':
          dwMaxTimeInterval = strtoul(ptr, NULL, 10);
          break;
        case 'n':
          dwMaxLoop = strtoul(ptr, NULL, 10);
          break;
        default:
          errno = EINVAL;
          printf("transmitest: unknown command line argument\n");
          goto error;
          break;
      }
    }
    else
      filename = ptr;
  }

  // test for filename
  if (filename == NULL) 
  {
    errno = EINVAL;
    perror("transmitest: no filename given");
    goto error;
  }

  // test device node and type
  if (bDevNodeGiven && bTypeGiven) 
  {
    errno = EINVAL;
    perror("transmitest: device node and type together is useless");
    goto error;
  }

  // give the filename to my parser
  MyParser.setFileName(filename);

  // tell some information to the user
  if (!bTypeGiven) 
  {
    printf("transmitest: device node=\"%s\"\n", szDevNode);
  }
  else {
    printf("transmitest: type=%s", getNameOfInterface(nType));
    if (nType == HW_USB) 
      printf(", Serial Number=default, Device Number=%d\n", dwPort); 
    else {
      if (dwPort) 
      {
        if (nType == HW_PCI)
          printf(", %d. PCI device", dwPort);
        else
          printf(", port=0x%08x", dwPort);
      }
      else
        printf(", port=default");
      
      if ((wIrq) && !(nType == HW_PCI))
        printf(" irq=0x%04x\n", wIrq);
      else
        printf(", irq=default\n");
    }
  }

  if (nExtended == CAN_INIT_TYPE_EX)
    printf("             Extended frames are sent");
  else
    printf("             Only standard frames are sent");
  
  if (wBTR0BTR1)
    printf(", init with BTR0BTR1=0x%04x\n", wBTR0BTR1);
  else
    printf(", init with 500 kbit/sec.\n");
    printf("             Data will be read from \"%s\".\n", filename);

  if (dwMaxTimeInterval)
    printf("             Messages are send in random time intervalls with a max. gap time of %d msec.\n", dwMaxTimeInterval);
  
  /* get the list of data from parser */
  List = MyParser.Messages();
  if (!List)
  {
    errno = MyParser.nGetLastError();
    perror("transmitest: error at file read");
    goto error;
  }
  
  /* open CAN port */
  if ((bDevNodeGiven) || (!bDevNodeGiven && !bTypeGiven)) 
  {
    h = LINUX_CAN_Open(szDevNode, O_RDWR);
    if (!h)
    {
      printf("transmitest: can't open %s\n", szDevNode);
      goto error;
    }
  }
  else {
    // please use what is appropriate  
    // HW_DONGLE_SJA 
    // HW_DONGLE_SJA_EPP 
    // HW_ISA_SJA 
    // HW_PCI 
    h = CAN_Open(nType, dwPort, wIrq);
    if (!h)
    {
      printf("transmitest: can't open %s device.\n", getNameOfInterface(nType));
      goto error;
    }
  }

  /* clear status */
  CAN_Status(h);
  
  // get version info
  errno = CAN_VersionInfo(h, txt);
  if (!errno)
    printf("transmitest: driver version = %s\n", txt);
  else {
    perror("transmitest: CAN_VersionInfo()");
    goto error;
  }
  
  // init to a user defined bit rate
  if (wBTR0BTR1) 
  {
    errno = CAN_Init(h, wBTR0BTR1, nExtended);
    if (errno) 
    {
      perror("transmitest: CAN_Init()");
      goto error;
    }
  }
  // enter in the write loop
  errno = write_loop(dwMaxTimeInterval, dwMaxLoop);
  if (!errno)
    return 0;

  error:
    do_exit(errno);
    return errno;
}
Exemplo n.º 3
0
// here all is done
int main(int argc, char *argv[])
{
	char *ptr;
	int i;
	int nType = HW_PCI;
	__u32 dwPort = 0;
	__u16 wIrq = 0;
	__u16 wBTR0BTR1 = 0;
	int nExtended = CAN_INIT_TYPE_ST;
	const char  *szDevNode = DEFAULT_NODE;
	bool bDevNodeGiven = false;
	bool bTypeGiven = false;
	bool bDisplayOn = true;
	char txt[VERSIONSTRING_LEN];

	errno = 0;

	current_release = CURRENT_RELEASE;
	disclaimer("receivetest");

	init();

	// decode command line arguments
	for (i = 1; i < argc; i++) {
		char c;

		ptr = argv[i];

		while (*ptr == '-')
			ptr++;

		c = *ptr;
		ptr++;

		if (*ptr == '=')
			ptr++;

		switch(tolower(c)) {
		case 'f':
			szDevNode = ptr;
			bDevNodeGiven = true;
			break;
		case 'd':
			if (strcmp(ptr, "no") == 0)
				bDisplayOn = false;
			break;
		case 't':
			nType = getTypeOfInterface(ptr);
			if (!nType) {
				errno = EINVAL;
				printf("receivetest: unknown type of interface!\n");
				goto error;
			}
			bTypeGiven = true;
			break;
		case 'p':
			dwPort = strtoul(ptr, NULL, 16);
			break;
		case 'i':
			wIrq = (__u16)strtoul(ptr, NULL, 10);
			break;
		case 'e':
			nExtended = CAN_INIT_TYPE_EX;
			break;
		case '?':
		case 'h':
			hlpMsg();
			goto error;
			break;
		case 'b':
			wBTR0BTR1 = (__u16)strtoul(ptr, NULL, 16);
			break;
		default:
			errno = EINVAL;
			perror("receivetest: unknown command line argument!\n");
			goto error;
			break;
		}
	}

	// simple command input check
	if (bDevNodeGiven && bTypeGiven) {
		errno = EINVAL;
		perror("receivetest: device node and type together is useless");
		goto error;
	}

	// give some information back
	if (!bTypeGiven) {
		printf("receivetest: device node=\"%s\"\n", szDevNode);
	} else {
		printf("receivetest: type=%s", getNameOfInterface(nType));
		switch (nType) {
		case HW_USB:
		case HW_USB_PRO:
		case HW_USB_FD:
		case HW_USB_PRO_FD:
			if (dwPort)
				printf(", %d. device\n", dwPort);
			else
				printf(", standard device\n");
			break;

		default:
			if (dwPort) {
				if (nType == HW_PCI)
					printf(", %d. PCI device", dwPort);
				else
					printf(", port=0x%08x", dwPort);
			} else
				printf(", port=default");

			if ((wIrq) && !(nType == HW_PCI))
				printf(" irq=0x%04x\n", wIrq);
			else
				printf(", irq=default\n");
			break;
		}
	}

	if (nExtended == CAN_INIT_TYPE_EX)
		printf("             Extended frames are accepted");
	else
		printf("             Only standard frames are accepted");
	if (wBTR0BTR1)
		printf(", init with BTR0BTR1=0x%04x\n", wBTR0BTR1);
	else
		printf(", init with 500 kbit/sec.\n");

	/* open CAN port */
	if ((bDevNodeGiven) || (!bDevNodeGiven && !bTypeGiven)) {
		h = LINUX_CAN_Open(szDevNode, O_RDWR);
		if (!h) {
			printf("receivetest: can't open %s\n", szDevNode);
			goto error;
		}
	} else {
		// please use what is appropriate  
		// HW_DONGLE_SJA 
		// HW_DONGLE_SJA_EPP 
		// HW_ISA_SJA 
		// HW_PCI 
		h = CAN_Open(nType, dwPort, wIrq);
		if (!h) {
			printf("receivetest: can't open %s device.\n",
				getNameOfInterface(nType));
			goto error;
		}
	}

	/* clear status */
	//  CAN_Status(h);

	// get version info
	errno = CAN_VersionInfo(h, txt);
	if (!errno)
		printf("receivetest: driver version = %s\n", txt);
	else {
		perror("receivetest: CAN_VersionInfo()");
		goto error;
	}

	// init to a user defined bit rate
	if (wBTR0BTR1) {
		errno = CAN_Init(h, wBTR0BTR1, nExtended);
		if (errno) {
			perror("receivetest: CAN_Init()");
			goto error;
		}
	}
	errno = read_loop(bDisplayOn);
	if (!errno)
		return 0;

error:
	do_exit(errno);
	return errno;
}
Exemplo n.º 4
0
// here all is done
int main(int argc, char *argv[])
{
  char *ptr;
  int i;
  const char *szDeviceNode = DEFAULT_NODE;
  
  errno = 0;

  current_release = CURRENT_RELEASE;
  disclaimer("bitratetest");

  // decode command line arguments
  for (i = 1; i < argc; i++)
  {
    char c;

    ptr = argv[i];

    while (*ptr == '-')
      ptr++;
      
    c = *ptr;
    ptr++;

    if (*ptr == '=')
      ptr++;
      
    switch(tolower(c))
    {
    	case 'f':
				szDeviceNode = ptr;
				break;
			case '?':
      case 'h':
        hlpMsg();
	      my_private_exit(0);
	      break;
	      
	    default:
	      errno = EINVAL;
	      perror("bitratetest: unknown command line argument");
	      my_private_exit(errno);
	      break;
	   }
  }
  
  // open the CAN port
  // please use what is appropriate
  // HW_DONGLE_SJA 
  // HW_DONGLE_SJA_EPP
  // HW_ISA_SJA 
  // HW_PCI
  h = LINUX_CAN_Open(szDeviceNode, O_RDWR);

  if (h)
  {
    char txt[VERSIONSTRING_LEN];

    // get version info
    errno = CAN_VersionInfo(h, txt);
    if (!errno)
      printf("bitratetest: driver version = %s\n", txt);
    else
    {
      perror("bitratetest: CAN_VersionInfo()");
      my_private_exit(errno);
    }

    printf("\n");
    
    // calculate BTR0BTR1 from bitrates
    for (i = 1000000; i > 2500; i /= 2)
    {
    	if (i == 62500)
				i = 100000;
			if (i == 6250)
				i = 10000;
				
    	printf("bitratetest: %d bits/sec \t->BTR0BTR1=0x%04x\n", i + 1,  LINUX_CAN_BTR0BTR1(h, i + 1));
    	printf("             %d bits/sec \t->BTR0BTR1=0x%04x\n", i    ,  LINUX_CAN_BTR0BTR1(h, i));
    	printf("             %d bits/sec \t->BTR0BTR1=0x%04x\n", i - 1,  LINUX_CAN_BTR0BTR1(h, i - 1));
    };
    printf("\n");
  }
  else
  {
    errno = nGetLastError();
    perror("bitratetest: CAN_Open()");
  }
    
  my_private_exit(errno);
  
  return errno;
}