debug tty

This commit is contained in:
wmano
2025-09-14 23:17:13 +08:00
parent 349741d9e1
commit ea8dcada90
22 changed files with 982 additions and 1156 deletions
-266
View File
@@ -1,266 +0,0 @@
#include <rtthread.h>
#include "mb.h"
#include "mbutils.h"
#include "chrg_mb.h"
//Slave mode:DiscreteInputs variables
rt_uint16_t usSDiscInStart = S_DISCRETE_INPUT_START;
rt_uint8_t ucSDiscInBuf[(S_DISCRETE_INPUT_NDISCRETES+7)/8];
//Slave mode:Coils variables
rt_uint16_t usSCoilStart = S_COIL_START;
rt_uint8_t ucSCoilBuf[(S_COIL_NCOILS+7)/8];
//Slave mode:InputRegister variables
rt_uint16_t usSRegInStart = S_REG_INPUT_START;
rt_uint16_t usSRegInBuf[S_REG_INPUT_NREGS];
//Slave mode:HoldingRegister variables
rt_uint16_t usSRegHoldStart = S_REG_HOLDING_START;
rt_uint16_t usSRegHoldBuf[S_REG_HOLDING_NREGS];
/**
* Modbus slave input register callback function.
*
* @param pucRegBuffer input register buffer
* @param usAddress input register address
* @param usNRegs input register number
*
* @return result
*/
eMBErrorCode eMBRegInputCB(rt_uint8_t * pucRegBuffer, rt_uint16_t usAddress, rt_uint16_t usNRegs )
{
eMBErrorCode eStatus = MB_ENOERR;
rt_uint16_t iRegIndex;
rt_uint16_t * pusRegInputBuf;
rt_uint16_t REG_INPUT_START;
rt_uint16_t REG_INPUT_NREGS;
rt_uint16_t usRegInStart;
pusRegInputBuf = usSRegInBuf;
REG_INPUT_START = S_REG_INPUT_START;
REG_INPUT_NREGS = S_REG_INPUT_NREGS;
usRegInStart = usSRegInStart;
/* it already plus one in modbus function method. */
usAddress--;
if ((usAddress >= REG_INPUT_START)
&& (usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS))
{
iRegIndex = usAddress - usRegInStart;
while (usNRegs > 0)
{
*pucRegBuffer++ = (rt_uint8_t) (pusRegInputBuf[iRegIndex] >> 8);
*pucRegBuffer++ = (rt_uint8_t) (pusRegInputBuf[iRegIndex] & 0xFF);
iRegIndex++;
usNRegs--;
}
}
else
{
eStatus = MB_ENOREG;
}
return eStatus;
}
/**
* Modbus slave holding register callback function.
*
* @param pucRegBuffer holding register buffer
* @param usAddress holding register address
* @param usNRegs holding register number
* @param eMode read or write
*
* @return result
*/
eMBErrorCode eMBRegHoldingCB(rt_uint8_t * pucRegBuffer, rt_uint16_t usAddress,
rt_uint16_t usNRegs, eMBRegisterMode eMode)
{
eMBErrorCode eStatus = MB_ENOERR;
rt_uint16_t iRegIndex;
rt_uint16_t * pusRegHoldingBuf;
rt_uint16_t REG_HOLDING_START;
rt_uint16_t REG_HOLDING_NREGS;
rt_uint16_t usRegHoldStart;
pusRegHoldingBuf = usSRegHoldBuf;
REG_HOLDING_START = S_REG_HOLDING_START;
REG_HOLDING_NREGS = S_REG_HOLDING_NREGS;
usRegHoldStart = usSRegHoldStart;
/* it already plus one in modbus function method. */
usAddress--;
if ((usAddress >= REG_HOLDING_START)
&& (usAddress + usNRegs <= REG_HOLDING_START + REG_HOLDING_NREGS))
{
iRegIndex = usAddress - usRegHoldStart;
switch (eMode)
{
/* read current register values from the protocol stack. */
case MB_REG_READ:
while (usNRegs > 0)
{
*pucRegBuffer++ = (rt_uint8_t) (pusRegHoldingBuf[iRegIndex] >> 8);
*pucRegBuffer++ = (rt_uint8_t) (pusRegHoldingBuf[iRegIndex] & 0xFF);
iRegIndex++;
usNRegs--;
}
break;
/* write current register values with new values from the protocol stack. */
case MB_REG_WRITE:
while (usNRegs > 0)
{
pusRegHoldingBuf[iRegIndex] = *pucRegBuffer++ << 8;
pusRegHoldingBuf[iRegIndex] |= *pucRegBuffer++;
iRegIndex++;
usNRegs--;
}
break;
}
}
else
{
eStatus = MB_ENOREG;
}
return eStatus;
}
/**
* Modbus slave coils callback function.
*
* @param pucRegBuffer coils buffer
* @param usAddress coils address
* @param usNCoils coils number
* @param eMode read or write
*
* @return result
*/
eMBErrorCode eMBRegCoilsCB(rt_uint8_t * pucRegBuffer, rt_uint16_t usAddress,
rt_uint16_t usNCoils, eMBRegisterMode eMode)
{
eMBErrorCode eStatus = MB_ENOERR;
rt_uint16_t iRegIndex , iRegBitIndex , iNReg;
rt_uint8_t * pucCoilBuf;
rt_uint16_t COIL_START;
rt_uint16_t COIL_NCOILS;
rt_uint16_t usCoilStart;
iNReg = usNCoils / 8 + 1;
pucCoilBuf = ucSCoilBuf;
COIL_START = S_COIL_START;
COIL_NCOILS = S_COIL_NCOILS;
usCoilStart = usSCoilStart;
/* it already plus one in modbus function method. */
usAddress--;
if( ( usAddress >= COIL_START ) &&
( usAddress + usNCoils <= COIL_START + COIL_NCOILS ) )
{
iRegIndex = (rt_uint16_t) (usAddress - usCoilStart) / 8;
iRegBitIndex = (rt_uint16_t) (usAddress - usCoilStart) % 8;
switch ( eMode )
{
/* read current coil values from the protocol stack. */
case MB_REG_READ:
while (iNReg > 0)
{
*pucRegBuffer++ = xMBUtilGetBits(&pucCoilBuf[iRegIndex++],
iRegBitIndex, 8);
iNReg--;
}
pucRegBuffer--;
/* last coils */
usNCoils = usNCoils % 8;
/* filling zero to high bit */
*pucRegBuffer = *pucRegBuffer << (8 - usNCoils);
*pucRegBuffer = *pucRegBuffer >> (8 - usNCoils);
break;
/* write current coil values with new values from the protocol stack. */
case MB_REG_WRITE:
while (iNReg > 1)
{
xMBUtilSetBits(&pucCoilBuf[iRegIndex++], iRegBitIndex, 8,
*pucRegBuffer++);
iNReg--;
}
/* last coils */
usNCoils = usNCoils % 8;
/* xMBUtilSetBits has bug when ucNBits is zero */
if (usNCoils != 0)
{
xMBUtilSetBits(&pucCoilBuf[iRegIndex++], iRegBitIndex, usNCoils,
*pucRegBuffer++);
}
break;
}
}
else
{
eStatus = MB_ENOREG;
}
return eStatus;
}
/**
* Modbus slave discrete callback function.
*
* @param pucRegBuffer discrete buffer
* @param usAddress discrete address
* @param usNDiscrete discrete number
*
* @return result
*/
eMBErrorCode eMBRegDiscreteCB( rt_uint8_t * pucRegBuffer, rt_uint16_t usAddress, rt_uint16_t usNDiscrete )
{
eMBErrorCode eStatus = MB_ENOERR;
rt_uint16_t iRegIndex , iRegBitIndex , iNReg;
rt_uint8_t * pucDiscreteInputBuf;
rt_uint16_t DISCRETE_INPUT_START;
rt_uint16_t DISCRETE_INPUT_NDISCRETES;
rt_uint16_t usDiscreteInputStart;
iNReg = usNDiscrete / 8 + 1;
pucDiscreteInputBuf = ucSDiscInBuf;
DISCRETE_INPUT_START = S_DISCRETE_INPUT_START;
DISCRETE_INPUT_NDISCRETES = S_DISCRETE_INPUT_NDISCRETES;
usDiscreteInputStart = usSDiscInStart;
/* it already plus one in modbus function method. */
usAddress--;
if ((usAddress >= DISCRETE_INPUT_START)
&& (usAddress + usNDiscrete <= DISCRETE_INPUT_START + DISCRETE_INPUT_NDISCRETES))
{
iRegIndex = (rt_uint16_t) (usAddress - usDiscreteInputStart) / 8;
iRegBitIndex = (rt_uint16_t) (usAddress - usDiscreteInputStart) % 8;
while (iNReg > 0)
{
*pucRegBuffer++ = xMBUtilGetBits(&pucDiscreteInputBuf[iRegIndex++],
iRegBitIndex, 8);
iNReg--;
}
pucRegBuffer--;
/* last discrete */
usNDiscrete = usNDiscrete % 8;
/* filling zero to high bit */
*pucRegBuffer = *pucRegBuffer << (8 - usNDiscrete);
*pucRegBuffer = *pucRegBuffer >> (8 - usNDiscrete);
}
else
{
eStatus = MB_ENOREG;
}
return eStatus;
}
-26
View File
@@ -1,26 +0,0 @@
#ifndef __CHRG_MB_H__
#define __CHRG_MB_H__
#define S_DISCRETE_INPUT_START 0
#define S_DISCRETE_INPUT_NDISCRETES 16
#define S_COIL_START 0
#define S_COIL_NCOILS 64
#define S_REG_INPUT_START 0
#define S_REG_INPUT_NREGS 100
#define S_REG_HOLDING_START 0
#define S_REG_HOLDING_NREGS 100
/* salve mode: holding register's all address */
#define S_HD_RESERVE 0
/* salve mode: input register's all address */
#define S_IN_RESERVE 0
/* salve mode: coil's all address */
#define S_CO_RESERVE 0
/* salve mode: discrete's all address */
#define S_DI_RESERVE 0
#endif