modbus_implement.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /**
  2. ******************************************************************************
  3. * @file : modbus_implement.h
  4. * @brief : Header for modbus_implement.c file.
  5. * This file contains the common defines of the application.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2025 STMicroelectronics.
  10. * All rights reserved.
  11. *
  12. * This software is licensed under terms that can be found in the LICENSE file
  13. * in the root directory of this software component.
  14. * If no LICENSE file comes with this software, it is provided AS-IS.
  15. *
  16. ******************************************************************************
  17. */
  18. /* Define to prevent recursive inclusion -------------------------------------*/
  19. #ifndef __MODBUS_IMPLEMENT_H
  20. #define __MODBUS_IMPLEMENT_H
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. /* Includes ------------------------------------------------------------------*/
  25. #include "stm32l4xx_hal.h"
  26. #define Cmd_FirmwareVersion (0x0000)
  27. #define Cmd_HardwareVersion (0x0008)
  28. #define Cmd_DeviceID (0x0009)
  29. #define Cmd_DeviceType (0x000B)
  30. #define Cmd_Reboot (0x000C)
  31. #define Cmd_Reset (0x000D)
  32. #define Cmd_Addr (0x0010)
  33. #define Cmd_Baudrate (0x0011)
  34. #define Cmd_DateTime (0x0012)
  35. #define Cmd_IapUpgrade (0xAABB)
  36. #define MB_FUNC_03 (0x03)
  37. #define MB_FUNC_06 (0x06)
  38. #define MB_FUNC_10 (0x10)
  39. #define MB_FUNC_42 (0x42) //for iap
  40. #define NO_EXCEPTION (0x00)
  41. #define INVALID_FUNCTION_CODE (0x01)
  42. #define INVALID_COMMAND (0x02)
  43. #define INVALID_DATA (0x03)
  44. #define DEVICE_FAULT (0x04)
  45. /* Exported functions prototypes ---------------------------------------------*/
  46. //读取时返回,数据长度, 如果返回 0, 说明buf_len 不够, 设备故障
  47. uint16_t Read_FirmwareVersion(uint8_t *pBuf, uint16_t buf_len);
  48. uint16_t Read_HardwareVersion(uint8_t *pBuf, uint16_t buf_len);
  49. uint16_t Read_Deviceid(uint8_t *pBuf, uint16_t buf_len);
  50. uint16_t Read_Devicetype(uint8_t *pBuf, uint16_t buf_len);
  51. uint16_t Read_Addr(uint8_t *pBuf, uint16_t buf_len);
  52. uint16_t Read_Baudrate(uint8_t *pBuf, uint16_t buf_len);
  53. #define RET_ERROR_MASK (0xF0)
  54. #define RET_OK (0x00)
  55. #define RET_NEED_SAVE (0X01)
  56. #define RET_NEED_REBOOT (0X02)
  57. #define RET_DATAINVALID (0x10)
  58. #define RET_CMDINVALID (0x20)
  59. //#define RET_DATAINVALID (3)
  60. //写入成功时返回 RET_OK
  61. //写入不成功时,返回 RET_DATAINVALID
  62. uint8_t Write_Addr(uint8_t *pdata, uint8_t len);
  63. uint8_t Write_Baudrate(uint8_t *pdata, uint8_t len);
  64. uint8_t Write_HardwareVersion(uint8_t *pdata, uint8_t len);
  65. uint8_t Write_Deviceid(uint8_t *pdata, uint8_t len);
  66. uint8_t Write_Devicetype(uint8_t *pdata, uint8_t len);
  67. #ifdef __cplusplus
  68. }
  69. #endif
  70. #endif /* __MODBUS_IMPLEMENT_H */