Ethernet Communications Overview

Written on: September 13, 2022 1:14 pm

JNIORs can connect with other devices via Ethernet connections. Defining a port and IP, the JNIOR can listen or send to a device with those Ethernet settings using the IO and Net classes of the JANOS Runtime library. Receiving data uses a DataInputStream and sending data uses a DataOutputStream. 

TCP

Below is a very short example, that shows a quick reference to the IO class to create a socket to open a DataOutputStream. Once that is open I can send a message to an external device. The socket only requires the IP address and the port number. In this example the IP is 10.0.0.17 and the port is 9222. Once the DataOutputStream is created using the socket, I send a string out it. I use a TCP server I created that listens on that IP and port number to see if it receives the data sent.

package ethernetexample;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;

public class TCPSocketExample {

    //declare output stream and socket objects for an ethernet connection
    public static OutputStream outputStream = null;
    public static Socket socket = null;

    public static void main(String[] args) throws IOException {

        //set IP and port number
        int portOut = 9222;
        String IP = "10.0.0.17";

        try {
            //set socket and then create outputstream with socket
            socket = new Socket(IP, portOut);
            outputStream = new DataOutputStream(socket.getOutputStream());
        } catch (NullPointerException e) {
        }
        
        try {
            //send message out the outputstream
            outputStream.write("This was sent from an ethernet connection.".getBytes());
            outputStream.flush();
        } catch (NullPointerException e) {}
        

    }

}

I put the built jar file of this example application into the JNIOR’s flash folder and ran it from the Web UI’s console tab. After it has successfully run, I check my TCP server and see that the information was successfully received.

UDP

Another example of Ethernet control uses a UDP connection versus a TCP connection. Another example is below, using the Datagram Socket and Packet classes from the Java.net package. This creates a UDP connection from the JNIOR to any UDP server listening and sends it a string. 

package ethernetexample2;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class UDPExample {

    public static DatagramSocket udpsocket;
    public static InetAddress address;
    public static byte[] buf;
    
    public static void main(String[] args) throws UnknownHostException, IOException {
        
    udpsocket = new DatagramSocket();
    address = InetAddress.getByName("10.0.0.17");

       try {
        buf = "This was sent from a UDP connection.".getBytes();
        DatagramPacket packet = new DatagramPacket(buf, buf.length, address, 9222);
        udpsocket.send(packet);
        } catch (NullPointerException e) {
        }
    }
    
}

Just like before, I put the built jar file of this example application into the JNIOR’s flash folder and ran it from the Web UI’s console tab. After it has successfully run, I check a UDP server I created  and see that the information was successfully received.

By | Updated On October 20, 2022 1:55 pm | No Comments | Categories: , | Tags:


 

INTEG Process Group, inc. © 2022

Real-Time
Mon - Fri, 8am - 4pm EST
P: 724-933-9350
PureChat
Always Available
Contact Form
sales@integpg.com
support@integpg.com

@integpg
@jniordev