本帖最后由 奏奏奏 于 2017-1-2 23:14 编辑 用的芯片是STM32F107
因为它有两个CAN口
做CAN的中继来用
问题是将CAN1口的接收数据用CAN2口发出是正常的
但是反过来将CAN2口的接收数据用CAN1口发出就出问题了
所以就在考虑是否关系到这个问题:CAN1口是master mode,而CAN2口是slave mode,是否CAN2口的接收中断用法与CAN1口的不一样
贴一下代码:
- void HAL_CAN_RxCpltCallback(CAN_HandleTypeDef* hcan)
- {
- if(hcan == &hcan1)
- {
- printf("\r\n CAN receive ID=%x",hcan->pRxMsg->StdId);
- printf("\r\n CAN receive length=%d \r\n CAN receive data:\r\n",hcan->pRxMsg->DLC);
- uint8_t j=hcan->pRxMsg->DLC;
- for(uint8_t i=0;i<j;i++)
- {
- printf(" %x ",hcan->pRxMsg->Data);
- }
- printf("\r\n");
-
- //转发到CAN2端口
-
- //
- /*##-1- Configure CAN2 Transmission Massage #####################################*/
- hcan2.pTxMsg->StdId = hcan->pRxMsg->StdId;
- hcan2.pTxMsg->RTR = CAN_RTR_DATA;
- hcan2.pTxMsg->IDE = CAN_ID_STD;
- hcan2.pTxMsg->DLC = hcan->pRxMsg->DLC;
- uint8_t j2=hcan->pRxMsg->DLC;
- for(uint8_t i=0;i<j2;i++)
- {
- hcan2.pTxMsg->Data =hcan->pRxMsg->Data;
- }
- HAL_CAN_Transmit(&hcan2, 10);
- //
-
- __HAL_CAN_ENABLE_IT(hcan,CAN_IT_FMP0);//重新开启FIF00消息挂号中断
- }
- if(hcan == &hcan2)
- {
- printf("\r\n CAN receive ID=%x",hcan->pRxMsg->StdId);
- printf("\r\n CAN receive length=%d \r\n CAN receive data:\r\n",hcan->pRxMsg->DLC);
- uint8_t j=hcan->pRxMsg->DLC;
- for(uint8_t i=0;i<j;i++)
- {
- printf(" %x ",hcan->pRxMsg->Data);
- }
- printf("\r\n");
-
- //转发到CAN1端口
-
- //
- /*##-1- Configure CAN2 Transmission Massage #####################################*/
- hcan1.pTxMsg->StdId = hcan->pRxMsg->StdId;
- hcan1.pTxMsg->RTR = CAN_RTR_DATA;
- hcan1.pTxMsg->IDE = CAN_ID_STD;
- hcan1.pTxMsg->DLC = hcan->pRxMsg->DLC;
- uint8_t j2=hcan->pRxMsg->DLC;
- for(uint8_t i=0;i<j2;i++)
- {
- hcan1.pTxMsg->Data =hcan->pRxMsg->Data;
- }
- HAL_CAN_Transmit_IT(&hcan1);
- //
-
- __HAL_CAN_ENABLE_IT(hcan,CAN_IT_FMP0);//重新开启FIF00消息挂号中断
- }
- }
复制代码
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。