2020-01-06 08:31 AM
CAN_FilterTypeDef sFilterConfig;
CAN_TxHeaderTypeDef TxHeader;
CAN_RxHeaderTypeDef RxHeader;
uint8_t TxData[8];
uint8_t RxData[8];
uint32_t TxMailbox;
/* USER CODE END PV */
/* Private function prototypes ----------Ча�?тные функции прототипов-------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_CAN_Init(void);
static void MX_WWDG_Init(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_CAN_Init();
MX_WWDG_Init();
/* USER CODE BEGIN 2 */
sFilterConfig.FilterBank=0;
sFilterConfig.FilterMode=CAN_FILTERMODE_IDMASK;
sFilterConfig.FilterScale=CAN_FILTERSCALE_32BIT;
sFilterConfig.FilterIdHigh=0x0000;
sFilterConfig.FilterIdLow=0x0000;
sFilterConfig.FilterMaskIdHigh=0x0000;
sFilterConfig.FilterMaskIdLow=0x0000;
sFilterConfig.FilterFIFOAssignment=CAN_RX_FIFO0;
sFilterConfig.FilterActivation=ENABLE;
sFilterConfig.SlaveStartFilterBank=14;
if(HAL_CAN_ConfigFilter(&hcan,&sFilterConfig)!=HAL_OK){
/* Filter configuration Error */
Error_Handler();
}
if(HAL_CAN_Start(&hcan)!=HAL_OK){
/* Start Error */
Error_Handler();
}
/*активируем обработчик прерывани�?*/
if (HAL_CAN_ActivateNotification(&hcan, CAN_IT_RX_FIFO0_MSG_PENDING) !=HAL_OK) { Error_Handler(); }
if (HAL_CAN_ActivateNotification(&hcan, CAN_IT_ERROR) !=HAL_OK) { Error_Handler(); }
/* Notification Error */
TxOffHeader.StdId=0x3B3; 0x43E; 0x171; 0x22A; 0x017;
TxOffHeader.ExtId=0x00;
TxOffHeader.RTR=CAN_RTR_DATA;
TxOffHeader.IDE=CAN_ID_STD;
TxOffHeader.DLC=8;
TxOffHeader.TransmitGlobalTime=DISABLE;
TxData[0]=0x10, 0x00, 0x10, 0x10, 0x10, 0x10;
TxData[1]=0x80, 0x9D, 0x80, 0x80, 0x80, 0x80;
TxData[2]=0x74, 0x10, 0x74, 0x74, 0x74, 0x74;
TxData[3]=0x07, 0x80, 0x07, 0x07, 0X07, 0x07;
TxData[4]=0x0F, 0x00, 0x0F, 0x0F; 0x0F, 0x0F;
TxData[5]=0xFF, 0x00, 0xFF, 0xFF; 0xFF, 0xFF;
TxData[6]=0x00, 0x00, 0x00, 0x00; 0x00, 0x00;
TxData[7]=0x00, 0x00, 0x00, 0x00; 0x00, 0x00;
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
if(TicksMs == 0){
TicksMs = PERIOD;
HAL_CAN_AddTxMessage(&hcan,&TxOffHeader,TxData,&TxMailbox);
HAL_GPIO_WritePin(Led2_GPIO_Port, Led2_Pin, GPIO_PIN_SET); //вкл �?ветодиод
}
/* USER CODE END WHILE */
/* USER CODE BEGIN 4 */
void HAL_CAN_TxMailbox0CompleteCallback(CAN_HandleTypeDef* hcan){
//HAL_GPIO_TogglePin(Led1_GPIO_Port, Led1_Pin);
}
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef* hcan){
HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &RxHeader,RxData);
//HAL_GPIO_TogglePin(Led2_GPIO_Port, Led2_Pin);
}
/* USER CODE END 4 */
2020-01-06 08:58 AM
Hard to guess at your implementation details.
For the transmit to succeed you'd need a receiving node on the network with equivalent bit rate setting to respond with an acknowledgement.
2020-01-06 11:39 AM
I haven't looked at the rest of the code, but what do you expect these lines to actually do?
TxOffHeader.StdId=0x3B3; 0x43E; 0x171; 0x22A; 0x017;
and
TxData[0]=0x10, 0x00, 0x10, 0x10, 0x10, 0x10;
TxData[1]=0x80, 0x9D, 0x80, 0x80, 0x80, 0x80;
TxData[2]=0x74, 0x10, 0x74, 0x74, 0x74, 0x74;
TxData[3]=0x07, 0x80, 0x07, 0x07, 0X07, 0x07;
TxData[4]=0x0F, 0x00, 0x0F, 0x0F; 0x0F, 0x0F;
TxData[5]=0xFF, 0x00, 0xFF, 0xFF; 0xFF, 0xFF;
TxData[6]=0x00, 0x00, 0x00, 0x00; 0x00, 0x00;
TxData[7]=0x00, 0x00, 0x00, 0x00; 0x00, 0x00;
2020-01-06 11:48 AM
I sent one identifier and one date.
TxOffHeader.StdId=0x3B3;
TxOffHeader.ExtId=0x00;
TxOffHeader.RTR=CAN_RTR_DATA;
TxOffHeader.IDE=CAN_ID_STD;
TxOffHeader.DLC=8;
TxOffHeader.TransmitGlobalTime=DISABLE;
TxData[0]=0x10;
TxData[1]=0x80;
TxData[2]=0x74;
TxData[3]=0x07;
TxData[4]=0x0F;
TxData[5]=0xFF;
TxData[6]=0x00;
TxData[7]=0x00;
I managed. But I tried sending some identifiers. And it was not successful.
2020-01-06 11:50 AM
It isn't C, I think it just shows the 5 sets of data being sent.
2020-01-06 11:57 AM
And how it should be right? I’m studying the can-bus of a car and while it’s hard for me
2020-01-06 12:06 PM
The commas in those lines are the "C" comma operator, which specifies a sequence of expressions on a "line". For example:
int a, b, c;
a = 1, b = 2, c = 3;
contains 3 assignments in a single "C" "line"; This code:
TxData[0]=0x10, 0x00, 0x10, 0x10, 0x10, 0x10;
is the same as:
2020-01-06 12:12 PM
%@$@ - I hit REPLY in stead of the "code" icon. Let me try again.
The commas in those lines are the "C" comma operator, which specifies a sequence of expressions on a "line". For example:
int a, b, c;
a = 1, b = 2, c = 3;
contains 3 assignments in a single "C" "line", which is perfectly valid. This code:
TxData[0]=0x10, 0x00, 0x10, 0x10, 0x10, 0x10;
is the same as:
(TxData[0]=0x10), 0x00, 0x10, 0x10, 0x10, 0x10;
which is also the same as:
TxData[0]=0x10;
0x00;
0x10;
0x10;
0x10;
0x10;
where everything after the first comma is ignored because it has no effect, and the compile should have complained bitterly about ignoring code that has no effect (mine does, but then I have LOTS of warnings enabled). The last example emphasizes the "this line doesn't do anything" nature of those excess constants.
2020-01-06 06:32 PM
If the other node is a car it's using the J1939 CAN protocol, which requires the full 29-bit extended address. What specific SAE PGN are you trying to access?
If your RTR request isn't properly formatted for J1939 I would not expect to see a reply. J1939 is sensitive to PGN objects, make sure you have the right one and it's supported on your car. Not every PGN is supported by a vendor. The packet will be ignored since it doesn't follow the protocol, or if your object doesn't exist.
Have you tried examining the CAN error registers to see if some bus type error is being reported? If you're being disconnected due to a bus timeout your request isn't being sent.
Jack Peacock
2020-01-06 10:54 PM
No no no, the protocol has nothing to do with it.I’m sending one message.I do not know how to write code for five messages.