Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5. #include <signal.h>
  6. #include <asm/types.h>
  7.  
  8. #ifndef NO_RT
  9. #include <sys/mman.h>
  10. #endif
  11. #define PCAN_DEVICE PCAN_USBBUS1
  12.  
  13. #include <PCANBasic.h>
  14.  
  15. static void signal_handler(int s)
  16. {
  17. printf("Interrupted by SIG%u!\n", s);
  18. }
  19.  
  20.  
  21. int main(int argc, char* argv[])
  22. {
  23. TPCANMsg Message;
  24. TPCANStatus Status;
  25. unsigned long ulIndex = 0;
  26. unsigned int pcan_device = PCAN_DEVICE;
  27.  
  28.  
  29.  
  30. #ifndef NO_RT
  31. mlockall(MCL_CURRENT | MCL_FUTURE);
  32. #endif
  33.  
  34. signal(SIGINT, signal_handler);
  35.  
  36. Status = CAN_Initialize(pcan_device, PCAN_BAUD_500K, 0, 0, 0);
  37. printf("CAN_Initialize(%xh): Status=0x%x\n", pcan_device, (int )Status);
  38. if (Status)
  39. goto lbl_exit;
  40.  
  41. Message.ID = 0x02;
  42. Message.LEN = 8;
  43. Message.MSGTYPE = PCAN_MESSAGE_STANDARD;
  44. //memset(Message.DATA, '\0', sizeof(Message.DATA));
  45. for(int i=0; i<8; i++)
  46. {
  47. Message.DATA[i]=i*i;
  48. }
  49.  
  50. while(1) {
  51. while ((Status = CAN_Write(pcan_device, &Message)) == PCAN_ERROR_OK) {
  52. for (int i = 0; i < 8; i++)
  53. if (++Message.DATA[i])
  54. break;
  55.  
  56. ulIndex++;
  57. if ((ulIndex % 1000) == 0)
  58. printf(" - T Message %i\n", (int)ulIndex);
  59. }
  60.  
  61. if (Status != PCAN_ERROR_QXMTFULL) {
  62. printf("CAN_Write(%xh): Error 0x%x\n", pcan_device,
  63. (int)Status);
  64. break;
  65. }
  66.  
  67. // Tx queue is full: must wait a bit instad of forever
  68. // looping. Handle ^C here too.
  69. if (usleep(100))
  70. break;
  71. }
  72.  
  73. CAN_Uninitialize(pcan_device);
  74.  
  75. lbl_exit:
  76.  
  77. return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement