comm_car_485(7462).c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #include "comm_car_485.h"
  2. //-------------define
  3. #define TIMEOUT_485 10 // 10S
  4. //-------------参数定义
  5. rs485RecDate s_rs485RecDate;
  6. rs485SendDate s_rs485SendDate;
  7. uint8_t timeoutCnt_485 = 0;
  8. /**
  9. * @brief 计算Modbus RTU消息的CRC校验码
  10. * @param ptr: 数据首地址
  11. * @param len: 数据长度
  12. * @return
  13. */
  14. uint16_t modbusCRC(uint8_t *buf, int len) {
  15. uint16_t crc = 0xFFFF;
  16. for (int pos = 0; pos < len; pos++) {
  17. crc ^= (uint16_t)buf[pos]; // XOR byte into CRC
  18. for (int i = 8; i != 0; i--) { // 循环8次
  19. if ((crc & 0x0001) != 0) { // 如果低位为1
  20. crc >>= 1; // 右移一位
  21. crc ^= 0xA001; // 与多项式码进行XOR
  22. } else {
  23. crc >>= 1; // 仅仅右移一位
  24. }
  25. }
  26. }
  27. return crc;
  28. }
  29. void modbus_recDate(void){
  30. HAL_UART_Receive_DMA(&huart3, (uint8_t*)g_usart3_rx_buf, USART3_REC_LEN); //设置接收缓冲区
  31. }
  32. /**
  33. * @brief 485发送函数
  34. * @param data: 发送的数据
  35. * @param dataLen: 发送的数据长度
  36. * @note
  37. * @retval 无
  38. */
  39. void modbus_sendDate(uint8_t data[], uint32_t dataLen){
  40. HAL_UART_Transmit(&huart3, data, dataLen, 1000);
  41. // 接收使能
  42. modbus_recDate();
  43. }
  44. // 构造Modbus RTU请求函数 03 功能码
  45. void modbus_read_holding_registers(uint8_t slave_id, uint16_t start_address, uint16_t num_of_registers) {
  46. // 请求数据包的长度为 8 个字节
  47. uint8_t request[8] = {0};
  48. // 设置从设备地址
  49. request[0] = slave_id;
  50. // 设置功能码,03 功能码用于读取保持寄存器
  51. request[1] = 0x03;
  52. // 设置寄存器起始地址
  53. request[2] = (start_address >> 8) & 0xFF; // 寄存器起始地址的高字节
  54. request[3] = start_address & 0xFF; // 寄存器起始地址的低字节
  55. // 设置读取寄存器的数量
  56. request[4] = (num_of_registers >> 8) & 0xFF; // 要读取的寄存器数量的高字节
  57. request[5] = num_of_registers & 0xFF; // 要读取的寄存器
  58. // 计算CRC
  59. uint16_t crc = modbusCRC(request, 6);
  60. request[6] = crc & 0xFF; // CRC低位
  61. request[7] = crc >> 8; // CRC高位
  62. // 发送请求
  63. modbus_sendDate(request, 8);
  64. }
  65. // 构建Modbus 10功能码(写多个寄存器)请求帧
  66. // 注意:调用此函数时需要确保`request`数组有足够的空间来存放整个请求帧,此空间应大于或等于 9 + 2 * numRegisters 字节。
  67. void modbus_write_holding_registers(uint8_t slaveID, uint16_t startAddress, uint16_t numRegisters, uint16_t *values) {
  68. uint8_t request[256] = {0}; // 确保有足够的空间
  69. uint16_t requestSize = 0;
  70. int i;
  71. request[0] = slaveID; // 从设备地址
  72. request[1] = 0x10; // 功能码(写多个寄存器)
  73. request[2] = startAddress >> 8; // 起始地址高位
  74. request[3] = startAddress & 0xFF; // 起始地址低位
  75. request[4] = numRegisters >> 8; // 寄存器数量高位
  76. request[5] = numRegisters & 0xFF; // 寄存器数量低位
  77. request[6] = numRegisters * 2; // 字节计数(每个寄存器2字节)
  78. // 循环设置寄存器的值
  79. for (i = 0; i < numRegisters; i++) {
  80. request[7 + 2*i] = values[i] >> 8; // 寄存器值高字节
  81. request[8 + 2*i] = values[i] & 0xFF; // 寄存器值低字节
  82. }
  83. // 计算整个请求帧的大小
  84. requestSize = 7 + 2 * numRegisters;
  85. // 计算CRC
  86. uint16_t crc = modbusCRC(request, requestSize);
  87. request[requestSize] = crc & 0xFF; // CRC低位
  88. request[requestSize + 1] = crc >> 8; // CRC高位
  89. // 更新请求帧大小以包含CRC
  90. requestSize += 2;
  91. // 发送请求
  92. modbus_sendDate(request, requestSize);
  93. }
  94. // 验证Modbus RTU消息的CRC校验码
  95. int checkModbusCRC(uint8_t *response, uint16_t responseSize) {
  96. if (responseSize < 4) return -1; // 不够长,无法包含CRC
  97. uint16_t receivedCRC = response[responseSize - 2] | response[responseSize - 1] << 8;
  98. uint16_t calculatedCRC = modbusCRC(response, responseSize - 2);
  99. return (receivedCRC == calculatedCRC) ? 0 : -1;
  100. }
  101. // 解析Modbus 03功能码的响应,并带有错误处理和完整性校验
  102. int parseModbus03Response(uint8_t *response, uint16_t responseSize) {
  103. rs485RecDate *p_rs485RecDate = &s_rs485RecDate;
  104. // 最小长度检查
  105. if (responseSize < 5 || response[1] != 0x03) {
  106. printf("Response too short or function code mismatch!\n");
  107. return -1;
  108. }
  109. // 解析响应数据 从第四个字节开始为第一个数据
  110. p_rs485RecDate->vehicleSpeed = ((uint16_t)response[3] << 8) | response[4];
  111. p_rs485RecDate->dailyDrivTime = ((uint16_t)response[5] << 8) | response[6];
  112. p_rs485RecDate->dailyDrivMileage = ((uint16_t)response[7] << 8) | response[8];
  113. p_rs485RecDate->accTotalDrivTime_day = ((uint16_t)response[9] << 8) | response[10];
  114. p_rs485RecDate->accTotalDrivTime_h_min = ((uint16_t)response[11] << 8) | response[12];
  115. p_rs485RecDate->accTotalMileage_h = ((uint16_t)response[13] << 8) | response[14];
  116. p_rs485RecDate->accTotalMileage_l = ((uint16_t)response[15] << 8) | response[16];
  117. p_rs485RecDate->runTime = ((uint16_t)response[17] << 8) | response[18];
  118. p_rs485RecDate->batCompartmentTemp = ((uint16_t)response[19] << 8) | response[20];
  119. p_rs485RecDate->demandCur = ((uint16_t)response[21] << 8) | response[22];
  120. p_rs485RecDate->demandVol = ((uint16_t)response[23] << 8) | response[24];
  121. p_rs485RecDate->alarmLevel = ((uint16_t)response[25] << 8) | response[26];
  122. p_rs485RecDate->alarmType = ((uint16_t)response[27] << 8) | response[28];
  123. memcpy(p_rs485RecDate->batSn, &response[29], 40);
  124. memcpy(p_rs485RecDate->Vehicle_Num, &response[69], 20);
  125. memcpy(p_rs485RecDate->VIN, &response[89], 26);
  126. s_comData.vinRecSuccess = 1; // 成功获取到车的数据
  127. return 0; // 成功
  128. }
  129. // 解析Modbus 10功能码的响应,并带有错误处理和完整性校验
  130. int parseModbus10Response(uint8_t *response, uint16_t responseSize) {
  131. // 最小长度检查
  132. if (responseSize != 8 || response[1] != 0x10) {
  133. printf("Response length mismatch or function code mismatch!\n");
  134. return -1;
  135. }
  136. // 解析响应数据
  137. uint16_t startAddress = response[2] << 8 | response[3];
  138. uint16_t numRegisters = response[4] << 8 | response[5];
  139. // 打印结果
  140. printf("Written to start address: %d, number of registers: %d\n", startAddress, numRegisters);
  141. return 0; // 成功
  142. }
  143. /**
  144. * @brief 485接收分析函数
  145. * @param response: 接收到的数据
  146. * @note 如果在中断中进行解析,则不能加互斥量,如果是在任务中解析,则加入互斥量的获取
  147. * @retval 无
  148. */
  149. int parseModbusResponse(uint8_t *response, uint16_t responseSize){
  150. uint8_t fun_Code = 0;
  151. // 检查CRC
  152. if (checkModbusCRC(response, responseSize) != 0) {
  153. printf("CRC check failed!\n");
  154. return -1;
  155. }
  156. // 确认功能码
  157. fun_Code = response[1];
  158. // 数据解析
  159. switch(fun_Code){
  160. case 0x03:
  161. parseModbus03Response(response, responseSize);
  162. break;
  163. case 0x10:
  164. parseModbus10Response(response, responseSize);
  165. break;
  166. default:
  167. break;
  168. }
  169. return 0; // 成功
  170. }
  171. /**
  172. * @brief 轮询发送接收
  173. * @note 1s请求一次
  174. * @retval 无
  175. */
  176. void rs485_poll_sendReceive(uint16_t speed){
  177. static uint8_t step_485 = 0;
  178. switch(step_485){
  179. case 0:
  180. // 读寄存器数据
  181. modbus_read_holding_registers(0x01, 0x0000, 56);
  182. step_485 = 1;
  183. break;
  184. case 1:
  185. // 写寄存器数据
  186. modbus_write_holding_registers(0x01, 0x0010, 1, &speed);
  187. step_485 = 0;
  188. break;
  189. default:
  190. break;
  191. }
  192. if(timeoutCnt_485 < TIMEOUT_485){
  193. timeoutCnt_485++;
  194. }else{
  195. timeoutCnt_485 = TIMEOUT_485;
  196. BIT_SET(s_comData.Malfunction, com485); // 置位485通信故障
  197. }
  198. }