
|
//-----------------------------------------------------------------------------
|
|
|
// test.vpl, created 2001-10-31 12:31
|
|
|
// Simple GPRS test program. The program tries to connect to an "echo host".
|
|
|
// The echo host will echo all data received on port 5005 back to the sender.
|
|
|
// The program connects to the echo host, and send a packet, which is then
|
|
|
// echoed back from the echo server. This is done 100 times, then the program
|
|
|
// will disconnect the socket, and try to connect again. It will then send
|
|
|
// another 100 frames and so on. If the 'restart' input is activated, the
|
|
|
// program will disconnect and then connect again to the echo host.
|
|
|
// Please note that the echo server program is located in the same directory
|
|
|
// as the "Socket Example" project, under the x:\....\RTCU-IDE\Examples\ directory,
|
|
|
// and is called echos.exe ("C" source also included in echos.c).
|
|
|
// This program must be started on a PC with a fixed IP address. This IP address
|
|
|
// (or symbolic name) must then be put into the 'host' variable (see below,
|
|
|
// currently set to "rtcu.dk")
|
|
|
//-----------------------------------------------------------------------------
|
|
|
// Input variables that can be configured via the configuration dialog (These are global)
|
|
|
restart : BOOL R_EDGE; | Restart connection (disconnect/connect)
|
|
|
// Output variables that can be configured via the configuration dialog (These are global)
|
|
|
toggle : BOOL; | LED that indicate when data is received from echo host
|
|
|
gprs : BOOL; | LED that indicates that a GPRS connection is present.
|
|
|
// These are the global variables of the program
|
|
|
sockrcv : sockReceive; // Receive data on a socket
|
|
|
soccon : sockConnection; // Create a socket connection
|
|
|
ip : DINT; // The IP address of the echo host
|
|
|
id : SINT; // ID of the connection
|
|
|
buffer : ARRAY[0..300] OF SINT; // Receive buffer
|
|
|
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"; // The echo host (can also be a dotted format IP address)
|
|
|
port : INT :=5005; // The echo host port number.
|
|
|
iter : DINT; // Number of iterations
|
|
|
rc : INT; // Return codes from socket calls
|
|
|
//-----------------------------------------------------------------------------
|
|
|
//-----------------------------------------------------------------------------
|
|
|
DebugMsg(message:="GPRS Socket test-program started.");
|
|
|
// Turn on power to GSM module
|
|
|
// Open the GPRS connetion
|
|
|
DebugFmt(message:="gprsOpen()=\1",v1:=rc);
|
|
|
// Wait for GPRS connected (after this, we are connected to the Internet)
|
|
|
WHILE NOT gprsConnected() DO
|
|
|
DebugMsg(message:="Waiting for GPRS connection");
|
|
|
// Lookup the IP address of the echo host (DNS lookup)
|
|
|
ip:=sockIPFromName(str:=host);
|
|
|
// Connect to the echo host, using port 5005
|
|
|
id:=sockConnect(ip:=sockIPFromName(str:=host),port:=port);
|
|
|
DebugFmt(message:="socConnect()=\1",v1:=id);
|
|
|
// Initialize the sockConnection() functionblock
|
|
|
// And intialize the socket receiver
|
|
|
sockrcv.data:=ADDR(buffer);
|
|
|
sockrcv.maxsize:=SIZEOF(buffer);
|
|
|
soccon(); // Update sockConnection() functionblock
|
|
|
sockrcv(); // Update sockReceive() functionblock
|
|
|
// If restart switch activated, disconnect the old connection, and make a new one
|
|
|
sockDisconnect(id:=soccon.id);
|
|
|
// Lookup IP address of echo host, and make a connection to it
|
|
|
id:=sockConnect(ip:=sockIPFromName(str:=host),port:=port);
|
|
|
DebugFmt(message:="socConnect()=\1",v1:=id);
|
|
|
// Update sockConnection()/sockReceive() functionblocks with the new connection ID
|
|
|
// If connection status changed on socket
|
|
|
// ...and we are now connected, go send data to echo host
|
|
|
// (This will "prime" the conversation, after this the data are "re-used"
|
|
|
// as the echo server gets it's own data back again (see sockrcv.ready below))
|
|
|
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);
|
|
|
// ...and we are now disconnected, go make a new connection attempt
|
|
|
DebugMsg(message:="Socket disconnected");
|
|
|
sockDisconnect(id:=soccon.id);
|
|
|
// Let the sockConnection() see that we are disconnected
|
|
|
// Lookup IP address of echo host, and make a connection to it
|
|
|
id:=sockConnect(ip:=sockIPFromName(str:=host),port:=port);
|
|
|
DebugFmt(message:="socConnect()=\1",v1:=id);
|
|
|
// Update sockConnection()/sockReceive() functionblocks with the new connection ID
|
|
|
// If received data on the socket (echo data from echo server)
|
|
|
DebugFmt(message:="\4 - data received len=\1", v4:=iter, v1:=sockrcv.size);
|
|
|
toggle:=NOT toggle; // Change the state of the LED
|
|
|
// After 100 iterations, we Diconnect the socket. This will activate
|
|
|
// the code above (soccon.changed) and then try to connect the socket again
|
|
|
// Send the data we just received from the echo server, back again
|
|
|
sockSend(id:=id, data:=ADDR(buffer), size:=sockrcv.size);
|
|
|
sockDisconnect(id:=soccon.id);
|
|
|
|