NEW, 14.04.2013.
FUNCTION CODE LIBRARY ADDED:
01 Read Coil Status
02 Read Input Status
03 Read Holding Registers
04 Read Input Registers
05 Force Single Coil
06 Preset Single Register
Under Function codes Library section of the package file:
Look at the picture bellow (package Modbus RTU Master installed);
opcode:
Modbus_MASTER_Config (baud_rate, system_clock, parity_setting);
//baud rate in bps
//system_clock in MHz
//parity setting:
//- 0 no parity check involved
//- 1 odd parity applied
//- 2 even parity
//Timeout value set
Timeout_Set (timeout_value); //timeout value in miliseconds, 1500ms by default
// 01 Read Coil Status
Function_01 (slave_address, starting_address, number_of_coils);
// 02 Read Input Status
Function_02 (slave_address, starting_address, number_of_inputs);
// 03 Read Holding Registers
Function_03 (slave_address, starting_address, number_of_registers);
// 04 Read Input Registers
Function_04 (slave_address, starting_address, number_of_registers);
// 05 Force Single Coil
Function_05 (slave_address, coil_address, force_data); // force_data (coil state) = 0 or 1
// 06 Preset Single Register
Function_06 (slave_address, register_address, preset_data);
Example:
//Modbus RTU Master initialization
Modbus_MASTER_Config (9600, 16., 2); // baud rate = 9600 bps, f_osc = 16Mhz, even parity,
// TIMEOUT is 1500ms by default
// note: system_clock value is required in order to get // UART baud rate parameters calculated
Timeout_Set (2500); // new value for the timeout period approximatelly2500ms
// .....
// part of code where the user software (master device) initiates Modbus transaction
// eg. the master requests contents of ten holding registers at adresses 40250 to 40259 from slave
// device 15
Function_03 (15, 250, 10);
// the response message is being stored into resp_msg, 256 bytes long array that is defined within
// the library, it can't be redefined by the user, it has sufficient capacity to recive the longest Modbus // RTU ADU frame (256 bytes) anyway
// the user software serves over the received message content
// there is a 8-bit status register defined by the library RESPSTAT which status bits tells the // master whether a response mesage is received or not upon the receiving is complete. It is on the // user software to check it out. That part of code should be applied as the next section after writting // function code library function
RESPSTAT register:
bit 7: -
bit 6: -
bit 5: -
bit 4: -
bit 3: RERR communication error occured
bit 2: TOUT TIMEOUT expired
bit 1: ROK RESPONSE OK
bit 0: MRXF calculated number of mesage bytes received (reception complete)
example: (continued from the previous modbus requesting)
if (ROK_bit == 1)
Response_processing(); // response OK (expected response received - normal or exception)
else
Response_fault (); // none response received, timeout expired or communication error