dlt645.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*************************************************
  2. Copyright (c) 2019
  3. All rights reserved.
  4. File name: dlt645.c
  5. Description:
  6. History:
  7. 1. Version:
  8. Date: 2019-09-20
  9. Author: wangjunjie
  10. Modify:
  11. *************************************************/
  12. #include "dlt645_private.h"
  13. #include "dlt645_1997.h"
  14. #include "dlt645_2007.h"
  15. #include "string.h"
  16. /**
  17. * Name: dlt645_receive_msg
  18. * Brief: 645协议调用底层接收数据
  19. * Input:
  20. * @ctx: 645环境句柄
  21. * @msg: 数据包存储地址
  22. * @len: 最大接收长度
  23. * @addr: 从站地址
  24. * @code: 数据标识
  25. * @protocal: 645协议类型
  26. * Output: 接收成功:0,接收失败:-1
  27. */
  28. int dlt645_receive_msg(dlt645_t *ctx, uint8_t *msg, uint16_t len, uint32_t code, dlt645_protocal protocal)
  29. {
  30. int msg_len = ctx->read(ctx, msg, len);
  31. if (protocal == DLT645_1997)
  32. {
  33. return dlt645_1997_recv_check(msg, msg_len, ctx->addr, code);
  34. }
  35. else if (protocal == DLT645_2007)
  36. {
  37. return dlt645_2007_recv_check(msg, msg_len, ctx->addr, code);
  38. }
  39. else
  40. {
  41. return -1;
  42. }
  43. }
  44. /**
  45. * Name: dlt645_send_msg
  46. * Brief: 645协议调用底层发送数据
  47. * Input:
  48. * @ctx: 645环境句柄
  49. * @msg: 发送的数据首地址
  50. * @len: 发送长度
  51. * Output: 实际发送的字节数,错误返回-1
  52. */
  53. int dlt645_send_msg(dlt645_t *ctx, uint8_t *msg, int len)
  54. {
  55. msg[0] = DL645_START_CODE;
  56. msg[DL645_ADDR_LEN + 1] = DL645_START_CODE;
  57. msg[len - 1] = DL645_STOP_CODE;
  58. msg[len - 2] = _crc(msg, len - 2);
  59. return ctx->write(ctx, msg, len);
  60. }
  61. /**
  62. * Name: dlt645_set_addr
  63. * Brief: 设置从站地址
  64. * Input:
  65. * @ctx: 645环境句柄
  66. * @addr: 从站地址
  67. * Output: None
  68. */
  69. void dlt645_set_addr(dlt645_t *ctx, uint8_t *addr)
  70. {
  71. uint8_t addr_temp[6];
  72. memset(addr_temp, 0, 6);
  73. //转换字节序
  74. for (int i = 0; i < 6; i++)
  75. {
  76. addr_temp[5 - i] = addr[i];
  77. }
  78. memcpy(ctx->addr, addr_temp, DL645_ADDR_LEN);
  79. }
  80. /**
  81. * Name: dlt645_set_debug
  82. * Brief: 设置调试模式
  83. * Input:
  84. * @ctx: 645环境句柄
  85. * @flag: 调试标志
  86. * Output: None
  87. */
  88. int dlt645_set_debug(dlt645_t *ctx, int flag)
  89. {
  90. ctx->debug = flag;
  91. return 0;
  92. }
  93. /**
  94. * Name: dlt645_read_data(用户调用)
  95. * Brief: 根据645协议类型读取数据
  96. * Input:
  97. * @ctx: 645环境句柄
  98. * @addr: 从站地址
  99. * @code: 标识符
  100. * @read_data: 读取数据存储地址
  101. * @protocal: 协议类型
  102. * Output: 成功返回数据长度,失败返回-1
  103. */
  104. int dlt645_read_data(dlt645_t *ctx,
  105. uint32_t code,
  106. uint8_t *read_data,
  107. dlt645_protocal protocal)
  108. {
  109. int rs = -1;
  110. switch (protocal)
  111. {
  112. case DLT645_1997:
  113. rs = dlt645_1997_read_data(ctx, code, read_data);
  114. break;
  115. case DLT645_2007:
  116. rs = dlt645_2007_read_data(ctx, code, read_data);
  117. break;
  118. default:
  119. DLT645_LOG("unrecognized protocal!\r\n");
  120. break;
  121. }
  122. return rs;
  123. }