USB-Ethernet/Ethernet-USB


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users USB-Ethernet/Ethernet-USB
# 8  
Old 05-28-2014
Okay makes sense.
Well most instrumentation gets attached via serial ports. In the survey business we are in that is how device manufacturers put out data. So it was initially designed for this purpose. Logging Ethernet data is a new configuration.

The instrument in questions acts as a server spitting out data. So I may just spawn a client to connect to it->get the data->send it over to the USB server.
# 9  
Old 05-28-2014
What kind of instrumentation? Just dealing with this building's solar instrumentation I've needed to combine RS-232, RS-485, USB, I2C, and ethernet. If you're lucky enough to stick with one manufacturer I suppose it's the same protocol all the time.

You could set up another computer to send it to the "usb server" I suppose, but that seems like overkill. It could be possible to run your application on the same machine. Is this application willing to read from a FIFO or file?
# 10  
Old 05-28-2014
its ground surveying equipment used to measure gamma rays. I wouldnt use a whole extra computer I would just have another process run on logging computer to handle it. as it stands now the USB server reads USB ports and sends them via a socket to a client on the same machine for graphical drawing to tell user that the devices are working. so I would just end up with two clients working with the same server.

---------- Post updated at 06:20 PM ---------- Previous update was at 06:03 PM ----------

i'm curious: for your building's solar instrumentation are you bundling all of that incoming data in one process? I am using the select() funciton to monitor the serial devices along with incoming socket connections. Is your setup similar?
# 11  
Old 05-28-2014
Mostly they must be polled -- they won't talk unless asked -- so select() isn't so useful. I have a bunch of small applications driven by one central script, which is run every 5 minutes by cron.

It's really quite a mishmash of different interfaces and protocols. Each device came with its own program from the manufacturer and no integration methods(unless you bought that particular manufacturer's product solely) so we rolled our own. Some of them were quite complicated, especially the inverters which used a complicated protocol involving packets and checksums on a shared RS-485 line. I eventually hooked up a little piezo speaker to the RS-485 line itself while wrestling with the protocol, when I finally got the checksums good enough the device would actually answer, I could hear 2 clicks instead of one. Then came the trouble of dealing with the garbage and retries necessary when wrangling RS-485 without hardware timing.

Only one of them, our UPS, was simple enough to just open the serial port and read it.
# 12  
Old 05-29-2014
intresting stuff. ya this device I have broacasting over ethernet needs to be asked as well. you send it a string and it sends some binary data back

---------- Post updated at 11:44 AM ---------- Previous update was at 11:31 AM ----------

Okay, been doing some toying here. This is probably going to be very obvious but something isn't working. I have been trying to setup a server that uses the select call to handle multiple client connections.

I use a master socket which listens on a port/IP and adds incoming connections to an array of child connections.

My client application simply reads from the socket over and over. What I want to do is once a new client connection is made on the server simply continue to send a message to that client. Then the client just printf's the message to the console.

Once I get a bare-bones version going I have a couple of different applications to apply this to.

Thanks a lot in advance. If I have done anything dumb...go easy Smilie

---------- Post updated at 11:46 AM ---------- Previous update was at 11:44 AM ----------

---------- Post updated at 11:48 AM ---------- Previous update was at 11:46 AM ----------


SERVER:

Code:
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <time.h> 

int g_portno = 40000;

int main (int argc, char *argv[]){


	FILE * error;
	error = fopen("server_error.txt","w");
    int i;

    //Parse command line:
    /*if(argc != 2){
        printf("Please specify how clients will connect?\n");
        exit(0);
    }
    int num_connections,i;
    num_connections = atoi(argv[1]);
    printf("num connections: %i\n",num_connections);
    */

	//CREATE SOCKET:
	int master_socket = socket(AF_INET,SOCK_STREAM,0);
	if(master_socket<0){
		fprintf(error,"socket create fail\n");
		exit(0);
	}

    //initialize all client_socket[] to 0 so not checked:
    int max_clients = 30;
    int client_socket[30];
    for(i=0;i<max_clients;i++){
        client_socket[i] = 0;
    }

    int opt=1;
    if(setsockopt(master_socket,SOL_SOCKET,SO_REUSEADDR,(char *)&opt,sizeof(opt))<0){
        fprintf(error,"setsockopt fail\n");
        exit(0);
    }

	struct sockaddr_in serv_addr;
	memset(&serv_addr, '0', sizeof(serv_addr));
	serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    serv_addr.sin_port = htons(g_portno);

    //BIND:
    int bd = bind(master_socket,(struct sockaddr*)&serv_addr, sizeof(serv_addr)); 
    if(bd<0){
    	fprintf(error,"bind error\n");
    	exit(0);
    }

    //LISTEN:
    int l = listen(master_socket,10);
    if(l<0){
    	fprintf(error,"listen error");
    	exit(0);
    }

    char buffer[1024];
    bzero(buffer,1024);
    
    int max_sd,sd,activity,s;
    fd_set readfds;


    while(1){


        //clear the socket set:
        FD_ZERO(&readfds);

        //add master_socket to set:
        FD_SET(master_socket,&readfds);
        max_sd = master_socket;

        //add child sockets to set:
        for(i=0;i<max_clients;i++){

            //socket descriptor:
            sd = client_socket[i];

            //if valid socket desrciptor then add to read list:
            if(sd > 0) FD_SET(sd,&readfds);

            //highest file descrptor number, need it for the select function:
            if(sd > max_sd) max_sd = sd;
        }

        //wait for an activity on one of the sockets, timeout is NULL, so wait indefinitely.
        activity = select(max_sd + 1,&readfds,NULL,NULL,NULL);
        if(activity < 0 ){
            fprintf(error,"select error\n");
            exit(0);
        }

        //if something happened on the master socket, then its an incoming connection:
        if(FD_ISSET(master_socket,&readfds)){

            int newsockfd = accept(master_socket, (struct sockaddr*)NULL, NULL); 
            if(newsockfd<0){
                fprintf(error,"accept fail\n");
                exit(0);
            }

            printf("new connection established on fd: %i\n",newsockfd);

            //add new socket to array of sockets:
            for(i=0;i<max_clients;i++){
                if(client_socket[i] == 0){
                    client_socket[i] = newsockfd;
                    printf("Adding to list of sockets as: %d\n",i);
                    break;
                }
            }

            snprintf(buffer,1024,"connected...\n");
            send(newsockfd,buffer,strlen(buffer),0); 

        }

        for(i=0;i<max_sd;i++){

            if(FD_ISSET(client_socket[i],&readfds)){                

                snprintf(buffer,1024,"Hello from server client %i\n",i);
                s = send(client_socket[i], buffer, strlen(buffer),0); 
                if(s==-1){
                    fprintf(error,"send fail\n");
                    exit(0);
                }

            }
        
            sleep(1);
        }

	}

	return 0;
}

---------- Post updated at 11:51 AM ---------- Previous update was at 11:48 AM ----------

CLIENT:

Code:
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <time.h> 

int g_portno = 40000;

int main (int argc, char *argv[]){

	FILE * error;
	error = fopen("client_1_error.txt","w");

	if(argc != 2){
             printf("Need an IP address\nTry again please\n");
             exit(0);
        } 

	//CREATE SOCKET:
	int client_1_socket = socket(AF_INET,SOCK_STREAM,0);
	if(client_1_socket<0){
		fprintf(error,"socket create fail\n");
		exit(0);
	}

	struct sockaddr_in serv_addr; 
	memset(&serv_addr, '0', sizeof(serv_addr));

	serv_addr.sin_family = AF_INET;
        serv_addr.sin_port = htons(g_portno);
        if(inet_pton(AF_INET, argv[1], &serv_addr.sin_addr)<=0){
             fprintf(error,"inet_pton error occured\n");
             exit(0);
       } 

    int connection = connect(client_1_socket,(struct sockaddr *)&serv_addr, sizeof(serv_addr));
    if(connection<0){
    	fprintf(error,"connection problem\n");
    	exit(0);
    }

    char buffer[1024];
    bzero(buffer,1024);

    while(1){    	

    	int n = read(client_1_socket,buffer,sizeof(buffer));

    	if(client_1_socket<0){
    		fprintf(error,"socket has closed\n");
    		exit(0);
    	}

        if(n<0){
            printf("read problem\n");
            exit(0);
        }

        printf("%s",buffer);

    }





	return 0;

}

---------- Post updated at 05:56 PM ---------- Previous update was at 11:51 AM ----------

Ok, all good.
Just needed to create another fd_set to monitor writeable sockets.

Last edited by fedora18; 05-29-2014 at 12:52 PM.. Reason: Indentation
# 13  
Old 05-29-2014
?

It works here.

Code:
$ ./server

new connection established on fd: 5
Adding to list of sockets as: 0

Code:
$ ./client 127.0.0.1
connected...

# 14  
Old 05-30-2014
Ya, I connected fine.
My problem was having a constant write from server to client loop. Reason being that while monitoring the fd_set readfs I wasn't getting a wakeup from select when the client was ready for writing. so created another fd_set writefs and all good.

thanks

---------- Post updated at 12:26 PM ---------- Previous update was at 09:26 AM ----------

So lets say I have bound a socket to a port and am listening on that port for incoming connections.

Is there a way upon receiving and incoming connection and calling accept() where I can establish a new socket on a different port. Can I feed a new sockaddr_in structure into accept()?

Back the data collection program here haha. Another instrument I am dealing with requires a client to connect on one port, send a request for data, and then it sends that data back on a different port.

Thanks all!

Last edited by fedora18; 05-30-2014 at 11:09 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. AIX

vio server ethernet to vio client ethernet(concepts confusing)

Hi In the vio server when I do # lsattr -El hdisk*, I get a PVID. The same PVID is also seen when I put the lspv command on the vio client partition. This way Im able to confirm the lun using the PVID. Similarly how does the vio client partition gets the virtual ethernet scsi client adapter... (1 Reply)
Discussion started by: newtoaixos
1 Replies

2. UNIX for Dummies Questions & Answers

USB-USB cable between linux and windows computers

Is there an easy way to setup a cross-over cable (USB-USB) between a linux box and a windows PC? My 2 machines are next to each other but I really do not want to keep transfering my files using my USB drive. Thanks! (4 Replies)
Discussion started by: Xterra
4 Replies

3. Solaris

solaris ethernet card and mounting usb drives

I had installed soalris 10 on my dell vostro 1400.It had installed succefully. If i type ifconfig -a it is showing only my loop back adpater. So how to tell me how to mount my usb drive and how to configure my lan ethernet card,My lan ethernet card is Broadcom. Tell me step... (1 Reply)
Discussion started by: testerindia25
1 Replies

4. Solaris

ethernet

my solris box network is unreachable how to solve this ipaddress assiagning and entry in vi /etc/host how to solve network is unreachable (2 Replies)
Discussion started by: tirupathi
2 Replies

5. Solaris

Ethernet Port Name

I have a new Sun 440 and I am trying to configure it. Non of the Ethernet ports are enabled; when I issue "ifconfig -a" it returns nothing. Is there a way to know the available port name (e.g. ce, bge, etc.) by running a command or so ? (4 Replies)
Discussion started by: StarSol
4 Replies

6. AIX

ethernet down

Hello everyone I have a problem with one server, has Aix 5.3 in the errpt has this message IDENTIFIER TIMESTAMP T C RESOURCE_NAME DESCRIPTION F3931284 0105133009 I H ent0 ETHERNET NETWORK RECOVERY MODE EC0BCCD4 0105133009 T H ent0 ETHERNET DOWN F3931284 ... (4 Replies)
Discussion started by: lo-lp-kl
4 Replies

7. Solaris

Quad Ethernet

I have a Quad Ethernet card in a 220R. 2 ports activated. Each has its own hostname file and both hostnames are in the hosts file. I want both ports to have the same IP address so we can use the 2nd port in case we lose communication on the port. On bootup, the box gives me: SIOCSLIFFLAGS:... (5 Replies)
Discussion started by: hshapiro
5 Replies

8. UNIX for Dummies Questions & Answers

Ethernet

We are trying to Establish Network using Linux.Now we are facing the problem in configring Ethernet.In out network we are giving eth0 in server and activating it at the boot time,so it is activating as it is but when we define the Ethernet of node on server and allowing it to activate at boot time... (5 Replies)
Discussion started by: at_renai2001
5 Replies
Login or Register to Ask a Question