Чт июл 05, 2012 21:34:50
По freemodbus есть справка, правда я не могу в ней разобраться
Чт июл 05, 2012 21:44:53
Чт июл 05, 2012 23:09:59
//----------
//------- Эта секция программы эмулирует массив регистров для Modbus: --------
//----------
//---------- Эмуляция массива COILS на запись: ----------
bool ForceCoil(unsigned int addr, unsigned int word)
{
if(word == 0xFF00)
return(Regs_SetCoil(addr, 1));
if(word == 0x0000)
return(Regs_SetCoil(addr, 0));
return(0);
}
//---------- Эмуляция массива COILS на чтение: ----------
bool ReadCoil(unsigned int addr, bool *bit)
{
return(Regs_GetCoil(addr, bit)); //чтение реле
}
//---------- Эмуляция массива INPUTS на чтение: ----------
bool ReadInput(unsigned int addr, bool *bit)
{
return(Regs_GetInput(addr, bit)); //нет INPUTS
}
//---------- Эмуляция массива INPUT REGISTERS на чтение: ----------
bool ReadIReg(unsigned int addr, unsigned int *word)
{
if(addr < 4)
{
*word = Adc_Get(addr); //чтение ADC1..ADC4
return(1);
}
switch(addr)
{
case 5: *word = Regs_Get(); break; //чтение SW
case 6: *word = Step_GetIc(); break; //Ic
case 7: *word = Step_GetAc(); break; //Ac
case 8: *word = Step_GetVc(); break; //Vc
case 9: *word = Control_GetLims(); break; //чтение LIM
case 10: *word = Control_GetCtrs(); break; //чтение CONTROLS
case 11: *word = Control_GetStat(); break; //состояние
default: return(0);
}
return(1);
}
//---------- Эмуляция массива HOLDING REGISTERS на чтение: ----------
bool ReadHReg(unsigned int addr, unsigned int *word)
{
switch(addr)
{
case 1: *word = Step_GetFM(); break; //FM
case 2: *word = Step_GetIh(); break; //Ih
case 3: *word = Step_GetIa(); break; //Ia
case 4: *word = Step_GetIw(); break; //Iw
case 5: *word = Step_GetAw(); break; //Aw
case 6: *word = Step_GetVm(); break; //Vm
case 7: *word = Step_GetVw(); break; //Vw
default: return(0);
}
return(1);
}
//---------- Эмуляция массива HOLDING REGISTERS на запись: ----------
bool PresetHReg(unsigned int addr, unsigned int word)
{
switch(addr)
{
case 1: Step_SetFM(word); break; //FM
case 2: Step_SetIh(word); break; //Ih
case 3: Step_SetIa(word); break; //Ia
case 4: Step_SetIw(word); break; //Iw
case 5: Step_SetAw(word); break; //Aw
case 6: Step_SetVm(word); break; //Vm
case 7: Step_SetVw(word); break; //Vw
default: return(0);
}
return(1);
}
//----------
Пн сен 27, 2021 15:03:19
/* ---------- AVR includes ----------*/
#include "avr/io.h"
#include "avr/interrupt.h"
/* ---------- Modbus includes ----------*/
#include "mb.h"
#include "mbport.h"
/* ---------- Defines ----------*/
#define REG_INPUT_START 1000
#define REG_INPUT_NREGS 4
/* ---------- Static variables ----------*/
static USHORT usRegInputStart = REG_INPUT_START;
static USHORT usRegInputBuf[REG_INPUT_NREGS];
#define REG_HOLDING_START 1200
#define REG_HOLDING_NREGS 99
static USHORT usRegHoldingStart = REG_HOLDING_START;
static USHORT usRegHoldingBuf[REG_HOLDING_NREGS];
#define REG_COILS_START 1100
#define REG_COILS_SIZE 16
static unsigned char ucRegCoilsBuf[REG_COILS_SIZE / 8];
/* ---------- Start implementation ----------*/
int
main( void )
{
const UCHAR ucSlaveID[] = { 0xAA, 0xBB, 0xCC };
eMBErrorCode eStatus;
eStatus = eMBInit( MB_RTU, 0x0A, 0, 38400, MB_PAR_EVEN );
eStatus = eMBSetSlaveID( 0x34, TRUE, ucSlaveID, 3 );
sei( );
/* Enable the Modbus Protocol Stack. */
eStatus = eMBEnable( );
for( ;; )
{
( void )eMBPoll( );
/* Here we simply count the number of poll cycles. */
usRegInputBuf[0]++;
}
}
eMBErrorCode
eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs )
{
eMBErrorCode eStatus = MB_ENOERR;
int iRegIndex;
if( ( usAddress >= REG_INPUT_START )
&& ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) )
{
iRegIndex = ( int )( usAddress - usRegInputStart );
while( usNRegs > 0 )
{
*pucRegBuffer++ =
( unsigned char )( usRegInputBuf[iRegIndex] >> 8 );
*pucRegBuffer++ =
( unsigned char )( usRegInputBuf[iRegIndex] & 0xFF );
iRegIndex++;
usNRegs--;
}
}
else
{
eStatus = MB_ENOREG;
}
return eStatus;
}
eMBErrorCode
eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs,
eMBRegisterMode eMode )
{
eMBErrorCode eStatus = MB_ENOERR;
int iRegIndex;
if( ( usAddress >= REG_HOLDING_START ) &&
( usAddress + usNRegs <= REG_HOLDING_START + REG_HOLDING_NREGS ) )
{
iRegIndex = ( int )( usAddress - usRegHoldingStart );
switch ( eMode )
{
/* Pass current register values to the protocol stack. */
case MB_REG_READ:
while( usNRegs > 0 )
{
*pucRegBuffer++ = ( UCHAR ) ( usRegHoldingBuf[iRegIndex] >> 8 );
*pucRegBuffer++ = ( UCHAR ) ( usRegHoldingBuf[iRegIndex] & 0xFF );
iRegIndex++;
usNRegs--;
}
break;
/* Update current register values with new values from the
* protocol stack. */
case MB_REG_WRITE:
while( usNRegs > 0 )
{
usRegHoldingBuf[iRegIndex] = *pucRegBuffer++ << 8;
usRegHoldingBuf[iRegIndex] |= *pucRegBuffer++;
iRegIndex++;
usNRegs--;
}
}
}
else
{
eStatus = MB_ENOREG;
}
return eStatus;
}
eMBErrorCode
eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils,
eMBRegisterMode eMode )
{
return MB_ENOREG;
}
eMBErrorCode
eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete )
{
return MB_ENOREG;
}
Вт сен 28, 2021 10:29:51