
** Purpose:

These demo programs show you how to use TouchPAD as Modbus RTU slave in C code.
The master TouchPAD communicates to slave TouchPAD base on 115200 bps, 8N1, 
NetID = 1 by default.

These demos can be used as 1 master to 1 slave communication test.
For 1 master to many slaves, you have to add more devices/IO tags/Ladder 
codes to support more slave TouchPADs. 
Make sure each slave TouchPAD has different NetID when they are in the same RS-485 bus.

Also, user can use another Modbus RTU master program/controller/device 
to test with these slave TouchPADs.

For more information, refer to comments in the slave demo programs.


** Requirement:
 
HMIWorks v2.04 Beta 2 or last.


** Functions:

// NetID : 1 ~ 247
// Each char in pDioBuf contains 8-ch DIO data.
// The spaces pointered by DioBuf and AioBuf are allocated by user.
// Currently, we support only 1 slave device in a TouchPAD.
// Set 0 to both DIO_ch_count and AIO_ch_count to unregister that slave device.
// Return: TRUE = OK, FALSE = Parameter Error.
BOOL mrs_RegisterSlave(unsigned char NetID, WORD DIO_StartAddr, WORD DIO_count, char *pcDioBuf, 
                                            WORD AIO_StartAddr, WORD AIO_count, WORD *pwAioBuf);

// Process the Modbus RTU command (suggested in each 30 ms).
// Return: bit 0 = DIO is updated, bit 1 = AIO is updated.
//         bit 31 = Error (0x8000 0000), bit 8 = Read DI, bit 9 = Read AI
unsigned long mrs_ProcessCmd(HANDLE hPort);


** Question & Answer

Q: How many TouchPADs can be installed in a RS-485 bus? 
A: These Slave API functions does not have the limitation. 
   TouchPAD's RS-485 has pull-high/low resistor, this limits about 16 ~ 32 pcs 
   TouchPADs can be installed in a RS-485 bus. For more TouchPAD to be installed 
   in a single RS-485 bus, the repeater is required.
   
Q: How many I/O points are supported by the Slave API functions?
A: These Slave API functions does not have the limiation.
   It's limited on TouchPAD's memroy/flash size.   
 
Q: How to set the baudrate/data format of the Modbus RTU slave?
A: You have to use uart_open() function to assign a COM port and baudrate/data 
   format first. And then use the handle value for mrs_ProcessCmd() function.
   Please refer to the demo programs.
 
Q: Can other slave I/O devices be installed in the RS-485 bus when there is 
   TouchPAD in Slave mode?
A: Yes, but you have to make sure their NetID are all different.
 
Q: How to control other slave I/O devices by slave TouchPAD?
A: No, that is master's (PC/Controller/PLC...) job to control a slave I/O device.
   The Slave TouchPAD responses to master's command only.
   The Slave TouchPAD will not communicate with other slave devcies.
   All slave devices and slave TouchPAD must be controlled by a master. 
 
Q: How to support multi-switching between Slave TouchPADs?
   (Note: Multi-switching means multiple clients to access/share the same 
   I/O devices or I/O points.)
A: That is master's job to control/access/share I/O devices between slaves. 
 
Q: How to update multiple TouchPAD's I/O and display status at the same time 
   or automaticlly.
A: No, it is master's job to update each TouchPAD one-by-one.
   The TouchPAD in slave mode cannot require data automatically.
 
Q: Will the TouchPAD automatically update the I/O and display status after reboot?
A: No, it is master's job to update each TouchPAD one-by-one.
   The TouchPAD in slave mode cannot require data automatically.
   In the demo programs, we use a flag in TouchPAD (slave mode) for master 
   to check if the TouchPAD is rebooted or not.
 
Q: Which models are supported by the Modbus RTU Slave API functions?
A: It supports TPD-280, TPD-280U, TPD-430 and VPD-130 models.
   It does not support TPD-283, since the TPD-283 has no serial port.
 
Q: Does the TouchPAD slave function supports Ladder mode?
A: Not yet! Users have to write their C code for the salve function and also 
   need to defines their I/O address in the C code.  
 
Q: Looks like the Slave API functions has many limitations?
A: Yes, most limitations are base on the RS-485 bus, that requires half-duplex 
   communication and requires a single Master in a RS-485 bus.
   You cannot have two or more masters in the same RS-485 bus, else the commands 
   can be sent at the same time and then causes garbage messages.
   
   In slave mode, the TouchPAD is working like other slave devices such as 
   I-7000/M-7000... etc, that are all requires a master to control it. 
   Most logics are totally controlled by the master.
    
