IndustrialShields: Installation, Library und YanuxxNetClients
Arduino-IDE
Wir empfehlen, die Arduino-IDE von hier zu beziehen da andere Quellen evtl. nicht kompatibel sind:
http://www.arduino.org/downloads
Librarie SetUp
Um die Ethernet-Funktion des M-Duino aktivieren zu können, muss die Library W5100.h entpackt und in das folgende Arduino-Verzeichnis kopiert werden:
\\…\libraries\Ethernet\src\utility\
Die vorherige W5100.h sollte umbenannt werden.
Folgend ein Beispiel eines YanuxxNetClients:
/*Yx_Client.dpp
YanuxNet: Arduino-Web client
TechnoMicro(R):ThomasKroeger
V1.0.2-2016.05.03
*/
//#include "Yx_Client.h"
//#include <Yx_Client.h>
#include "Yx_Client.h"
EthernetClient Yx__Eclient;
//___________________________________
bool Yx_Client::StartSocket(IPAddress IP, int Port, byte *MAC)
{
// start the Ethernet connection:
if (IP[0]==0)
{ return Ethernet.begin(MAC); //via DHCP
}
else
{ Ethernet.begin(MAC, IP); //nur mit const IP
return true;
}
}//StartSocket
//___________________________________
bool Yx_Client::Open2Socket(IPAddress IP, int Port, int ConnectTimeout, int ReadTimeout)
{//connect server
gConnectTimeout=ConnectTimeout;
gReadTimeout=ReadTimeout;
return (Yx__Eclient.connect(IP, Port)) ;
}//Open2Socket
//___________________________________
void Yx_Client::Close2Socket(void)
{ Yx__Eclient.stop();
}//Close2Socket
//___________________________________
String Yx_Client::Get1Value(IPAddress IP, int Port, int ConnectTimeout, int ReadTimeout)
{ String inbuf="";
//connect server:
if (Open2Socket(IP, Port, ConnectTimeout, ReadTimeout))
{ inbuf=Get2Value(); //read server
Close2Socket(); //disconnect server
}
return inbuf;
}//GetValue
//___________________________________
String Yx_Client::Get2Value(void)
{ int n=0;
char c=0;
String inbuf;
Yx__Eclient.print("value");
while (c!='\r')
{ if (Yx__Eclient.available())
{ c = Yx__Eclient.read();
inbuf += c;
}
if (c=='\r')
{ inbuf +="\r";
return inbuf;
}
delay(1);
if (n++>gReadTimeout)
{ inbuf="e";
return inbuf;
}
}
}//Get2Value
//___________________________________
String Yx_Client::Tx2CmdRx(String cmd)
{ int n=0;
char c=0;
String inbuf;
Yx__Eclient.print(cmd);
while (c!='\r')
{ if (Yx__Eclient.available())
{ c = Yx__Eclient.read();
inbuf += c;
}
if (c=='\r')
{ inbuf +="\r";
return inbuf;
}
delay(1);
if (n++>gReadTimeout)
{ inbuf="e";
return inbuf;
}
}
}//Tx2CmdRx
//___________________________________
String Yx_Client::Set2Value(String cmd)
{ int n=0;
char c=0;
String inbuf="";
//empfohlen:
Yx__Eclient.print("value "+cmd);
/*
//nicht empfohlen:
cmd ="value "+cmd+'\0';
for(int i=0; i<cmd.length(); i++)
{ Yx__Eclient.write(cmd[i]);
}//for(int i=0
*/
while (c!='\r')
{ if (Yx__Eclient.available())
{ c = Yx__Eclient.read();
inbuf += c;
}
if (c=='\r')
{ inbuf +="\r";
return inbuf;
}
delay(1);
if (n++>gReadTimeout)
{ inbuf="e";
return inbuf;
}
}
}//Set2Value
//___________________________________
/*Yx_Client.h
YanuxNet: Arduino-Web client
TechnoMicro(R):ThomasKroeger
V1.0.1-2016.05.03
*/
#ifndef __Yx_Client__
#define __Yx_Client__
#include <Arduino.h>
#include <Ethernet.h>
//_______________________________________________
class Yx_Client
{
private:
int gConnectTimeout;
int gReadTimeout;
public:
//IP=(0, xx, xx, xx): ueber DHCP IP=(>0, xx, xx, xx):mit angegebener IP
//Port hat keine Funktion
bool StartSocket(IPAddress IP, int Port, byte *MAC);
bool Open2Socket(IPAddress IP, int Port, int ConnectTimeout, int ReadTimeout);
void Close2Socket(void);
String Get2Value(void);
String Get1Value(IPAddress IP, int Port, int ConnectTimeout, int ReadTimeout);
String Tx2CmdRx(String cmd);
String Set2Value(String cmd);
};
//_______________________________________________
#endif //Yx_Client