@SurferTim
My communicator class in seperate cpp an h files and these are used in my sketch files:
My sketch file:
#include <CommunicationModule.h>
#include <CommunicationModuleTCPIP.h>
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0xA4, 0x13 }; //physical mac address
IPAddress serverIP(192,168,100,101); // IP Adress to our Server
int serverPort=5000;
char Buffer[10];
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to
EthernetClient client;
CommunicationModuleTCPIP CMIP;
void setup() {
CMIP = CommunicationModuleTCPIP(mac, serverIP, 5000, false);
CMIP.Connect();
}
void loop()
{
// if there are incoming bytes available
// from the server, read them and print them:
if (CMIP.Client.available() == 10) {
CMIP.RecieveBuffer(Buffer, 10);
Serial.print(Buffer);
CMIP.SendBuffer(Buffer, 10);
}
// if the server's disconnected, stop the client:
if (!CMIP.Client.connected()) {
Serial.println();//report it to the serial
Serial.println("disconnected");
CMIP.Client.stop();
// do nothing forevermore:
for(;;)
;
}
}
My class headerfile:
#ifndef COMMUNICATIONMODULETCPIP_H_
#define COMMUNICATIONMODULETCPIP_H_
#include "CommunicationModule.h"
#include "Arduino.h"
#include <SPI.h>
#include <Ethernet.h>
//extern EthernetServer ServerObject;
class CommunicationModuleTCPIP : public CommunicationModule
{
public:
CommunicationModuleTCPIP(byte MacAdress[], IPAddress ServerIPServerIPAdress, int ServPort, bool Server);
CommunicationModuleTCPIP();
int Connect();
void SendBuffer(char Buffer[], int Size);
void RecieveBuffer(char Buffer[], int Size);
void ClearBuffer(char Buffer[]);
bool IsServer;
byte Mac[];
IPAddress ServerIP;
int ServerPort;
EthernetClient Client;
EthernetServer ServerObject;
bool DataRecieved;
private:
};
#endif /* COMMUNICATIONMODULE_H_ */
And the cpp file of the class:
#include "CommunicationModuleTCPIP.h"
CommunicationModuleTCPIP:: CommunicationModuleTCPIP()
:ServerObject(5000){
}
CommunicationModuleTCPIP:: CommunicationModuleTCPIP(byte MacAdress[], IPAddress ServerIPAdress, int ServPort, bool Server)
:ServerObject(5000)
{
if(!Server){
IsServer = false;
byte Mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0xA4, 0x13 }; //physical mac address
//for(int Index=0; Index < 6; Index++){
// Mac[Index] = MacAdress[Index];
//};
ServerIP = ServerIPAdress; // IP Adress to our Server
ServerPort = ServPort;
// start the serial for debugging
Serial.begin(9600);
// start the Ethernet connection:
// the router's gateway address:
byte gateway[] = { 192, 168, 100, 1 };
// the subnet:
byte subnet[] = { 255, 255, 255, 0 };
IPAddress ip(192,168,100,105);
// For direct connection
Ethernet.begin(Mac, ip, gateway, gateway, subnet);
// For DHCP connection
//if (Ethernet.begin(Mac) == 0) {
// Serial.println("Failed to configure Ethernet using DHCP");
// // no point in carrying on, so do nothing forevermore:
// for(;;){
// ;
// }
//}
}
else{
IsServer = true;
Serial.begin(9600);
delay(1000);
char Buffer[10];
Serial.println("Setting up server");
// the router's gateway address:
byte gateway[] = { 192, 168, 100, 1 };
// the subnet:
byte subnet[] = { 255, 255, 255, 0 };
//Mac = byte[] { 0x90, 0xA2, 0xDA, 0x0D, 0xA4, 0x13 }; //physical mac address
for(int Index=0; Index < 6; Index++){
Mac[Index] = MacAdress[Index];
};
ServerIP = ServerIPAdress; // IP Adress to our Server
ServerPort = ServPort;
// initialize the ethernet device
Ethernet.begin(Mac, ServerIP, gateway, gateway, subnet);
// start listening for clients
ServerObject.begin();
Serial.println("Server setup");
}
}
int CommunicationModuleTCPIP::Connect(){
// give the Ethernet shield a second to initialize:
delay(1000);
Serial.println("connecting to Server ...");
// if you get a connection to the Server
//IPAddress serverIp(192,168,100,101);
if (Client.connect(ServerIP , 5000)) {
Serial.println("connected");//report it to the Serial
return 0;
}
else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
return -1;
}
};
void CommunicationModuleTCPIP::SendBuffer(char Buffer[], int Size){
//Serial.println("Before");
//Client = ServerObject.available();
//Client.write((uint8_t*)Buffer, Size);
//Serial.println("After");
if(IsServer){
if(DataRecieved){
// Serial.println("client connected send");
Client.write((uint8_t*)Buffer, Size);
DataRecieved = false;
}
}
else{
Client.write((uint8_t*)Buffer, Size);
}
}
void CommunicationModuleTCPIP::RecieveBuffer(char Buffer[], int Size){
if(IsServer){
Client = ServerObject.available();
if (Client.connected()) {
if(!DataRecieved){
// Serial.println("client connected recieve");
for(int i=0;i < Size; i++){
Buffer[i] = Client.read();
}
for (unsigned int i = 0; i < 10; i++) {
Serial.print(Buffer[i]);
}
Serial.println();
DataRecieved = true;
}
}
}
else{
for(int i=0;i < Size; i++){
Buffer[i] = Client.read();
};
}
}
void CommunicationModuleTCPIP::ClearBuffer(char Buffer[]){
Buffer[0] = '\0';
}
Hope this gives more insight, i have to go to an appointment so i will not be able to respond back until to night/ tommorrow. Hope that you (and others) can help me.
Many thanks!