Skip to main content
Home
for DLMS smart meters

Main navigation

  • Home
  • Products
  • About us
  • Open Source
  • Community
  • Forum
  • Downloads
  • Gurux Club
User account menu
  • Log in

Breadcrumb

  1. Home
  2. Gurux.Net

Gurux.Net

gxdn
Profile picture for user Administrator
By Administrator, 24 November, 2015
View releasesView issues
Join the Gurux Community or follow @Gurux for project updates.

With Gurux.Net component you can send data easily syncronously or asyncronously using TCP or UDP connection.

Open Source GXNet media component, made by Gurux Ltd, is a part of GXMedias set of media components, which programming interfaces help you implement communication by chosen connection type. Gurux media components also support the following connection types: serial port, Terminal, SMS.

There are also examples available in GitHub. If you have problems you can ask your questions in Gurux Forum.

Note!
It's important to listen OnError event. Connection might break and it's the only way to get info from it.

Simple example

Before use you must set following settings:
  • HostName
  • Port
  • Protocol

It is also good to add listener to listen following events.

  • onError
  • onReceived
  • onMediaStateChange

and in server mode following events might be important.

  • onClientConnected
  • onReceived
  • onClientDisconnected

  • Java
  • C#
  • ANSI C++
GXNet cl = new GXNet();
cl.setHostName("localhost");
cl.setPort(1000);
cl.setProtocol(NetworkType.TCP);
cl.open();
GXNet cl = new GXNet();
cl.HostName = "localhost";
cl.Port = 1000;
cl.Protocol = NetworkType.Tcp;
cl.Open();
CGXNet cl;
cl.SetProtocol(GX_NW_TCPIP);
cl.SetPort(port);
cl.SetTrace(GX_TRACE_LEVEL_VERBOSE);
cl.IsServer(true);
cl.AddListener(this);
int ret;
if ((ret = m_Media.Open()) != 0)
{
	printf("Media open failed %d", ret);
	return ret;
}	

Data is send with send command:

  • Java
  • C#
  • ANSI C++
cl.send("Hello World!", null);
cl.Send("Hello World!");
const char* pData = "Hello World";
vector data;
data.insert(data.end(), pData, reinterpret_cast(pData) + strlen(pData));			
cl.Send(data, NULL);

In default mode received data is coming as asynchronously from OnReceived event.
Event listener is added like this:

  • Java
  • C#
  • ANSI C++
//1. Add class that you want to use to listen media events and derive class from IGXMediaListener
*/
 Media listener.
*/
class GXMediaListener implements IGXMediaListener, gurux.net.IGXNetListener
{
    /** 
    Represents the method that will handle the error event of a Gurux component.
    @param sender The source of the event.
    @param ex An Exception object that contains the event data.
    */
    @Override
    void onError(Object sender, RuntimeException ex)
    {
    }

    /** 
     Media component sends received data through this method.

     @param sender The source of the event.
     @param e Event arguments.
    */
    @Override
    void onReceived(Object sender, ReceiveEventArgs e)
    {

    }

    /** 
     Media component sends notification, when its state changes.
     @param sender The source of the event.    
     @param e Event arguments.
    */
    @Override
    void onMediaStateChange(Object sender, MediaStateEventArgs e)
    {

    }

    /** 
     Called when the Media is sending or receiving data.
     @param sender
     @param e
     @see IGXMedia.Trace Traceseealso>
    */
    @Override
    void onTrace(Object sender, TraceEventArgs e)
    {

    }

    // Summary:
    //     Represents the method that will handle the System.ComponentModel.INotifyPropertyChanged.PropertyChanged
    //     event raised when a property is changed on a component.
    //
    // Parameters:
    //   sender:
    //     The source of the event.
    //
    //   e:
    //     A System.ComponentModel.PropertyChangedEventArgs that contains the event
    //     data.
    @Override
    void onPropertyChanged(Object sender, PropertyChangedEventArgs e)
    {

    }

    /*
     * Client is made connection.
     */
    @Override
    public void onClientConnected(Object sender, gurux.net.ConnectionEventArgs e) 
    {
        System.out.println("Client Connected.");
    }

    /*
     * Client is closed connection.
     */
    @Override
    public void onClientDisconnected(Object sender, gurux.net.ConnectionEventArgs e) 
    {
        System.out.println("Client Disconnected.");
    }
}

//2. Listener is registered calling addListener method.
cl.addListener(this);
cl.OnReceived += new ReceivedEventHandler(this.OnReceived);
//1. Add class that you want to use to listen media events and derive class from IGXMediaListener
class CGXMediaListener : IGXMediaListener, IGXNetListener
{
    void OnClientConnected(IGXMedia* pSender, CConnectionEventArgs& e)
	{
		printf("Client Connected : %s\r\n", e.GetInfo().c_str());
	}

    /** 
     Called when the client has been disconnected from the GXNet server.

     @param sender The source of the event.    
     @param e Event arguments.
    */
    void OnClientDisconnected(IGXMedia* pSender, CConnectionEventArgs& e)
	{
    	printf("Client Disonnected : %s\r\n", e.GetInfo().c_str());
	}
    
    void OnError(IGXMedia* pSender, basic_string& ex)
	{
		printf("Error has occurred : %s\r\n", ex.c_str());
	}

    /** 
     Media component sends received data through this method.

     @param sender The source of the event.
     @param e Event arguments.
    */
    void OnReceived(IGXMedia* pSender, CReceiveEventArgs& e)
	{		
	}

    /** 
     Media component sends notification, when its state changes.

     @param sender The source of the event.    
     @param e Event arguments.
    */
    void OnMediaStateChange(IGXMedia* pSender, CMediaStateEventArgs& e)
	{
	}

    /** 
     Called when the Media is sending or receiving data.

     @param sender
     @param e
     @see IGXMedia.Trace Trace
    */
    void OnTrace(IGXMedia* pSender, CTraceEventArgs& e)
	{
    	printf("%s\r\n", e.ToString().c_str());
	}
    
    // Summary:
    //  Event raised when a property is changed on a component.
    //
    // Parameters:
    //   sender:
    //     The source of the event.
    //
    //   e:
    //     A System.ComponentModel.PropertyChangedEventArgs that contains the event
    //     data.
    void OnPropertyChanged(IGXMedia* pSender, CPropertyChangedEventArgs& e)
	{

	}
}

//2. Listener is registered calling addListener method.
cl.AddListener(this);

Data can be also send as syncronous if needed.

  • Java
  • C#
  • ANSI C++
synchronized (cl.getSynchronous())
{
    String reply = "";    
    ReceiveParameters<byte[]> p = new ReceiveParameters<byte[]>(byte[].class);    
    //End of Packet.
    p.setEop('\n'); 
    //How long reply is waited.   
    p.setWaitTime(1000);          
    cl.send("Hello World!", null);
    if (!cl.receive(p))
    {
        throw new RuntimeException("Failed to receive response..");
    }
}
lock (cl.Synchronous)
{
    string reply = "";
    ReceiveParameters<string> p = new ReceiveParameters<string>()
    //ReceiveParameters<byte[]> p = new ReceiveParameters<byte[]>()
    {
       //Wait time tells how long data is waited.
       WaitTime = 1000,
       //Eop tells End Of Packet charachter.
       Eop = '\r'
    };
    cl.Send("Hello World!", null);
    if (cl.Receive(p))
    {
        reply = Convert.ToString(p.Reply);
    }
}

//ANSI C++ do not support syncronous mode at the moment.

Thread safety

Only c# version from Gurux.Net is thread safety. It's recommended that component is used only one thread.

  • C#
lock (gxNet1.SyncRoot)
{
    gxNet1.Open();
    ...
    gxNet1.Close();
}

  • Create new account
  • Reset your password

Hire Us!

Latest Releases

Fri, 03/24/2023 - 14:22
gurux.dlms.c 20230324.1
Thu, 03/23/2023 - 11:01
GXDLMSDirector 9.0.2303.2301
Thu, 03/23/2023 - 09:10
Gurux.DLMS.Python 1.0.142
Wed, 03/22/2023 - 13:51
Gurux.DLMS.Net 9.0.2303.2201
Wed, 03/22/2023 - 10:15
gurux.dlms.c 20230322.1

Open bugs

Gurux.DLMS.AMI4
1
Gurux.DLMS.Android
1
gurux.dlms.c
3
gurux.dlms.cpp
3
gurux.dlms.delphi
1
RSS feed
Privacy FAQ GXDN Issues Contact
Follow Gurux on Twitter Follow Gurux on Linkedin