mbs.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #include "plt.h"
  2. #include "mbs.h"
  3. enum mbs_idx_t
  4. {
  5. MBSIDX_MDL = 0,
  6. MBSIDX_EMA = 1,
  7. };
  8. fmodbus_t *MB[4];
  9. /* ----------------------- Defines ------------------------------------------*/
  10. USHORT usHoldingRegisters[REG_HOLDING_NREGS] = {0};
  11. USHORT usInputRegisters[REG_INPUT_NREGS] = {0};
  12. static enum ThreadState {
  13. STOPPED,
  14. RUNNING,
  15. SHUTDOWN
  16. } ePollThreadState;
  17. static pthread_mutex_t xLock = PTHREAD_MUTEX_INITIALIZER;
  18. // static BOOL bDoExit;
  19. void *pvPollingThread(void *pvParameter)
  20. {
  21. fmodbus_t *ctx = (fmodbus_t *)pvParameter;
  22. log_dbg("%s, ++, mbsidx:%d", __func__, ctx->mbsidx);
  23. if (eMBEnable(ctx) == MB_ENOERR)
  24. {
  25. do
  26. {
  27. if (eMBPoll(ctx) != MB_ENOERR)
  28. break;
  29. } while (TRUE);
  30. }
  31. (void)eMBDisable(ctx);
  32. log_dbg("%s, --, mbsidx:%d", __func__, ctx->mbsidx);
  33. return NULL;
  34. }
  35. // 04
  36. static void on_04_ReadInputRegisters(fmodbus_t *ctx)
  37. {
  38. inner_states_to_modbus();
  39. }
  40. // 03
  41. static void on_03_ReadMultipleHoldingRegisters(fmodbus_t *ctx)
  42. {
  43. inner_states_to_modbus();
  44. }
  45. // 06
  46. static void on_06_WriteSingleHoldingRegister(fmodbus_t *ctx)
  47. {
  48. holding_reg_to_inner_states_on_write();
  49. }
  50. eMBErrorCode eMBRegInputCB(fmodbus_t *ctx, UCHAR *pucRegBuffer, USHORT usAddress, USHORT usNRegs)
  51. {
  52. eMBErrorCode eStatus = MB_ENOERR;
  53. int iRegIndex;
  54. if (ctx->mbsidx == MBSIDX_MDL)
  55. {
  56. if ((usAddress >= REG_INPUT_START) && (usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS))
  57. {
  58. // iRegIndex = (int)(usAddress - usRegInputStart_PCS[chan->mbsidx]);
  59. on_04_ReadInputRegisters(ctx);
  60. iRegIndex = (int)(usAddress - REG_INPUT_START);
  61. while (usNRegs > 0)
  62. {
  63. *pucRegBuffer++ = (UCHAR)(usInputRegisters[iRegIndex] >> 8);
  64. *pucRegBuffer++ = (UCHAR)(usInputRegisters[iRegIndex] & 0xFF);
  65. iRegIndex++;
  66. usNRegs--;
  67. }
  68. }
  69. else
  70. {
  71. eStatus = MB_ENOREG;
  72. }
  73. return eStatus;
  74. }
  75. return MB_ENOREG;
  76. }
  77. eMBErrorCode eMBRegHoldingCB(fmodbus_t *ctx, UCHAR *pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegisterMode eMode)
  78. {
  79. eMBErrorCode eStatus = MB_ENOERR;
  80. int iRegIndex;
  81. int i = 0;
  82. if (ctx->mbsidx == MBSIDX_MDL)
  83. { // ctn modbus tcp slave
  84. if ((usAddress >= REG_HOLDING_START) && (usAddress + usNRegs <= REG_HOLDING_START + REG_HOLDING_NREGS))
  85. {
  86. iRegIndex = (int)(usAddress - REG_HOLDING_START);
  87. switch (eMode)
  88. {
  89. /* Pass current register values to the protocol stack. */
  90. case MB_REG_READ:
  91. on_03_ReadMultipleHoldingRegisters(ctx);
  92. // log_dbg("%s, MB_REG_READ, %d, %d ", __func__, usAddress, usNRegs);
  93. while (usNRegs > 0)
  94. {
  95. *pucRegBuffer++ = (UCHAR)(usHoldingRegisters[iRegIndex] >> 8);
  96. *pucRegBuffer++ = (UCHAR)(usHoldingRegisters[iRegIndex] & 0xFF);
  97. iRegIndex++;
  98. usNRegs--;
  99. }
  100. break;
  101. /* Update current register values with new values from the
  102. * protocol stack. */
  103. case MB_REG_WRITE:
  104. log_dbg("%s, MB_REG_WRITE, %d, %d ", __func__, usAddress, usNRegs);
  105. while (usNRegs > 0)
  106. {
  107. usHoldingRegisters[iRegIndex] = *pucRegBuffer++ << 8;
  108. usHoldingRegisters[iRegIndex] |= *pucRegBuffer++;
  109. iRegIndex++;
  110. usNRegs--;
  111. }
  112. on_06_WriteSingleHoldingRegister(ctx);
  113. break;
  114. }
  115. }
  116. else
  117. {
  118. eStatus = MB_ENOREG;
  119. }
  120. return eStatus;
  121. }
  122. }
  123. eMBErrorCode eMBRegCoilsCB(fmodbus_t *ctx, UCHAR *pucRegBuffer, USHORT usAddress, USHORT usNCoils, eMBRegisterMode eMode)
  124. {
  125. return MB_ENOREG;
  126. }
  127. eMBErrorCode eMBRegDiscreteCB(fmodbus_t *ctx, UCHAR *pucRegBuffer, USHORT usAddress, USHORT usNDiscrete)
  128. {
  129. return MB_ENOREG;
  130. }
  131. int mbs_start_MDL()
  132. {
  133. int ret = 0;
  134. const UCHAR ucSlaveID[] = {0xAA, 0xBB, 0xCC};
  135. pthread_t xthrd;
  136. if (eMBInit(&MB[MBSIDX_MDL], MB_RTU, MDL.adr, MDL.szSerial, 9600, MB_PAR_NONE) != MB_ENOERR)
  137. {
  138. log_dbg("%s, eMBTCPInit fail", __func__);
  139. ret = -1;
  140. }
  141. else if (eMBSetSlaveID(MB[MBSIDX_MDL], 0x34, TRUE, ucSlaveID, 3) != MB_ENOERR)
  142. {
  143. log_dbg("%s, eMBSetSlaveID fail", __func__);
  144. ret = -2;
  145. }
  146. else
  147. {
  148. MB[MBSIDX_MDL]->mbsidx = MBSIDX_MDL;
  149. if (pthread_create(&xthrd, NULL, pvPollingThread, MB[MBSIDX_MDL]) != 0)
  150. {
  151. log_dbg("%s, pthread_create fail", __func__);
  152. ret = -3;
  153. }
  154. else
  155. {
  156. log_dbg("%s, start ok", __func__);
  157. }
  158. }
  159. return ret;
  160. }