Hubert_M

Untitled

Jun 12th, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <mcp_can.h>
  3.  
  4. int CSPin = 10; // numer pina CS
  5. MCP_CAN CAN(CSPin);
  6.  
  7. int Value = 0;
  8.  
  9. /* --- Odebrane --- */
  10. long unsigned int Id;
  11. unsigned char len = 0;
  12. unsigned char buf[8];
  13. /* --- Odebrane --- */
  14.  
  15. /* --- Wysylka --- */
  16. unsigned char x = 1;
  17. unsigned char stmp[8] = {5, 0, 0, 0, 0, 0, 0, 0}; // dane przygotowane do wysłania
  18. /* --- Wysylka --- */
  19.  
  20. /* --- Pierwsze Odebranie --- */
  21. unsigned char sentMessage[8] = {5, 0, 0, 0, 0, 0, 0, 0}; // Wiadomosc do wyslania
  22. unsigned char receivedMessage[8];
  23. /* --- Pierwsze Odebranie --- */
  24.  
  25. /* --- Logowanie --- */
  26. unsigned char loggedMessage[8] = {9, 0, 0, 0, 0, 0, 0, 0};
  27. unsigned char receivedLoginMessage[8];
  28. /* --- Logowanie --- */
  29.  
  30.  
  31. void setup()
  32. {
  33. /* --- Konfiguracja --- */
  34. Serial.begin(9600);
  35. if (CAN.begin(MCP_ANY, CAN_100KBPS, MCP_8MHZ) == CAN_OK)
  36. Serial.println("Inicjalizacja mcp2515 udana! Arduino 2");
  37. else Serial.println("Błąd w inicjalizacji mcp2515 :(");
  38. CAN.setMode(MCP_NORMAL);
  39. }
  40.  
  41. /* --- Konfiguracja --- */
  42. void loop()
  43. {
  44. /* --- Pierwsze Odebranie --- */
  45. if (Value == 0) {
  46. if (CAN_MSGAVAIL == CAN.checkReceive()) // sprawdza czy jest wiadomość
  47. {
  48. CAN.readMsgBuf(&Id, &len, receivedMessage);
  49. if (receivedMessage[0] == 4) {
  50. CAN.sendMsgBuf(0x10, 0, 8, sentMessage);
  51. Value = 1;
  52. Serial.println("Otrzymano komunikat powitalny");
  53. }
  54. }
  55. }
  56. /* --- Pierwsze Odebranie --- */
  57.  
  58.  
  59. /* --- Logowanie --- */
  60. if (Value == 1) {
  61. if (CAN_MSGAVAIL == CAN.checkReceive()) // sprawdza czy jest wiadomość
  62. {
  63. CAN.readMsgBuf(&Id, &len, receivedLoginMessage);
  64. Serial.println(receivedLoginMessage[0]);
  65. if (receivedLoginMessage[0] == 1) {
  66. CAN.sendMsgBuf(0x10, 0, 8, loggedMessage);
  67.  
  68. Serial.println("Zalogowano pomyslnie");
  69. Value = 2;
  70. }
  71. else
  72. Serial.println("Blad logowania");
  73.  
  74. }
  75. }
  76. /* --- Logowanie --- */
  77.  
  78. }
Advertisement
Add Comment
Please, Sign In to add comment