123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- #include "plt.h"
- #include "mbs.h"
- enum mbs_idx_t
- {
- MBSIDX_MDL = 0,
- MBSIDX_EMA = 1,
- };
- fmodbus_t *MB[4];
- /* ----------------------- Defines ------------------------------------------*/
- USHORT usHoldingRegisters[REG_HOLDING_NREGS] = {0};
- USHORT usInputRegisters[REG_INPUT_NREGS] = {0};
- static enum ThreadState {
- STOPPED,
- RUNNING,
- SHUTDOWN
- } ePollThreadState;
- static pthread_mutex_t xLock = PTHREAD_MUTEX_INITIALIZER;
- // static BOOL bDoExit;
- void *pvPollingThread(void *pvParameter)
- {
- fmodbus_t *ctx = (fmodbus_t *)pvParameter;
- log_dbg("%s, ++, mbsidx:%d", __func__, ctx->mbsidx);
- if (eMBEnable(ctx) == MB_ENOERR)
- {
- do
- {
- if (eMBPoll(ctx) != MB_ENOERR)
- break;
- } while (TRUE);
- }
- (void)eMBDisable(ctx);
- log_dbg("%s, --, mbsidx:%d", __func__, ctx->mbsidx);
- return NULL;
- }
- // 04
- static void on_04_ReadInputRegisters(fmodbus_t *ctx)
- {
- inner_states_to_modbus();
- }
- // 03
- static void on_03_ReadMultipleHoldingRegisters(fmodbus_t *ctx)
- {
- inner_states_to_modbus();
- }
- // 06
- static void on_06_WriteSingleHoldingRegister(fmodbus_t *ctx)
- {
- holding_reg_to_inner_states_on_write();
- }
- eMBErrorCode eMBRegInputCB(fmodbus_t *ctx, UCHAR *pucRegBuffer, USHORT usAddress, USHORT usNRegs)
- {
- eMBErrorCode eStatus = MB_ENOERR;
- int iRegIndex;
- if (ctx->mbsidx == MBSIDX_MDL)
- {
- if ((usAddress >= REG_INPUT_START) && (usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS))
- {
- // iRegIndex = (int)(usAddress - usRegInputStart_PCS[chan->mbsidx]);
- on_04_ReadInputRegisters(ctx);
- iRegIndex = (int)(usAddress - REG_INPUT_START);
- while (usNRegs > 0)
- {
- *pucRegBuffer++ = (UCHAR)(usInputRegisters[iRegIndex] >> 8);
- *pucRegBuffer++ = (UCHAR)(usInputRegisters[iRegIndex] & 0xFF);
- iRegIndex++;
- usNRegs--;
- }
- }
- else
- {
- eStatus = MB_ENOREG;
- }
- return eStatus;
- }
- return MB_ENOREG;
- }
- eMBErrorCode eMBRegHoldingCB(fmodbus_t *ctx, UCHAR *pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegisterMode eMode)
- {
- eMBErrorCode eStatus = MB_ENOERR;
- int iRegIndex;
- int i = 0;
- if (ctx->mbsidx == MBSIDX_MDL)
- { // ctn modbus tcp slave
- if ((usAddress >= REG_HOLDING_START) && (usAddress + usNRegs <= REG_HOLDING_START + REG_HOLDING_NREGS))
- {
- iRegIndex = (int)(usAddress - REG_HOLDING_START);
- switch (eMode)
- {
- /* Pass current register values to the protocol stack. */
- case MB_REG_READ:
- on_03_ReadMultipleHoldingRegisters(ctx);
- // log_dbg("%s, MB_REG_READ, %d, %d ", __func__, usAddress, usNRegs);
- while (usNRegs > 0)
- {
- *pucRegBuffer++ = (UCHAR)(usHoldingRegisters[iRegIndex] >> 8);
- *pucRegBuffer++ = (UCHAR)(usHoldingRegisters[iRegIndex] & 0xFF);
- iRegIndex++;
- usNRegs--;
- }
- break;
- /* Update current register values with new values from the
- * protocol stack. */
- case MB_REG_WRITE:
- log_dbg("%s, MB_REG_WRITE, %d, %d ", __func__, usAddress, usNRegs);
- while (usNRegs > 0)
- {
- usHoldingRegisters[iRegIndex] = *pucRegBuffer++ << 8;
- usHoldingRegisters[iRegIndex] |= *pucRegBuffer++;
- iRegIndex++;
- usNRegs--;
- }
- on_06_WriteSingleHoldingRegister(ctx);
- break;
- }
- }
- else
- {
- eStatus = MB_ENOREG;
- }
- return eStatus;
- }
- }
- eMBErrorCode eMBRegCoilsCB(fmodbus_t *ctx, UCHAR *pucRegBuffer, USHORT usAddress, USHORT usNCoils, eMBRegisterMode eMode)
- {
- return MB_ENOREG;
- }
- eMBErrorCode eMBRegDiscreteCB(fmodbus_t *ctx, UCHAR *pucRegBuffer, USHORT usAddress, USHORT usNDiscrete)
- {
- return MB_ENOREG;
- }
- int mbs_start_MDL()
- {
- int ret = 0;
- const UCHAR ucSlaveID[] = {0xAA, 0xBB, 0xCC};
- pthread_t xthrd;
- if (eMBInit(&MB[MBSIDX_MDL], MB_RTU, MDL.adr, MDL.szSerial, 9600, MB_PAR_NONE) != MB_ENOERR)
- {
- log_dbg("%s, eMBTCPInit fail", __func__);
- ret = -1;
- }
- else if (eMBSetSlaveID(MB[MBSIDX_MDL], 0x34, TRUE, ucSlaveID, 3) != MB_ENOERR)
- {
- log_dbg("%s, eMBSetSlaveID fail", __func__);
- ret = -2;
- }
- else
- {
- MB[MBSIDX_MDL]->mbsidx = MBSIDX_MDL;
- if (pthread_create(&xthrd, NULL, pvPollingThread, MB[MBSIDX_MDL]) != 0)
- {
- log_dbg("%s, pthread_create fail", __func__);
- ret = -3;
- }
- else
- {
- log_dbg("%s, start ok", __func__);
- }
- }
- return ret;
- }
|