As it's the holidays, I'm a little bored, so looking back on something I did a while back thought I would post it just incase someone may be interested.
An ex collegue of mine had an FX3U Mitsi PLC with a standard 232-BD coms board, what he really needed was the 232-ADP-MB for modbus communications, this has the required firmware to generate the required modbus RTU message including the CRC 16 checksum, the BD board does not.
So the attached is a CRC generator function in ST.
a typical modbus master message is for example [01,03,00,00,00,02,C4,0B]
i.e. Read holding registers [Addr,Function,start addr hi,start addr lo,number regs hi,num regs lo,crc hi,crc lo] so a total of 8 bytes. this reads registers 0000-0001 (4001-4002) due to the offset.
The crc function takes the 6 bytes (not the crc) & calculates & returns the checksum. You then build the whole message as the above example. bearing in mind in Mitsubishi all registers are 16 bit so a little fiddly, i.e. the first send array[0] will be reversed i.e. 0301 as will all others, the checksum does not as it is already reversed in the CRC function.
This could be ported into other platforms, however, the WTOB (word to byte) function may not be availlable so either do a byte swap or move the upper & lower bytes of the 16 bit registers into seperate 16 bit registers, this would be extra or incorporate the byte swap within the loop with another loop but could get messy.
EDIT: I did this not realising that the FX3U IDE has a crc function but did not see it, however, I don't think the other processors have that (just before someone comments that there is a standard function for the FX3U). Have a Happy new year
An ex collegue of mine had an FX3U Mitsi PLC with a standard 232-BD coms board, what he really needed was the 232-ADP-MB for modbus communications, this has the required firmware to generate the required modbus RTU message including the CRC 16 checksum, the BD board does not.
So the attached is a CRC generator function in ST.
a typical modbus master message is for example [01,03,00,00,00,02,C4,0B]
i.e. Read holding registers [Addr,Function,start addr hi,start addr lo,number regs hi,num regs lo,crc hi,crc lo] so a total of 8 bytes. this reads registers 0000-0001 (4001-4002) due to the offset.
The crc function takes the 6 bytes (not the crc) & calculates & returns the checksum. You then build the whole message as the above example. bearing in mind in Mitsubishi all registers are 16 bit so a little fiddly, i.e. the first send array[0] will be reversed i.e. 0301 as will all others, the checksum does not as it is already reversed in the CRC function.
This could be ported into other platforms, however, the WTOB (word to byte) function may not be availlable so either do a byte swap or move the upper & lower bytes of the 16 bit registers into seperate 16 bit registers, this would be extra or incorporate the byte swap within the loop with another loop but could get messy.
EDIT: I did this not realising that the FX3U IDE has a crc function but did not see it, however, I don't think the other processors have that (just before someone comments that there is a standard function for the FX3U). Have a Happy new year
Last edited: