March 28, 2024, 11:09:46 PM

CAN-bus analyzer on vehicle

Started by kevin1234, September 19, 2017, 12:21:28 PM

Previous topic - Next topic

kevin1234

I would like to build CAN (Controller Area Network) analyzer for monitoring CAN bus on vehicles (not for diagnostics, just to see simple message frames with identifiers, etc.). This is my code but I cant receive any frame in my Terminal usart, does anyone have any experience? I really appreciate it because of about 7 days stuck in and Im using (STM-P103 BOARDS) :) :) :) :) ;) ;) ;


/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
#include "USART.h"
#include "CAN.h"
#include <stdio.h>

unsigned int val_Tx = 0, val_Rx = 0;              /* Globals used for display */

void Delay (uint32_t Time);
/*----------------------------------------------------------------------------
  fputc
*----------------------------------------------------------------------------*/
int fputc(int ch, FILE *f) {
  return (USART2_SendChar(ch));
}


/*----------------------------------------------------------------------------
  initialize CAN interface
*----------------------------------------------------------------------------*/
void can_Init (void) {

  CAN_setup ();                                   /* setup CAN Controller     */
  CAN_wrFilter (33, STANDARD_FORMAT);             /* Enable reception of msgs */

  /* ! COMMENT LINE BELOW TO ENABLE DEVICE TO PARTICIPATE IN CAN NETWORK !    */
  CAN_testmode(CAN_BTR_SILM | CAN_BTR_LBKM);      // Loopback, Silent Mode (self-test)

  CAN_start ();                                   /* start CAN Controller   */

  CAN_waitReady ();                               /* wait til tx mbx is empty */
}

//*****************************************************************************
int main(void)
{
    int i;
    char  str[50];
     SystemInit();            
    USART2_Init();
     USART2_SendString("\n\r-----------------------");
     USART2_SendString("\n\r");
     USART2_SendString("\n\r");
    USART2_SendString("\n\r");
     USART2_SendString("\n\r");

      
can_Init ();                                    /* initialize CAN interface */

  CAN_TxMsg.id = 33;                              /* initialize msg to send   */
  for (i = 0; i < 8; i++) CAN_TxMsg.data =0;
  CAN_TxMsg.len = 1;
  CAN_TxMsg.format = STANDARD_FORMAT;
  CAN_TxMsg.type = DATA_FRAME;

  while (1) {
      
   if (CAN_TxRdy) {                              /* tx msg on CAN Ctrl       */
      CAN_TxRdy = 0;

      CAN_TxMsg.data[0] = val_Tx++;                 /* data[0] = ADC value      */
      CAN_wrMsg (&CAN_TxMsg);                     /* transmit message         */
         USART2_SendString("\r\nTransfer      ");
         sprintf(str,"%i ", CAN_TxMsg.data[0]);
         USART2_SendString(str);
    }

   Delay (100);                                   /* delay for 10ms           */
// 0      Receive      11:49:09:845      Data frame      Standard frame      00000130      5      05 f0 fc ff ff 
      // format;             // 0 - STANDARD, 1- EXTENDED IDENTIFIER
    //type;               // 0 - DATA FRAME, 1 - REMOTE FRAME
    if (CAN_RxRdy) {                              /* rx msg on CAN Ctrl       */
        USART2_SendString("\r\nReceive      ");   
        if(CAN_RxMsg.type == 0)
            {
               USART2_SendString("DATA FRAME      ");
            }
            else
               {
                 USART2_SendString("REMOTE FRAME   ");
              }
            if(CAN_RxMsg.format == 0)
            {
               USART2_SendString("STANDARD     ");
            }
            else
               {
                 USART2_SendString("EXTENDED IDENTIFIER      ");
              }   
           sprintf(str,"%x      ", CAN_RxMsg.id);
           USART2_SendString(str);
         
           sprintf(str,"%x      ", CAN_RxMsg.len);
           USART2_SendString(str);
         for (i=0;i<CAN_RxMsg.len ;i++)
         {
           sprintf(str,"%x      ", CAN_RxMsg.data);
           USART2_SendString(str);
         }
         
     CAN_RxRdy = 0;
    }
  }
}
//---------------------------------------------------------------------------------------------//

KeesZagers

Hi Kevin,

I must admit that I'm not familiar with this environment, but I guess that if you want to see any real messages you should not put the CANbus in testmode. In this case nothing happens on the bus, only internally the Tx message is received by the Rx. Furtheron you have set the filter to ID 33, which means that only the message with ID 33 is received. To be active on the bus, you should also set the right bitrate. To see messages you don't need to send any message.

Kees

kevin1234

Hi, kees

Thank you so much, Its really helpful but I'm new with STM32F103 do you know any example of the Vehicle CAN bus monitoring ?

Thanks alot

KeesZagers

Unfortunally I don't have any experience with the STM32 environment. But if you look into the source of the CAN library it should be possible to do the steps I described in my earlier response.
- In setup set a bitrate and maybe some other parameters
- No filtering and no testmode
- Start the CANbus
- No Tx if you only want to monitor the bus
- Now loop for Rx messages

Hope this helps

Kees

kevin1234