wiki:Docs/Prog/Manual/DeviceSupport/Network

Version 6 (modified by Don Wilson, 14 years ago) ( diff )

--

Network

Network File System (NFS)

It is helpful to setup an NFS share on your host computer for application testing. You may place your executables to be tested in the directory and access them from the target hardware. Using MiniCom, HyperTerminal or another communications program to communicate with the debug serial port 115000 8N1 the prompt should appear as:

#

Type:

# mount –t nfs 192.168.1.100:/srv/nfs /mnt/nfs –o nolock <CR>

Replace "192.168.1.100:/srv/nfs" with the appropriate IP address and share name of your NFS share. "/srv/nfs" is NFS share name already configured when using the VMware development image.

Type:

# cd /mnt/nfs

to change to the NFS directory. You can then run an application directly from the NFS folder without having to copy it to the 825 NAND. Make sure the application is flagged as executable from the host. To set a file as executable:

# chmod +x indicator

To run the application type:

# ./indicator &

To run an application named indicator. Note the "./" at the beggining is needed to tell the OS to run the executable in the current directory. Alternatively, you may run the executable file by specifying the full path:

# /mnt/nfs/indicator &

The "&" symbol specifies that the application will run in a new thread so you may continue to type commands, for example to check memory or copy files while the program is running. If the "&" symbol is not specified the terminal will be locked until the program exits or "CTRL-C" from the terminal will usually be able to stop the execution.

To stop a running application type:

# ps

This will list the running processes:

  PID TTY          TIME CMD
    1 ?        00:00:02 init
    2 ?        00:00:00 kthreadd
    3 ?        00:00:00 migration/0
    4 ?        00:00:00 ksoftirqd/0
    5 ?        00:00:00 watchdog/0
    6 ?        00:00:00 migration/1
    7 ?        00:00:00 ksoftirqd/1
    8 ?        00:00:00 watchdog/1
    9 ?        00:00:00 events/0
   10 ?        00:00:00 events/1
   11 ?        00:00:00 khelper
   85 ?        00:00:00 kintegrityd/0
   86 ?        00:00:00 kintegrityd/1
   88 ?        00:00:00 kblockd/0
   89 ?        00:00:00 kblockd/1
   91 ?        00:00:00 kacpid
   92 ?        00:00:00 kacpi_notify

Identify the PID number of the process to stop and type:

# kill <PID>  (where <PID> is the appropriate process ID)

The "pidof" statement allows for another method of stopping a process:

# kill 'pidof indicator'

Note if you accidentally press ENTER before closing the apostrophe you may get a prompt:

>

It is not possible to get out of this prompt until a closing apostrophe "'" is typed and ENTER is pressed:

>'
Unknown command
#

At times you may lose track of your current path. To check the current path type:

# pwd

This will respond with the current path such as “/mnt/nfs”.

Weight Server -- Network Access

The 825 weight server feature allows remote control operations to be performed via a network connection. The weight server also allows weight data to be accessed via a network connection. The weight data may be accessed using SMA commands such as "<LF>W<CR>" from a client to request the weight or the legacy CTRL-E request for 738 computer format data. This also network access to the 825 from various software such as Cardinal WinVRS or Cardinal WinDDE.

Network Programming

Programs may be written utilizing many of the common Linux network libraries. Programs may be written to act as a TCP/IP server waiting for connections from clients. This is similar to the 825 weight server.

825 TCP/IP Server Example

//============================================================================
// Name        : tcpip_server.cpp
// Author      : Don Wilson
// Version     :
// Copyright   : Cardinal Scale Mfg
// Description : TCP/IP Server Demo
//============================================================================

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <termios.h>
#include <fcntl.h>

#include <string.h>

#include <math.h>
#include <time.h>

#include <signal.h>

#include <netinet/in.h>
#include <arpa/inet.h>

#include <sys/mman.h>
#include <sys/types.h>          /* socket, bind, accept */
#include <sys/socket.h>         /* socket, bind, accept, setsockopt, */
#include <sys/stat.h>           /* open */

#include <vector>
using namespace std;

// Dynamic list to keep track of connected clients
vector<int> client_list;

int server_s;

const int sock_opt = 1;

const int nPort = 10010;  // Change to use a different TCP/IP port

int ServerProcess(void)
{
	int i;
	int client_s;
	struct sockaddr_in addr;
	socklen_t socklen = sizeof(addr);

	// Check for new client connections
	client_s = accept(server_s, (sockaddr*)&addr, &socklen);
	if(client_s >= 0)
	{
		char szIP[INET_ADDRSTRLEN];

		inet_ntop(AF_INET, &addr.sin_addr, szIP, INET_ADDRSTRLEN);

		printf("TCP/IP server connection from %s socket %d\r\n", szIP, client_s);

	    /* nonblocking socket */
	    if (fcntl(client_s, F_SETFL, O_NONBLOCK) == -1)
		{
	        printf("fcntl: unable to set new socket to non-block\r\n");
	        close(client_s);
	    }
	    else
	    {
	    	client_list.push_back(client_s);
	    }
	}

	if(client_list.size() > 0)
	{
		fd_set rfds;
		FD_ZERO(&rfds);
		int nMaxfd = 0;
		for(i = 0; i < (int)client_list.size(); i++)
		{
			FD_SET(client_list[i], &rfds);
			if(client_list[i] > nMaxfd)
				nMaxfd = client_list[i];
		}

		struct timeval tv;
		tv.tv_sec = 0;
		tv.tv_usec = 0;

		int retval = select(nMaxfd + 1, &rfds, NULL, NULL, &tv);
		if(retval == -1)
			printf("select return -1\r\n");
		else if(retval > 0)
		{
			printf("select returned %d\r\n", retval);

			int rcvlen;
			char data[20], sendbuf[50];
			int z;
			int remove = -1;

			// Process all connected clients
			for(i = 0; i < (int)client_list.size(); i++)
			{
				if(FD_ISSET(client_list[i], &rfds))
				{
					rcvlen = recv(client_list[i], data, sizeof(data), 0);
					for(z = 0; z < rcvlen; z++)
					{
						if(data[z] == 0x05)  // ENQ = CTRL-E received
						{
							// Send response to client
							time_t rawtime;
							struct tm *timeinfo;
							time(&rawtime);
							timeinfo = localtime(&rawtime);

							sprintf(sendbuf, "Server time is: %02d:%02d:%02d ",
									timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);

							send(client_list[i], sendbuf, strlen(sendbuf), 0);
						}
					}
					if(rcvlen <= 0)
					{
						printf("recv Error %d\r\n", rcvlen);
						remove = i;
					}
				}
			}
			if(remove >= 0)
			{
				printf("Removing %d client socket %d\r\n", remove, client_list[remove]);
				client_list.erase(client_list.begin() + remove);
			}
		}
	}
	return 0;
}

int main()
{

	server_s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
	if(server_s == -1)
	{
		printf("unable to create socket\r\n");
		return -1;
	}

    /* server socket is nonblocking */
    if (fcntl(server_s, F_SETFL, O_NONBLOCK) == -1)
    {
	    printf("fcntl: unable to set server socket to nonblocking");
	    return -1;
    }

 //   /* close server socket on exec so CGIs can't write to it */
//	if (fcntl(server_s, F_SETFD, 1) == -1) {
 //       printf("can't set close-on-exec on server socket!");
//	}

    /* reuse socket addr */
	if ((setsockopt(server_s, SOL_SOCKET, SO_REUSEADDR, (void *) &sock_opt, sizeof (sock_opt))) == -1)
	{
        printf("setsockopt");
	}


	struct sockaddr_in server_sockaddr;
	memset(&server_sockaddr, 0, sizeof server_sockaddr);
	server_sockaddr.sin_family = PF_INET;

	server_sockaddr.sin_addr.s_addr = htonl(INADDR_ANY);

	server_sockaddr.sin_port = htons(nPort);

	if(bind(server_s, (struct sockaddr *) &server_sockaddr, sizeof (server_sockaddr)) == -1)
	{
		printf("Unable to bind\r\n");
		return -1;
	}

	if(listen(server_s, 10) == -1)
	{
		printf("unable to listen");
	}

	while(1)
	{
		ServerProcess();
	}

	return 0;
}

825 TCP/IP Client Example

// TCP/IP Client Example

Top

Note: See TracWiki for help on using the wiki.