Member #1442119

Member Since: September 25, 2018

Country: United States

  • this is kind of a crude way to do it but I put this program on both can485 boards. the only difference is the message ID between the two. Each one is getting a pushbutton input to pin 2 that can turn on output pin 5 on the other board

    #include <ASTCanLib.h>  
    
    #define MESSAGE_ID        250
    #define MESSAGE_PROTOCOL  1
    #define MESSAGE_LENGTH    8 
    #define MESSAGE_RTR       0 
    
     st_cmd_t Msg;
    
     uint8_t Buffer[8] = {};
    
     void setup() {
      canInit(500000);                  // Initialise CAN port. must be before Serial.begin
      Serial.begin(1000000);             // start serial port
      Msg.pt_data = &Buffer[0];      // reference message data to transmit buffer
      pinMode(5, OUTPUT);
      Msg.ctrl.ide = MESSAGE_PROTOCOL;  // Set CAN protocol (0: CAN 2.0A, 1: CAN 2.0B)
      Msg.id.ext   = MESSAGE_ID;        // Set message ID
      Msg.dlc      = MESSAGE_LENGTH;    // Data length: 8 bytes
      Msg.ctrl.rtr = MESSAGE_RTR;       // Set rtr bit
      }
    
    void loop() {
    tx:
      int buttonState = digitalRead(2);
      if (buttonState == HIGH){
    
        Buffer[0] = 10;
      Msg.ctrl.ide = MESSAGE_PROTOCOL;  // Set CAN protocol (0: CAN 2.0A, 1: CAN 2.0B)
      Msg.id.ext   = MESSAGE_ID;        // Set message ID
      Msg.dlc      = MESSAGE_LENGTH;    // Data length: 8 bytes
      Msg.ctrl.rtr = MESSAGE_RTR;       // Set rtr bit
      Msg.cmd = CMD_TX_DATA;      
      while(can_cmd(&Msg) != CAN_CMD_ACCEPTED);
      while(can_get_status(&Msg) == CAN_STATUS_NOT_COMPLETED);
        goto tx;}
        else{Buffer[0] = 30;
               Msg.ctrl.ide = MESSAGE_PROTOCOL;  // Set CAN protocol (0: CAN 2.0A, 1: CAN 2.0B)
      Msg.id.ext   = MESSAGE_ID;        // Set message ID
      Msg.dlc      = MESSAGE_LENGTH;    // Data length: 8 bytes
      Msg.ctrl.rtr = MESSAGE_RTR;       // Set rtr bit
            Msg.cmd = CMD_TX_DATA;      
      while(can_cmd(&Msg) != CAN_CMD_ACCEPTED);
      while(can_get_status(&Msg) == CAN_STATUS_NOT_COMPLETED);
        }
    
    Msg.cmd = CMD_RX_DATA;
    while(can_cmd(&Msg) != CAN_CMD_ACCEPTED);
    while(can_get_status(&Msg) == CAN_STATUS_NOT_COMPLETED);
    
    if (Buffer[0] == 10){
        digitalWrite(5, HIGH);}
        else if (Buffer[0] == 30) {
        digitalWrite(5, LOW);}
    }
    
  • I was getting these same errors with two of these setup one as CAN transmitter, one as CAN receiver. On the receiver in void setup i entered pinMode(13, OUTPUT); to use the onboard LED to indicate it saw a change in a CAN packet byte. I think the problem was this is SCK, serial clock, pin also. Once I uploaded the program i wouldn't be able to load another program to it until i reburned the bootloader to the board with an Arduino. Arduino IDE would just give me content mismatch and say board is not connected.

No public wish lists :(