Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SPI.h>
- #include <mcp_can.h>
- int CSPin = 10; // numer pina CS
- MCP_CAN CAN(CSPin);
- int Value = 0;
- /* --- Odebrane --- */
- long unsigned int Id;
- unsigned char len = 0;
- unsigned char buf[8];
- /* --- Odebrane --- */
- /* --- Wysylka --- */
- unsigned char x = 1;
- unsigned char stmp[8] = {5, 0, 0, 0, 0, 0, 0, 0}; // dane przygotowane do wysłania
- /* --- Wysylka --- */
- /* --- Pierwsze Odebranie --- */
- unsigned char sentMessage[8] = {5, 0, 0, 0, 0, 0, 0, 0}; // Wiadomosc do wyslania
- unsigned char receivedMessage[8];
- /* --- Pierwsze Odebranie --- */
- /* --- Logowanie --- */
- unsigned char loggedMessage[8] = {9, 0, 0, 0, 0, 0, 0, 0};
- unsigned char receivedLoginMessage[8];
- /* --- Logowanie --- */
- void setup()
- {
- /* --- Konfiguracja --- */
- Serial.begin(9600);
- if (CAN.begin(MCP_ANY, CAN_100KBPS, MCP_8MHZ) == CAN_OK)
- Serial.println("Inicjalizacja mcp2515 udana! Arduino 2");
- else Serial.println("Błąd w inicjalizacji mcp2515 :(");
- CAN.setMode(MCP_NORMAL);
- }
- /* --- Konfiguracja --- */
- void loop()
- {
- /* --- Pierwsze Odebranie --- */
- if (Value == 0) {
- if (CAN_MSGAVAIL == CAN.checkReceive()) // sprawdza czy jest wiadomość
- {
- CAN.readMsgBuf(&Id, &len, receivedMessage);
- if (receivedMessage[0] == 4) {
- CAN.sendMsgBuf(0x10, 0, 8, sentMessage);
- Value = 1;
- Serial.println("Otrzymano komunikat powitalny");
- }
- }
- }
- /* --- Pierwsze Odebranie --- */
- /* --- Logowanie --- */
- if (Value == 1) {
- if (CAN_MSGAVAIL == CAN.checkReceive()) // sprawdza czy jest wiadomość
- {
- CAN.readMsgBuf(&Id, &len, receivedLoginMessage);
- Serial.println(receivedLoginMessage[0]);
- if (receivedLoginMessage[0] == 1) {
- CAN.sendMsgBuf(0x10, 0, 8, loggedMessage);
- Serial.println("Zalogowano pomyslnie");
- Value = 2;
- }
- else
- Serial.println("Blad logowania");
- }
- }
- /* --- Logowanie --- */
- }
Advertisement
Add Comment
Please, Sign In to add comment