_modbus.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #include "_modbus__ModBus.h"
  2. #include "agile_modbus.h"
  3. #if !PIKASCRIPT_VERSION_REQUIRE_MINIMUN(1, 11, 1)
  4. #error "pikascript version must be greater than 1.11.1"
  5. #endif
  6. void _modbus__ModBus___init__rtu(PikaObj* self,
  7. int sendBUffSize,
  8. int readBuffSize) {
  9. agile_modbus_rtu_t ctx_rtu = {0};
  10. obj_setBytes(self, "sendBuff", NULL, sendBUffSize);
  11. obj_setBytes(self, "readBuff", NULL, readBuffSize);
  12. agile_modbus_rtu_init(&ctx_rtu, obj_getBytes(self, "sendBuff"),
  13. sendBUffSize, obj_getBytes(self, "readBuff"),
  14. readBuffSize);
  15. obj_setStruct(self, "ctx_rtu", ctx_rtu);
  16. agile_modbus_rtu_t* ctx_rtu_heap = obj_getStruct(self, "ctx_rtu");
  17. obj_setPtr(self, "ctx", &ctx_rtu_heap->_ctx);
  18. }
  19. void _modbus__ModBus_setSlave(PikaObj* self, int slave) {
  20. agile_modbus_t* ctx = obj_getPtr(self, "ctx");
  21. agile_modbus_set_slave(ctx, slave);
  22. }
  23. void _modbus__ModBus___init__tcp(PikaObj* self,
  24. int sendBuffSize,
  25. int readBuffSize) {
  26. agile_modbus_tcp_t ctx_tcp = {0};
  27. obj_setBytes(self, "sendBuff", NULL, sendBuffSize);
  28. obj_setBytes(self, "readBuff", NULL, readBuffSize);
  29. agile_modbus_tcp_init(&ctx_tcp, obj_getBytes(self, "sendBuff"),
  30. sendBuffSize, obj_getBytes(self, "readBuff"),
  31. readBuffSize);
  32. obj_setStruct(self, "ctx_tcp", ctx_tcp);
  33. agile_modbus_tcp_t* ctx_tcp_heap = obj_getStruct(self, "ctx_tcp");
  34. obj_setPtr(self, "ctx", &ctx_tcp_heap->_ctx);
  35. }
  36. int _modbus__ModBus_deserializeMaskWriteRegister(PikaObj* self, int msgLength) {
  37. agile_modbus_t* ctx = obj_getPtr(self, "ctx");
  38. return agile_modbus_deserialize_mask_write_register(ctx, msgLength);
  39. }
  40. Arg* _modbus__ModBus_deserializeReadRegisters(PikaObj* self, int msgLength) {
  41. uint16_t buff[128] = {0};
  42. agile_modbus_t* ctx = obj_getPtr(self, "ctx");
  43. int len = agile_modbus_deserialize_read_registers(ctx, msgLength,
  44. (uint16_t*)buff);
  45. if (len < 0) {
  46. return NULL;
  47. }
  48. return arg_newBytes((uint8_t*)buff, len * 2);
  49. }
  50. Arg* _modbus__ModBus_deserializeReadBits(PikaObj* self, int msgLength) {
  51. uint8_t buff[128] = {0};
  52. agile_modbus_t* ctx = obj_getPtr(self, "ctx");
  53. int len = agile_modbus_deserialize_read_bits(ctx, msgLength, buff);
  54. if (len < 0) {
  55. return NULL;
  56. }
  57. return arg_newBytes(buff, len);
  58. }
  59. Arg* _modbus__ModBus_deserializeReadInputBits(PikaObj* self, int msgLength) {
  60. uint8_t buff[128] = {0};
  61. agile_modbus_t* ctx = obj_getPtr(self, "ctx");
  62. int len = agile_modbus_deserialize_read_input_bits(ctx, msgLength, buff);
  63. if (len < 0) {
  64. return NULL;
  65. }
  66. return arg_newBytes(buff, len);
  67. }
  68. Arg* _modbus__ModBus_deserializeReadInputRegisters(PikaObj* self,
  69. int msgLength) {
  70. uint16_t buff[128] = {0};
  71. agile_modbus_t* ctx = obj_getPtr(self, "ctx");
  72. int len = agile_modbus_deserialize_read_input_registers(ctx, msgLength,
  73. (uint16_t*)buff);
  74. if (len < 0) {
  75. return NULL;
  76. }
  77. return arg_newBytes((uint8_t*)buff, len * 2);
  78. }
  79. Arg* _modbus__ModBus_deserializeReportSlaveId(PikaObj* self,
  80. int msgLength,
  81. int maxDest) {
  82. uint8_t buff[128] = {0};
  83. agile_modbus_t* ctx = obj_getPtr(self, "ctx");
  84. int len = agile_modbus_deserialize_report_slave_id(ctx, msgLength, maxDest,
  85. (uint8_t*)buff);
  86. if (len < 0) {
  87. return NULL;
  88. }
  89. return arg_newBytes(buff, len);
  90. }
  91. Arg* _modbus__ModBus_deserializeWriteAndReadRegisters(PikaObj* self,
  92. int msgLength) {
  93. uint16_t buff[128] = {0};
  94. agile_modbus_t* ctx = obj_getPtr(self, "ctx");
  95. int len = agile_modbus_deserialize_write_and_read_registers(
  96. ctx, msgLength, (uint16_t*)buff);
  97. if (len < 0) {
  98. return NULL;
  99. }
  100. return arg_newBytes((uint8_t*)buff, len * 2);
  101. }
  102. int _modbus__ModBus_deserializeWriteBit(PikaObj* self, int msgLength) {
  103. agile_modbus_t* ctx = obj_getPtr(self, "ctx");
  104. return agile_modbus_deserialize_write_bit(ctx, msgLength);
  105. }
  106. int _modbus__ModBus_deserializeWriteBits(PikaObj* self, int msgLength) {
  107. agile_modbus_t* ctx = obj_getPtr(self, "ctx");
  108. return agile_modbus_deserialize_write_bits(ctx, msgLength);
  109. }
  110. int _modbus__ModBus_deserializeWriteRegister(PikaObj* self, int msgLength) {
  111. agile_modbus_t* ctx = obj_getPtr(self, "ctx");
  112. return agile_modbus_deserialize_write_register(ctx, msgLength);
  113. }
  114. int _modbus__ModBus_deserializeWriteRegisters(PikaObj* self, int msgLength) {
  115. agile_modbus_t* ctx = obj_getPtr(self, "ctx");
  116. return agile_modbus_deserialize_write_registers(ctx, msgLength);
  117. }
  118. int _modbus__ModBus_serializeMaskWriteRegister(PikaObj* self,
  119. int addr,
  120. int andMask,
  121. int orMask) {
  122. agile_modbus_t* ctx = obj_getPtr(self, "ctx");
  123. return agile_modbus_serialize_mask_write_register(ctx, addr, andMask,
  124. orMask);
  125. }
  126. int _modbus__ModBus_serializeReadBits(PikaObj* self, int addr, int nb) {
  127. agile_modbus_t* ctx = obj_getPtr(self, "ctx");
  128. return agile_modbus_serialize_read_bits(ctx, addr, nb);
  129. }
  130. int _modbus__ModBus_serializeReadInputBits(PikaObj* self, int addr, int nb) {
  131. agile_modbus_t* ctx = obj_getPtr(self, "ctx");
  132. return agile_modbus_serialize_read_input_bits(ctx, addr, nb);
  133. }
  134. int _modbus__ModBus_serializeReadInputRegisters(PikaObj* self,
  135. int addr,
  136. int nb) {
  137. agile_modbus_t* ctx = obj_getPtr(self, "ctx");
  138. return agile_modbus_serialize_read_input_registers(ctx, addr, nb);
  139. }
  140. int _modbus__ModBus_serializeReadRegisters(PikaObj* self, int addr, int nb) {
  141. agile_modbus_t* ctx = obj_getPtr(self, "ctx");
  142. return agile_modbus_serialize_read_registers(ctx, addr, nb);
  143. }
  144. int _modbus__ModBus_serializeReportSlaveId(PikaObj* self) {
  145. agile_modbus_t* ctx = obj_getPtr(self, "ctx");
  146. return agile_modbus_serialize_report_slave_id(ctx);
  147. }
  148. int _modbus__ModBus_serializeWriteAndReadRegisters(PikaObj* self,
  149. int writeAddr,
  150. int writeNb,
  151. uint8_t* src,
  152. int readAddr,
  153. int readNb) {
  154. agile_modbus_t* ctx = obj_getPtr(self, "ctx");
  155. return agile_modbus_serialize_write_and_read_registers(
  156. ctx, writeAddr, writeNb, (uint16_t*)src, readAddr, readNb);
  157. }
  158. int _modbus__ModBus_serializeWriteBit(PikaObj* self, int addr, int status) {
  159. agile_modbus_t* ctx = obj_getPtr(self, "ctx");
  160. return agile_modbus_serialize_write_bit(ctx, addr, status);
  161. }
  162. int _modbus__ModBus_serializeWriteBits(PikaObj* self,
  163. int addr,
  164. int nb,
  165. uint8_t* src) {
  166. agile_modbus_t* ctx = obj_getPtr(self, "ctx");
  167. return agile_modbus_serialize_write_bits(ctx, addr, nb, src);
  168. }
  169. int _modbus__ModBus_serializeWriteRegister(PikaObj* self, int addr, int value) {
  170. agile_modbus_t* ctx = obj_getPtr(self, "ctx");
  171. return agile_modbus_serialize_write_register(ctx, addr, value);
  172. }
  173. int _modbus__ModBus_serializeWriteRegisters(PikaObj* self,
  174. int addr,
  175. int nb,
  176. uint8_t* src) {
  177. agile_modbus_t* ctx = obj_getPtr(self, "ctx");
  178. return agile_modbus_serialize_write_registers(ctx, addr, nb,
  179. (uint16_t*)src);
  180. }
  181. Arg* _modbus__ModBus_getSendBuff(PikaObj* self) {
  182. agile_modbus_t* ctx = obj_getPtr(self, "ctx");
  183. return arg_newBytes(ctx->send_buf, ctx->send_bufsz);
  184. }
  185. Arg* _modbus__ModBus_getReadBuff(PikaObj* self) {
  186. agile_modbus_t* ctx = obj_getPtr(self, "ctx");
  187. return arg_newBytes(ctx->read_buf, ctx->read_bufsz);
  188. }