Advertisement
Hubert_M

Untitled

Mar 9th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.51 KB | None | 0 0
  1. // CAN Send Example
  2. //
  3.  
  4. #include <mcp_can.h>
  5. #include <SPI.h>
  6.  
  7. #define CAN0_INT 2
  8. MCP_CAN CAN0(10); // Set CS to pin 10
  9. long unsigned int rxId;
  10. unsigned char len = 0;
  11. unsigned char rxBuf[8];
  12. char msgString[128]; // Array to store serial string
  13.  
  14.  
  15.  
  16. /*Dodatkowe*/
  17. byte Kod[8];
  18.  
  19. int a = 0;
  20. int b = 0;
  21. int ID = 0;
  22. int Length = 0;
  23. int temp;
  24.  
  25. char Input[29];
  26. char BufferID[5];
  27. char BufferLoop[4];
  28. char BufferLength[2];
  29.  
  30. String IDString = "";
  31. String commandString = "";
  32. String LengthString = "";
  33.  
  34.  
  35. /*Dodatkowe*/
  36.  
  37. void setup()
  38. {
  39. pinMode(LED_BUILTIN, OUTPUT);
  40. Serial.begin(115200);
  41. pinMode(CAN0_INT, INPUT);
  42. // Initialize MCP2515 running at 16MHz with a baudrate of 500kb/s and the masks and filters disabled.
  43. if(CAN0.begin(MCP_ANY, CAN_100KBPS, MCP_8MHZ) == CAN_OK) Serial.println("MCP2515 Initialized Successfully!");
  44. else Serial.println("Error Initializing MCP2515...");
  45.  
  46. CAN0.setMode(MCP_NORMAL); // Change to normal mode to allow messages to be transmitted
  47. /*
  48. Input[0] = '0';
  49. Input[1] = '1';
  50. Input[2] = '7';
  51. Input[3] = '0';
  52.  
  53. Input[4] = '8';
  54.  
  55. Input[5] = '0';
  56. Input[6] = '9';
  57. Input[7] = '5';
  58.  
  59. Input[8] = '0';
  60. Input[9] = '8';
  61. Input[10] = '9';
  62.  
  63. Input[11] = '0';
  64. Input[12] = '4';
  65. Input[13] = '0';
  66.  
  67. Input[14] = '2';
  68. Input[15] = '5';
  69. Input[16] = '4';
  70. //
  71. Input[17] = '0';
  72. Input[18] = '5';
  73. Input[19] = '2';
  74.  
  75. Input[20] = '2';
  76. Input[21] = '5';
  77. Input[22] = '5';
  78.  
  79. Input[23] = '1';
  80. Input[24] = '2';
  81. Input[25] = '8';
  82.  
  83. Input[26] = '1';
  84. Input[27] = '5';
  85. Input[28] = '3';
  86. */
  87. }
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95. byte data[8] = {0x45, 0x40, 0x21, 0x8F, 0xFE};
  96. byte data2[8] = {0xFF, 0x12,0xF7};
  97. byte data3[8] = {0xFD,0xFF,0xE0,0xF3,0x00,0x30,0xFC,0xBA};
  98. byte data5[8] = {0xA2,0x71,0x1E,0xFF,0xff,0xff,0xEA};
  99. byte data6[8] = {0x5F,0x59,0x28,0xFE,0x34,0xFF,0x80,0x99};
  100. byte data69[8] = {0x6C,0x06,0x28,0xFE,0x00,0x00,0x94,0x00};
  101. int counter=0x0;
  102. void loop()
  103. {
  104.  
  105. if (Serial.available() > 0) {
  106. digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
  107. delay(1500); // wait for a second
  108. digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
  109. for(int xdi =0; xdi < 29; xdi++) {
  110. Input[xdi] = Serial.read();
  111. delay(30);
  112. }
  113. }
  114. if(sizeof(Input)>27){
  115. BufferID[0] = Input[0];
  116. BufferID[1] = Input[1];
  117. BufferID[2] = Input[2];
  118. BufferID[3] = Input[3];
  119. BufferID[4] = 0;
  120.  
  121. IDString = BufferID;
  122. ID = IDString.toInt();
  123.  
  124.  
  125. BufferLength[0] = Input[4];
  126. BufferLength[1] = 0;
  127. LengthString = BufferLength;
  128. Length = LengthString.toInt();
  129.  
  130. for(int i = 5 ;i<29;i+=3){
  131.  
  132. BufferLoop[0] = Input[i];
  133. BufferLoop[1] = Input[i+1];
  134. BufferLoop[2] = Input[i+2];
  135. BufferLoop[3] = 0;
  136.  
  137. commandString = BufferLoop; //Byte[] to String
  138. temp = commandString.toInt(); //String to Int
  139. //Serial.print("temp (Int) : "); Serial.println(temp);
  140. Kod[b] = (byte)temp; //Int to Byte
  141. //Serial.print("temp (Byte) : "); Serial.println(Kod[b]);
  142. b++;
  143. }
  144. if (counter>0x0f) counter=0x0;
  145. //byte sndStat2 = CAN0.sendMsgBuf(0xAA, 8, Kod);
  146. }
  147.  
  148.  
  149. if (counter>0x0f) counter=0x0;
  150. byte data4[8] = {0xF0+counter,0xFF,0xE0,0xF3,0x00,0x30,0xFC,0xBA};
  151. // send data: ID = 0x100, Standard CAN Frame, Data length = 8 bytes, 'data' = array of data bytes to send
  152. /*
  153. Serial.print("ID: ");
  154. Serial.println(ID);
  155. //Serial.print(BufferID[0]); Serial.print(BufferID[1]);Serial.print(BufferID[2]);Serial.print(BufferID[10]);
  156.  
  157. Serial.print("Length: ");
  158. Serial.println(Length);
  159.  
  160. for(int i = 0; i < 8; i++)
  161. {
  162. Serial.print("Kod: ");
  163. Serial.println(Kod[i]);
  164. }
  165.  
  166.  
  167. //Serial.println("Dlugosc: " + Length);
  168. //Serial.println("Koniec");
  169. */
  170.  
  171. byte sndStat = CAN0.sendMsgBuf(0x130, 5, data);
  172. //byte sndStat2 = CAN0.sendMsgBuf(ID, Length, Kod);
  173.  
  174. //byte sndStat2 = CAN0.sendMsgBuf(0x21A, 0, 3, data2);
  175. //byte sndStat3 = CAN0.sendMsgBuf(0x34F, 0, 2, data3);
  176. //byte sndStat4 = CAN0.sendMsgBuf(0x0C0, 0, 8, data4);
  177. //byte sndStat5 = CAN0.sendMsgBuf(0x362, 0, 7, data5);
  178. //byte sndStat6 = CAN0.sendMsgBuf(0xAA, 0, 8, data6);
  179. counter++;
  180. //Serial.println((String)data4[0]);
  181.  
  182.  
  183. if(sndStat == CAN_OK){
  184. Serial.println("Message Sent Successfully!");
  185. } else {
  186. Serial.println("Error Sending Message...");
  187. }
  188.  
  189. if(!digitalRead(CAN0_INT)) // If CAN0_INT pin is low, read receive buffer
  190. {
  191. CAN0.readMsgBuf(&rxId, &len, rxBuf); // Read data: len = data length, buf = data byte(s)
  192.  
  193. if((rxId & 0x80000000) == 0x80000000) // Determine if ID is standard (11 bits) or extended (29 bits)
  194. sprintf(msgString, "Extended ID: 0x%.8lX DLC: %1d Data:", (rxId & 0x1FFFFFFF), len);
  195. else
  196. sprintf(msgString, "Standard ID: 0x%.3lX DLC: %1d Data:", rxId, len);
  197.  
  198. //Serial.print(msgString);
  199.  
  200. if((rxId & 0x40000000) == 0x40000000){ // Determine if message is a remote request frame.
  201. sprintf(msgString, " REMOTE REQUEST FRAME");
  202. Serial.print(msgString);
  203. } else {
  204. for(byte i = 0; i<len; i++){
  205. sprintf(msgString, " 0x%.2X", rxBuf[i]);
  206. // Serial.print(msgString);
  207. }
  208. }
  209.  
  210. //Serial.println();
  211. }
  212.  
  213. delay(100); // send data per 100ms
  214. }
  215.  
  216. /*********************************************************************************************************
  217. END FILE
  218. *********************************************************************************************************/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement