Lydia - Printhead
Modbus Function Codes for Discrete Coils/Inputs

Functions

int8_t Modbus::process_FC1 (uint16_t *regs, uint8_t u8size)
 This method processes functions 1 & 2 This method reads a bit array and transfers it to the master. More...
 
int8_t Modbus::process_FC15 (uint16_t *regs, uint8_t u8size)
 This method processes function 15 This method writes a bit array assigned by the master. More...
 
int8_t Modbus::process_FC5 (uint16_t *regs, uint8_t u8size)
 This method processes function 5 This method writes a value assigned by the master to a single bit. More...
 

Detailed Description

Function Documentation

◆ process_FC1()

int8_t Modbus::process_FC1 ( uint16_t *  regs,
uint8_t  u8size 
)
private

This method processes functions 1 & 2 This method reads a bit array and transfers it to the master.

Returns
u8BufferSize Response to master length

Definition at line 1302 of file ModbusRtu.h.

1303 {
1304  uint8_t u8currentRegister, u8currentBit, u8bytesno, u8bitsno;
1305  uint8_t u8CopyBufferSize;
1306  uint16_t u16currentCoil, u16coil;
1307 
1308  // get the first and last coil from the message
1309  uint16_t u16StartCoil = word(au8Buffer[ADD_HI], au8Buffer[ADD_LO]);
1310  uint16_t u16Coilno = word(au8Buffer[NB_HI], au8Buffer[NB_LO]);
1311 
1312  // put the number of bytes in the outcoming message
1313  u8bytesno = (uint8_t)(u16Coilno / 8);
1314  if (u16Coilno % 8 != 0)
1315  u8bytesno++;
1316  au8Buffer[ADD_HI] = u8bytesno;
1317  u8BufferSize = ADD_LO;
1318 
1319  // read each coil from the register map and put its value inside the outcoming message
1320  u8bitsno = 0;
1321 
1322  for (u16currentCoil = 0; u16currentCoil < u16Coilno; u16currentCoil++)
1323  {
1324  u16coil = u16StartCoil + u16currentCoil;
1325  u8currentRegister = (uint8_t)(u16coil / 16);
1326  u8currentBit = (uint8_t)(u16coil % 16);
1327 
1328  bitWrite(
1330  u8bitsno,
1331  bitRead(regs[u8currentRegister], u8currentBit));
1332  u8bitsno++;
1333 
1334  if (u8bitsno > 7)
1335  {
1336  u8bitsno = 0;
1337  u8BufferSize++;
1338  }
1339  }
1340 
1341  // send outcoming message
1342  if (u16Coilno % 8 != 0)
1343  u8BufferSize++;
1344  u8CopyBufferSize = u8BufferSize + 2;
1345  sendTxBuffer();
1346  return u8CopyBufferSize;
1347 }

◆ process_FC15()

int8_t Modbus::process_FC15 ( uint16_t *  regs,
uint8_t  u8size 
)
private

This method processes function 15 This method writes a bit array assigned by the master.

Returns
u8BufferSize Response to master length

Definition at line 1447 of file ModbusRtu.h.

1448 {
1449  uint8_t u8currentRegister, u8currentBit, u8frameByte, u8bitsno;
1450  uint8_t u8CopyBufferSize;
1451  uint16_t u16currentCoil, u16coil;
1452  boolean bTemp;
1453 
1454  // get the first and last coil from the message
1455  uint16_t u16StartCoil = word(au8Buffer[ADD_HI], au8Buffer[ADD_LO]);
1456  uint16_t u16Coilno = word(au8Buffer[NB_HI], au8Buffer[NB_LO]);
1457 
1458  // read each coil from the register map and put its value inside the outcoming message
1459  u8bitsno = 0;
1460  u8frameByte = 7;
1461  for (u16currentCoil = 0; u16currentCoil < u16Coilno; u16currentCoil++)
1462  {
1463 
1464  u16coil = u16StartCoil + u16currentCoil;
1465  u8currentRegister = (uint8_t)(u16coil / 16);
1466  u8currentBit = (uint8_t)(u16coil % 16);
1467 
1468  bTemp = bitRead(
1469  au8Buffer[u8frameByte],
1470  u8bitsno);
1471 
1472  bitWrite(
1473  regs[u8currentRegister],
1474  u8currentBit,
1475  bTemp);
1476 
1477  u8bitsno++;
1478 
1479  if (u8bitsno > 7)
1480  {
1481  u8bitsno = 0;
1482  u8frameByte++;
1483  }
1484  }
1485 
1486  // send outcoming message
1487  // it's just a copy of the incomping frame until 6th byte
1488  u8BufferSize = 6;
1489  u8CopyBufferSize = u8BufferSize + 2;
1490  sendTxBuffer();
1491  return u8CopyBufferSize;
1492 }

◆ process_FC5()

int8_t Modbus::process_FC5 ( uint16_t *  regs,
uint8_t  u8size 
)
private

This method processes function 5 This method writes a value assigned by the master to a single bit.

Returns
u8BufferSize Response to master length

Definition at line 1389 of file ModbusRtu.h.

1390 {
1391  uint8_t u8currentRegister, u8currentBit;
1392  uint8_t u8CopyBufferSize;
1393  uint16_t u16coil = word(au8Buffer[ADD_HI], au8Buffer[ADD_LO]);
1394 
1395  // point to the register and its bit
1396  u8currentRegister = (uint8_t)(u16coil / 16);
1397  u8currentBit = (uint8_t)(u16coil % 16);
1398 
1399  // write to coil
1400  bitWrite(
1401  regs[u8currentRegister],
1402  u8currentBit,
1403  au8Buffer[NB_HI] == 0xff);
1404 
1405  // send answer to master
1406  u8BufferSize = 6;
1407  u8CopyBufferSize = u8BufferSize + 2;
1408  sendTxBuffer();
1409 
1410  return u8CopyBufferSize;
1411 }
Modbus::u8BufferSize
uint8_t u8BufferSize
Definition: ModbusRtu.h:163
NB_HI
@ NB_HI
Number of coils or registers high byte.
Definition: ModbusRtu.h:97
Modbus::sendTxBuffer
void sendTxBuffer()
This method transmits au8Buffer to Serial line. Only if u8txenpin != 0, there is a flow handling in o...
Definition: ModbusRtu.h:962
Modbus::au8Buffer
uint8_t au8Buffer[MAX_BUFFER]
Definition: ModbusRtu.h:162
ADD_HI
@ ADD_HI
Address high byte.
Definition: ModbusRtu.h:95
ADD_LO
@ ADD_LO
Address low byte.
Definition: ModbusRtu.h:96
NB_LO
@ NB_LO
Number of coils or registers low byte.
Definition: ModbusRtu.h:98