Advertisement
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);
- /* --- Odebrane --- */
- long unsigned int Id;
- unsigned char len = 0;
- unsigned char buf[8];
- /* --- Odebrane --- */
- /* --- Wysylka --- */
- int Value = 0;
- unsigned char x = 1;
- unsigned char stmp[8] = {5, 0, 0, 0, 0, 0, 0, 0}; // dane przygotowane do wysłania
- /* --- Wysylka --- */
- /* --- Pierwsza wiadomosc --- */
- unsigned char firstMessage[8] = {4, 0, 0, 0, 0, 0, 0, 0};
- unsigned char receivedMessage[8];
- /* --- Pierwsza wiadomosc --- */
- /* --- Logowanie --- */
- unsigned char loginMessage[8] = {1, 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 1");
- else Serial.println("Błąd w inicjalizacji mcp2515 :(");
- CAN.setMode(MCP_NORMAL);
- }
- /* --- Konfiguracja --- */
- void loop()
- {
- /* --- Pierwsza wiadomosc --- */
- if (Value == 0) {
- CAN.sendMsgBuf(0x10, 0, 8, firstMessage);
- if (CAN_MSGAVAIL == CAN.checkReceive())
- {
- CAN.readMsgBuf(&Id, &len, receivedMessage);
- Serial.println(receivedMessage[0]);
- if (receivedMessage[0] == 5) {
- Value = 1;
- Serial.println("Pomyslnie zainicjalizowano polaczenie");
- delay(1000);
- }
- else
- Serial.println("Blad polaczenia");
- }
- }
- /* --- Pierwsza wiadomosc --- */
- /* --- Logowanie --- */
- if (Value == 1) {
- CAN.sendMsgBuf(0x10, 0, 8, loginMessage);
- if (CAN_MSGAVAIL == CAN.checkReceive()) {
- CAN.readMsgBuf(&Id, &len, receivedLoginMessage);
- if (receivedLoginMessage[0] == 9) {
- Serial.println("Zalogowano Pomyslnie");
- Value = 2;
- }
- else
- Serial.println("Blad logowania");
- }
- }
- /* --- Logowanie --- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement