Code: Select all
class ModbusTCP {
public:
ModbusTCP(void);
void begin(void);
void run(void);
int setFloat(uint16_t iAddress, float fValue);
float getFloat(uint16_t iAddress);
int setU32(uint16_t iAddress, uint32_t iValue);
uint32_t getU32(uint16_t iAddress);
int setU16(uint16_t iAddress, uint16_t iValue);
uint16_t getU16(uint16_t iAddress);
private:
uint16_t mb_reg[MB_REGISTERS_MAX];
uint8_t mb_adu[260];
Server* mb_server;
Client* mb_client;
#ifdef SOME_SPECIAL_DEFINE_TO_ENABLE_CC3000
// Members that are included only with CC3000 support. This is just to give a place to create
// and store these variables so they don't have to be created dynamically with new/malloc.
// Only the setup function should touch these variables, all the other functions should interact
// with mb_server and mb_client so they can work with either CC3000 or ethernet library
// client / server code.
Adafruit_CC3000 cc3000;
Adafruit_CC3000_Server cc3000server;
#elif SOME_SPECIAL_DEFINET_TO_ENABLE_ETHERNET_LIBRARY
// Members to hold ethernet library objects. Again, only setup() should touch these.
EthernetClient ethernetClient;
EthernetServer ethernetServer;
#endif
};
Then in your begin() function you'd want to probably put a couple ifdef blocks to setup the server and client classes depending on what board the user has specified. For example the CC3000 server setup might look something like:
Code: Select all
void ModbusTCP::begin(void) {
#ifdef SOME_SPECIAL_DEFINE_TO_ENABLE_CC3000
// Insert standard CC3000 setup code to connect to network, get IP, etc...
// i.e. call stuff like:
// cc3000.begin();
// cc3000.connectToAP(...);
// cc3000.checkDHCP();
// .. etc.
// Create server and save reference to it.
// Insert CC3000 server setup code like:
// cc3000server.begin();
// Finally update Server pointer to use cc3000 server reference.
Server = &cc3000server;
#elif SOME_SPECIAL_DEFINET_TO_ENABLE_ETHERNET_LIBRARY
// .. ethernet library setup code goes here ..
// Update Server pointer to use ethernet library server reference at the end:
Server = ðernetServer;
#endif
}
You'll also need to change all the functions that use mb_server and mb_client to deal with those objects as pointers (use -> instead of . to call a function for example). However the functions should 'just work' with any underlying implementation this way, since both the ethernet library and CC3000 library have implemented the same client & server interface. In practice I haven't seen a ton of examples of people doing this kind of cross-board support, so you might run into some bugs or quirks with the CC3000 library. Let me know if so an I can help give some advice or try to integrate fixes so the library works better side by side with the ethernet library. Good luck!