USB-Ethernet/Ethernet-USB

Hi,

I have posted several times regarding my application.

In a nutshell, I have a server application that logs attached instrument data over USB and Ethernet. My restriction here is having two wires or more to plug all instrumentation in - at least one for USB and at least one for ethernet.

The solution for the USB instrumentation is to route them through a hub into one wire to plug into the computer.

I have seen several low-cost Ethernet->USB as well as USB->Ethernet adapters. I understand that they both get data to your computer but thinking from a lower-level point of view, how does the computer see the data?

For the Ethernet->USB adapter does this mean that data that was initially sent from an instrument in TCP protocol arrives at a /dev/ttyUSB* port in USB fashion? Would I connect to it in my program via accessing the USB port?

I am sure the answer is quite simple but i'd like to know before I order anything up.

An example of one of the units I am looking at is:

http://www.thesource.ca/estore/product.aspx?product=8010146&language=en-CA&utm\_source=pla&utm\_medium=pla&utm\_campaign=adchemix-pla&gclid=CjgKEAjwkpacBRCNlprWw-u-nBwSJACwHiw-VHVELwMpWEK2LQUcyG6ccUQhAA2aLazQA2b1IULx3PD_BwE

Thanks in advance for your help.

Think of it this way -- a serial cable is like a composite television cable. You plug it in and it shows up on the TV, more or less. There's not a whole lot of fiddling.

A network cable is more like an antenna cable. If you plug it in and see nothing, maybe you're on the wrong channel, band, protocol.... It can carry lots of different things at the same time, you have to select what you want.

This is why you can't just 'cat /dev/ethernetdevice' and see the raw data from your data logger. You need to know what IP address and protocol you want to get data from, and have to have your computer configured correctly to read it.

Is there a way it's supposed to be used? Absolutely but we don't know what it is yet since all the information we need to know is missing. You've refused to answer all our questions.

You've been asked repeatedly for information about the device you're trying to log from. What is it? What model is it? Chances are the device will not log information until you make a TCP connection to it. It might use telnet or it might use http or it might use something else. The information may be in its manual.

When you tried to make a network, presumably to talk to this device on, we asked you repeatedly what information you had for the various things on your network. Several working, cooperating pieces all correctly configured are are required for a working network; knowing one computer's settings is less than half the story.

1 Like

I already wrote a program using your advice last week to log the Ethernet (TCP) data. Works fine. Thanks for the help. I opened a socket and connecte to the instruments IP and Port (as provided by manufacturer). Packet data is logged to MicroSD.

Given my new knowledge of these different data trasmition protocols I was just wondering how these Ethernet->USB devices work that's all.

Sorry for being a pain.

1 Like

The lack of feedback on your topics, and your fixation on turning eth0 into a ttyUSB device, gives the impression you're still fighting the same problem.

USB ethernet adaptors are nothing less than an external network "card". It will act just like your other ethernet devices. It will not make a TTY device. How could it? It's not going to have the correct port for your data logger built into the hardware.

There may be a raw USB device to read, but its data won't make much sense to you unless you spend some years studying USB protocol and reverse-engineering your USB network card's driver.

1 Like

Okay, so the card would just have its own IP and port of transmission then?

Yes, it'd need an IP address to talk over an IP network just like your other ethernet devices.

The "port" has more to do with the program you're connecting with. Consider it like a telephone branch office. The IP address is the "building", the port is the "extension number". Opening 1.2.3.4 port 80 would get you its web server, 1.2.3.4 port 21 would get its ssh server, etc.

Does your data logging program absolutely require a serial port? You might be able to emulate that, running an external program to read from TCP/IP and write to a pretend serial device, or perhaps a FIFO.

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.

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?

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?

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.

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 :slight_smile:

---------- 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:

#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 = 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;

            //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 == 0){
                    client_socket = 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,&readfds)){                

                snprintf(buffer,1024,"Hello from server client %i\n",i);
                s = send(client_socket, 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:

#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.

?

It works here.

$ ./server

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

$ ./client 127.0.0.1
connected...


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!

That sounds very strange unless the instrument's using UDP instead of TCP, which doesn't use connections at all... UDP is more like passing instant-messages back and forth. Individual messages fly around between arbitrary destinations without any connection needing to be set up between them, just fire-and-forget.

I have gradually given up hope that you're ever going to tell us what any of these instruments are, but I'm still bound to ask -- what instrument is this? Knowing what it is, I could probably give you much better advice.

It's a spectrometer. For measuring gamma rays. Not really a high production, well known piece of equipment. It's in the development stage and I am trying to set something up to working with it.

As I mentioned though, it requires me to open up another socket connection on a different port for reading. I am not sure how to do this with accept().

Depends what protocol it uses.

Having not seen its manual, I have absolutely no idea.