@ -12,71 +12,144 @@ ModbusException_t startOfAccess(ModbusSlave_t *ms, ModbusFunction_t fcx, uint8_t
void endOfAccess ( ModbusSlave_t * ms ) { printf ( " End of access \n " ) ; }
void endOfAccess ( ModbusSlave_t * ms ) { printf ( " End of access \n " ) ; }
typedef struct {
uint16_t num ;
uint16_t value ;
} register_slot_t ;
typedef struct {
uint16_t num ;
bool value ;
} coil_slot_t ;
//@formatter:off
static register_slot_t holdings [ ] = {
{ 1 , 10 } ,
{ 2 , 20 } ,
{ 3 , 30 } ,
{ 4 , 40 } ,
{ 5 , 50 } ,
{ 7 , 70 } ,
{ 0xffff , 0 }
} ;
static register_slot_t inputs [ ] = {
{ 1 , 11 } ,
{ 2 , 21 } ,
{ 3 , 31 } ,
{ 4 , 41 } ,
{ 5 , 51 } ,
{ 7 , 71 } ,
{ 0xffff , 0 }
} ;
static coil_slot_t coils [ ] = {
{ 1 , 1 } ,
{ 2 , 0 } ,
{ 3 , 1 } ,
{ 4 , 0 } ,
{ 5 , 1 } ,
{ 7 , 1 } ,
{ 0xffff , 0 }
} ;
static coil_slot_t discretes [ ] = {
{ 1 , 0 } ,
{ 2 , 1 } ,
{ 3 , 1 } ,
{ 4 , 1 } ,
{ 5 , 1 } ,
{ 7 , 0 } ,
{ 0xffff , 0 }
} ;
//@formatter:on
ModbusException_t readHolding ( ModbusSlave_t * ms , uint16_t i , uint16_t * out ) {
ModbusException_t readHolding ( ModbusSlave_t * ms , uint16_t i , uint16_t * out ) {
printf ( " rh %d \n " , i ) ;
printf ( " rh %d \n " , i ) ;
switch ( i ) {
case 107 :
const register_slot_t * p = holdings ;
* out = 0xAE41 ;
while ( p - > num ! = 0xffff ) {
break ;
if ( p - > num = = i ) {
case 108 :
* out = p - > value ;
* out = 0x5652 ;
break ;
case 109 :
* out = 0x4340 ;
break ;
default :
return MB_EXCEPTION_ILLEGAL_DATA_ADDRESS ;
}
return 0 ;
return 0 ;
}
}
ModbusException_t readInput ( ModbusSlave_t * ms , uint16_t i , uint16_t * out ) {
p + + ;
printf ( " ri %d \n " , i ) ;
}
switch ( i ) {
case 8 :
* out = 0xa ;
break ;
default :
return MB_EXCEPTION_ILLEGAL_DATA_ADDRESS ;
return MB_EXCEPTION_ILLEGAL_DATA_ADDRESS ;
}
}
ModbusException_t readInput ( ModbusSlave_t * ms , uint16_t i , uint16_t * out ) {
printf ( " ri %d \n " , i ) ;
const register_slot_t * p = inputs ;
while ( p - > num ! = 0xffff ) {
if ( p - > num = = i ) {
* out = p - > value ;
return 0 ;
return 0 ;
}
}
ModbusException_t readCoil ( ModbusSlave_t * ms , uint16_t i , bool * out ) {
p + + ;
uint8_t store [ ] = {
}
0 b11001101 , 0 b01101011 , 0 b10110010 , 0 b00001110 , 0 b00011011 ,
} ;
if ( i > = 19 & & i < = 55 ) {
int pos = i - 19 ;
* out = 0 ! = ( store [ pos / 8 ] & ( 1 < < ( pos % 8 ) ) ) ;
} else {
return MB_EXCEPTION_ILLEGAL_DATA_ADDRESS ;
return MB_EXCEPTION_ILLEGAL_DATA_ADDRESS ;
}
}
ModbusException_t readCoil ( ModbusSlave_t * ms , uint16_t i , bool * out ) {
const coil_slot_t * p = coils ;
while ( p - > num ! = 0xffff ) {
if ( p - > num = = i ) {
* out = p - > value ;
return 0 ;
return 0 ;
}
}
ModbusException_t readDiscrete ( ModbusSlave_t * ms , uint16_t i , bool * out ) {
p + + ;
uint8_t store [ ] = {
}
0 b10101100 ,
0 b11011011 ,
0 b00110101 ,
} ;
if ( i > = 196 & & i < = 217 ) {
int pos = i - 196 ;
* out = 0 ! = ( store [ pos / 8 ] & ( 1 < < ( pos % 8 ) ) ) ;
} else {
return MB_EXCEPTION_ILLEGAL_DATA_ADDRESS ;
return MB_EXCEPTION_ILLEGAL_DATA_ADDRESS ;
}
}
ModbusException_t readDiscrete ( ModbusSlave_t * ms , uint16_t i , bool * out ) {
const coil_slot_t * p = discretes ;
while ( p - > num ! = 0xffff ) {
if ( p - > num = = i ) {
* out = p - > value ;
return 0 ;
return 0 ;
}
}
p + + ;
}
return MB_EXCEPTION_ILLEGAL_DATA_ADDRESS ;
}
ModbusException_t writeCoil ( ModbusSlave_t * pSlave , uint16_t i , bool b ) {
ModbusException_t writeCoil ( ModbusSlave_t * pSlave , uint16_t i , bool b ) {
printf ( " Write coil %d <- %d \n " , i , b ) ;
printf ( " Write coil %d <- %d \n " , i , b ) ;
coil_slot_t * p = coils ;
while ( p - > num ! = 0 ) {
if ( p - > num = = i ) {
p - > value = b ;
return 0 ;
return 0 ;
}
}
ModbusException_t writeHolding ( ModbusSlave_t * pSlave , uint16_t i , uint16_t i1 ) {
p + + ;
printf ( " Write reg %d <- %d \n " , i , i1 ) ;
}
return MB_EXCEPTION_ILLEGAL_DATA_ADDRESS ;
}
ModbusException_t writeHolding ( ModbusSlave_t * pSlave , uint16_t i , uint16_t val ) {
printf ( " Write reg %d <- %d \n " , i , val ) ;
register_slot_t * p = holdings ;
while ( p - > num ! = 0 ) {
if ( p - > num = = i ) {
p - > value = val ;
return 0 ;
return 0 ;
}
}
p + + ;
}
return MB_EXCEPTION_ILLEGAL_DATA_ADDRESS ;
}
static ModbusSlave_t ms = {
static ModbusSlave_t ms = {
. addr = 1 ,
. addr = 1 ,