INCLUDE rtcu.inc
VAR_INPUT
restart : BOOL R_EDGE;
END_VAR;
VAR_OUTPUT
toggle : BOOL;
gprs : BOOL;
END_VAR;
VAR
sockrcv : sockReceive;
soccon : sockConnection;
id : SINT;
buffer : ARRAY[0..300] OF SINT;
teststr1 : STRING :="Hello world. This is a test of the RTCU SOCKET Interface";
teststr2 : STRING :="Hello world. The RTCU Concept is the most powerfull GSM Telemetry platform in the world!!";
host : STRING :="rtcu.dk";
port : INT :=5005;
iter : DINT;
rc : INT;
END_VAR;
PROGRAM test;
DebugMsg(message:="GPRS Socket test-program started.");
gsmPower(power:=ON);
rc:=gprsOpen();
DebugFmt(message:="gprsOpen()=\1",v1:=rc);
WHILE NOT gprsConnected() DO
DebugMsg(message:="Waiting for cellular connection");
Sleep(delay:=2500);
END_WHILE;
id:=sockConnect(ip:=sockIPFromName(str:=host),port:=port);
DebugFmt(message:="sockConnect()=\1",v1:=id);
soccon.id:=id;
sockrcv.id:=id;
sockrcv.data:=ADDR(buffer);
sockrcv.maxsize:=SIZEOF(buffer);
BEGIN
gprs:=gprsConnected();
soccon();
sockrcv();
IF restart THEN
sockDisconnect(id:=soccon.id);
id:=sockConnect(ip:=sockIPFromName(str:=host),port:=port);
DebugFmt(message:="sockConnect()=\1",v1:=id);
soccon.id:=id;
sockrcv.id:=id;
END_IF;
IF soccon.changed THEN
IF soccon.Connected THEN
DebugMsg(message:="Socket connected");
DebugMsg(message:=sockIPToName(ip:=soccon.remoteIP));
strToMemory(dst:=ADDR(buffer),str:=teststr1,len:=strLen(str:=teststr1));
rc:=sockSend(id:=id,data:=ADDR(buffer),size:=strLen(str:=teststr1));
DebugFmt(message:="sockSend rc=\1",v1:=rc);
strToMemory(dst:=ADDR(buffer),str:=teststr2,len:=strLen(str:=teststr2));
rc:=sockSend(id:=id,data:=ADDR(buffer),size:=strLen(str:=teststr2));
DebugFmt(message:="sockSend rc=\1",v1:=rc);
iter:=0;
ELSE
DebugMsg(message:="Socket disconnected");
sockDisconnect(id:=soccon.id);
soccon();
id:=sockConnect(ip:=sockIPFromName(str:=host),port:=port);
DebugFmt(message:="sockConnect=\1",v1:=id);
soccon.id:=id;
sockrcv.id:=id;
END_IF;
END_IF;
IF sockrcv.ready THEN
iter:=iter+1;
DebugFmt(message:="\4 - data received len=\1", v4:=iter, v1:=sockrcv.size);
toggle:=NOT toggle;
IF iter < 100 THEN
sockSend(id:=id, data:=ADDR(buffer), size:=sockrcv.size);
ELSE
sockDisconnect(id:=soccon.id);
END_IF;
END_IF;
END;
END_PROGRAM;
|