SimpleModbusSlave allows you to communicate to any slave using the Modbus RTU protocol. The crc calculation is based on https://os.mbed.com/users/jpelletier/code/CRC/ The functions implemented are functions 3 and 16. read holding registers and preset multiple registers of the Modbus RTU Protocol. This implementation DOES NOT fully comply with the Modbus specifications.
Revision 1:85e226914fb7, committed 2017-10-25
- Comitter:
- gugani
- Date:
- Wed Oct 25 10:38:11 2017 +0000
- Parent:
- 0:7c5265097fd2
- Commit message:
- Initial commit
Changed in this revision
SimpleModbusSlave.cpp | Show annotated file Show diff for this revision Revisions of this file |
SimpleModbusSlave.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/SimpleModbusSlave.cpp Tue Oct 10 12:22:31 2017 +0000 +++ b/SimpleModbusSlave.cpp Wed Oct 25 10:38:11 2017 +0000 @@ -18,6 +18,8 @@ unsigned int T1_5; // inter character time out unsigned int T3_5; // frame delay unsigned char buffer = 0; +unsigned char datarec = 0; +unsigned char datasent = 0; Timer intercharacter_timer; Timeout interframe_timeout; @@ -33,10 +35,27 @@ void sendPacket(unsigned char bufferSize); unsigned int reverse_CRC(unsigned int calculated_CRC); +unsigned int newdatarec(){ + if (datarec){ + datarec = 0; + return 1; + } + else return 0; +} + +unsigned int newdatasent(){ + if (datasent){ + datasent = 0; + return 1; + } + else return 0; +} + void end_of_transmission() { if (TxEnablePin > 1) dir = 0; + datasent = 1; } unsigned int modbus_update(unsigned int *holdingRegs) @@ -85,6 +104,7 @@ unsigned int crc = ((frame[buffer - 2] << 8) | frame[buffer - 1]); // combine the crc Low & High bytes if (reverse_CRC(calculate_crc16_Modbus(frame, buffer - 2)) == crc) // if the calculated crc matches the recieved crc continue { + datarec = 1; function = frame[1]; unsigned int startingAddress = ((frame[2] << 8) | frame[3]); // combine the starting address bytes unsigned int no_of_registers = ((frame[4] << 8) | frame[5]); // combine the number of register bytes
--- a/SimpleModbusSlave.h Tue Oct 10 12:22:31 2017 +0000 +++ b/SimpleModbusSlave.h Wed Oct 25 10:38:11 2017 +0000 @@ -84,6 +84,7 @@ // function definitions void modbus_configure(long baud, char _slaveID, char _TxEnablePin, unsigned int _holdingRegsSize, unsigned char _lowLatency); unsigned int modbus_update(unsigned int *holdingRegs); - +unsigned int newdatarec(void); +unsigned int newdatasent(void); #endif