Modbus Protocol
ajit.nayak87
Posts: 76
Dear all.
I have the c++ code for Arduino Uno modbus protocol. can someone help me to convert the code c ++ to c.
I have the c++ code for Arduino Uno modbus protocol. can someone help me to convert the code c ++ to c.
void Modbus::get_FC3() { uint8_t u8byte, i; u8byte = 3; for (i = 0; i < au8Buffer[ 2 ] / 2; i++) { au16regs[ i ] = word( au8Buffer[ u8byte ], au8Buffer[ u8byte + 1 ]); u8byte += 2; } } int8_t Modbus::process_FC1( uint16_t *regs, uint8_t u8size ) { uint8_t u8currentRegister, u8currentBit, u8bytesno, u8bitsno; uint8_t u8CopyBufferSize; uint16_t u16currentCoil, u16coil; // get the first and last coil from the message uint16_t u16StartCoil = word( au8Buffer[ ADD_HI ], au8Buffer[ ADD_LO ] ); uint16_t u16Coilno = word( au8Buffer[ NB_HI ], au8Buffer[ NB_LO ] ); // put the number of bytes in the outcoming message u8bytesno = (uint8_t) (u16Coilno / 8); if (u16Coilno % 8 != 0) u8bytesno ++; au8Buffer[ ADD_HI ] = u8bytesno; u8BufferSize = ADD_LO; // read each coil from the register map and put its value inside the outcoming message u8bitsno = 0; for (u16currentCoil = 0; u16currentCoil < u16Coilno; u16currentCoil++) { u16coil = u16StartCoil + u16currentCoil; u8currentRegister = (uint8_t) (u16coil / 16); u8currentBit = (uint8_t) (u16coil % 16); bitWrite( au8Buffer[ u8BufferSize ], u8bitsno, bitRead( regs[ u8currentRegister ], u8currentBit ) ); u8bitsno ++; if (u8bitsno > 7) { u8bitsno = 0; u8BufferSize++; } } // send outcoming message if (u16Coilno % 8 != 0) u8BufferSize ++; u8CopyBufferSize = u8BufferSize +2; sendTxBuffer(); return u8CopyBufferSize; } int8_t Modbus::process_FC3( uint16_t *regs, uint8_t u8size ) { uint8_t u8StartAdd = word( au8Buffer[ ADD_HI ], au8Buffer[ ADD_LO ] ); uint8_t u8regsno = word( au8Buffer[ NB_HI ], au8Buffer[ NB_LO ] ); uint8_t u8CopyBufferSize; uint8_t i; au8Buffer[ 2 ] = u8regsno * 2; u8BufferSize = 3; for (i = u8StartAdd; i < u8StartAdd + u8regsno; i++) { au8Buffer[ u8BufferSize ] = highByte(regs[i]); u8BufferSize++; au8Buffer[ u8BufferSize ] = lowByte(regs[i]); u8BufferSize++; } u8CopyBufferSize = u8BufferSize +2; sendTxBuffer(); return u8CopyBufferSize; } int8_t Modbus::process_FC5( uint16_t *regs, uint8_t u8size ) { uint8_t u8currentRegister, u8currentBit; uint8_t u8CopyBufferSize; uint16_t u16coil = word( au8Buffer[ ADD_HI ], au8Buffer[ ADD_LO ] ); // point to the register and its bit u8currentRegister = (uint8_t) (u16coil / 16); u8currentBit = (uint8_t) (u16coil % 16); // write to coil bitWrite( regs[ u8currentRegister ], u8currentBit, au8Buffer[ NB_HI ] == 0xff ); // send answer to master u8BufferSize = 6; u8CopyBufferSize = u8BufferSize +2; sendTxBuffer(); return u8CopyBufferSize; } int8_t Modbus::process_FC6( uint16_t *regs, uint8_t u8size ) { uint8_t u8add = word( au8Buffer[ ADD_HI ], au8Buffer[ ADD_LO ] ); uint8_t u8CopyBufferSize; uint16_t u16val = word( au8Buffer[ NB_HI ], au8Buffer[ NB_LO ] ); regs[ u8add ] = u16val; // keep the same header u8BufferSize = RESPONSE_SIZE; u8CopyBufferSize = u8BufferSize +2; sendTxBuffer(); return u8CopyBufferSize; } int8_t Modbus::process_FC15( uint16_t *regs, uint8_t u8size ) { uint8_t u8currentRegister, u8currentBit, u8frameByte, u8bitsno; uint8_t u8CopyBufferSize; uint16_t u16currentCoil, u16coil; boolean bTemp; // get the first and last coil from the message uint16_t u16StartCoil = word( au8Buffer[ ADD_HI ], au8Buffer[ ADD_LO ] ); uint16_t u16Coilno = word( au8Buffer[ NB_HI ], au8Buffer[ NB_LO ] ); // read each coil from the register map and put its value inside the outcoming message u8bitsno = 0; u8frameByte = 7; for (u16currentCoil = 0; u16currentCoil < u16Coilno; u16currentCoil++) { u16coil = u16StartCoil + u16currentCoil; u8currentRegister = (uint8_t) (u16coil / 16); u8currentBit = (uint8_t) (u16coil % 16); bTemp = bitRead( au8Buffer[ u8frameByte ], u8bitsno ); bitWrite( regs[ u8currentRegister ], u8currentBit, bTemp ); u8bitsno ++; if (u8bitsno > 7) { u8bitsno = 0; u8frameByte++; } } // send outcoming message // it's just a copy of the incomping frame until 6th byte u8BufferSize = 6; u8CopyBufferSize = u8BufferSize +2; sendTxBuffer(); return u8CopyBufferSize; } int8_t Modbus::process_FC16( uint16_t *regs, uint8_t u8size ) { uint8_t u8func = au8Buffer[ FUNC ]; // get the original FUNC code uint8_t u8StartAdd = au8Buffer[ ADD_HI ] << 8 | au8Buffer[ ADD_LO ]; uint8_t u8regsno = au8Buffer[ NB_HI ] << 8 | au8Buffer[ NB_LO ]; uint8_t u8CopyBufferSize; uint8_t i; uint16_t temp; // build header au8Buffer[ NB_HI ] = 0; au8Buffer[ NB_LO ] = u8regsno; u8BufferSize = RESPONSE_SIZE; // write registers for (i = 0; i < u8regsno; i++) { temp = word( au8Buffer[ (BYTE_CNT + 1) + i * 2 ], au8Buffer[ (BYTE_CNT + 2) + i * 2 ]); regs[ u8StartAdd + i ] = temp; } u8CopyBufferSize = u8BufferSize +2; sendTxBuffer(); return u8CopyBufferSize; }
Comments