W&T connects
Interfaces for TCP/IP, Ethernet, RS-232, RS-485, USB, 20mA, glass and plastic fiber optic cable, http, SNMP, OPC, Modbus TCP, I/O digital, I/O analog, ISA, PCI

Application for pure.box:

Run your own C program on the pure.box


The following example is intended to provide an overview of what steps are necessary to compile a C source text for the target platform of the pure.box, load the compiling into the box and run it cyclically. The function of this sample program is to reverse the switching state of an output on a digital Web-IO once a minute.

The pure.box allows your system to run created compilings at system startup, time-driven or when TCP or UDP data are received. The following libraries are supported:

  • GNU C-Lib v2.1
  • SQLite v3.6
  • OpenSSL v0.9
  • zlib v1.2

Downloading and installation of the Cross-Compiler

A BNU/Linux operating system runs in the Coldfire processor used in the pure.box. The cross-compiler (incl. documentation) needed for this processor/operating system combination can be downloaded here at no charge in the Lite version for Windows and Linux. Please refer to the documentation for information about installing on the host system.

Compiling source text under Windows

The source text for this sample program can be downloaded here. The archive contains the C-file as well as the already complete pure.box compiling (output_toggle).

Save the file output_toggle.c on your hard drive and open the Windows entry prompt. Navigate to the directory in which you previously saved the source text file. The following command creates the compiling:

m68k-linux-gnu-gcc -mcpu=54450 -o output_toggle output_toggle.c

The compiler m68k-linux-gnu-gcc is invoked by additionally specifying the target platform processor type (-mcpu=54450). The object file to be created is specified by -o output_toggle. Then you specify the C-file (output_toggle.c). If the command was successfully executed, you will find the created program file output_toggle in the same directory as the original source text.

Configuration of the pure.box

Save the file output_toggle using FTP or SMB sharing in the folder named programs in the pure.box. Then the following configuration steps are carried out:

  • Open the Homepage of the pure.box by entering the IP address in the address line of your browser.

    Motherbox homepage
  • Select Login in the menu tree and log on using your password in the opened dialog box.

    Motherbox login dialog box
  • Navigate to the page Home >> Configuration >> User files. In the file overview click on the folder named programs. The file save_data.php is displayed.

    Motherbox file browser
  • Clicking on the icon Edit file properties (left next to output_toggle) opens the Edit dialog.

    Edit file properties
  • Check the option Program. For Parameters enter:

    • IP address of the Web-IO digital
    • Number of the HTTP port
    • Channel to switch cyclically
    • Password if set

    If you have not assigned a password, the invoking could look as follows:
    10.40.27.10 80 5 ""

    The stringing together of the parameters is identical with that for direct console invoking of the program.

    Event control
  • Activate Time controlled and ensure that in each of the following text fields a * is noted. As a result of this CRON definition the program output_toggle is run once a minute with the values specified under Parameters.

    Store the new setting by clicking on Save... you’re done!

    Event control

From now on the Web-IO specified in the invoking parameters is accessed by the pure.box over the network. The switching state of the specified output is changed each time.

Source text

In the first lines the required header files are specified.

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>

The program consists exclusively of one main function. At the beginning the local variables are specified. The values sent by passing the parameters are then output. This output is suppressed by the pure.box each time the cycle starts, which however is very helpful during the development phase when the program is also run locally on the PC.

int main(int argc, char **argv) {
	int handle;
	char command[128];
	struct sockaddr_in remote;
	printf("address: %s\r\n",argv[1]);
	printf("port: %i\r\n",atoi(argv[2]));
	printf("channel: %i\r\n",atoi(argv[3]));
	printf("password: %s\r\n",argv[4]);
	...
}

Then the command for toggling the output is constructed and saved in the variable command array.

sprintf(command, "GET /outputaccess%i?PW=%s&State=TOGGLE&\r\n", atoi(argv[3]), argv[4]);

handle holds the socket through which communication with the Web-IO takes place. If this cannot be created, the program is exited.

handle = socket(PF_INET , SOCK_STREAM , 0);
if(socket < 0) {
	printf("Socket cannot be created\r\n");
	exit(-1);
}

Creating the connection end point and establishing the connection. If an error occurs, the program is exited.

inet_aton(argv[1], &remote.sin_addr);
remote.sin_family = AF_INET;
remote.sin_port = htons(atoi(argv[2]));
if(connect(handle, (struct sockaddr *) &remote, sizeof(remote)) != 0) {
	printf("Connection cannot be established\r\n");
	exit(-1);
}

The previously constructed command string is sent to the Web-IO through the opened connection.

if(send(handle, (const void *)command, sizeof(command), 0) == 0) {
	printf("Error at sending data\r\n");
}

Close the connection and properly exit the program.

close(handle);
exit(0);

The program has fully run and is exited. It is restarted at the next run time point and again runs as described above.

Summary

Using simple means the pure.box can be used as a carrier of your own C and C++ programs. Very complex tasks can be managed by accessing the network and the internal file system.


Up