gxdn

Gurux.Serial

Join the Gurux Community or follow @Gurux for project updates.

With Gurux.Serial component you can send data easily syncronously or asyncronously.

Open Source Gurux Serial media component 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: Network, Terminal, SMS.

You can get source codes and examples from http://www.github.com/gurux or if you use Maven add this to your POM-file:

  org.gurux
  gurux.serial
  1.0.13

If you are using Android add this to build.gradle-file:
compile 'org.gurux:gurux.serial.android:1.0.8'
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:
  • PortName
  • BaudRate
  • DataBits
  • Parity
  • StopBits

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

  • onError
  • onReceived
  • onMediaStateChange

GXSerial cl = new GXSerial();
cl.setPortName(gurux.serial.GXSerial.getPortNames()[0]);
cl.setBaudRate(9600);
cl.setDataBits(8);
cl.setParity(Parity.ODD);
cl.setStopBits(StopBits.ONE);
cl.open();
GXSerial cl = new GXSerial();
cl.PortName = Gurux.Serial.GXSerial.GetPortNames()[0];
cl.BaudRate = 9600;
cl.DataBits = 8;
cl.Parity = System.IO.Ports.Parity.Odd;
cl.StopBits = System.IO.Ports.StopBits.One;
cl.Open();
GXSerial cl = new GXSerial(this);
cl.setPort(cl.getPorts()[0]);
cl.setBaudRate(9600);
cl.setDataBits(8);
cl.setParity(Parity.ODD);
cl.setStopBits(StopBits.ONE);
cl.open();

Data is send with send command:

cl.send("Hello World!", null);
cl.Send("Hello World!");
cl.send("Hello World!", null);

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

//1. Add class that you want to use to listen media events and derive class from IGXMediaListener
*/
 Media listener.
*/
class GXMediaListener implements IGXMediaListener
{
    /** 
    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
    */
    @Override
    void onTrace(Object sender, TraceEventArgs e)
    {

    }

    // Summary:
    //  Event raised when a property is changed on a component.
    @Override
    void onPropertyChanged(Object sender, PropertyChangedEventArgs e)
    {

    }
}

//2. Listener is registered calling addListener method.
cl.addListener(this);
cl.OnReceived += new ReceivedEventHandler(this.OnReceived);
*/
 Media listener.
*/
public class MainActivity extends AppCompatActivity implements IGXMediaListener {
{
	/** 
    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)
    {
	//Show occured error.
    }

    /** 
    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)
    {
	//Handle received asyncronous data here.
    }

    /** 
    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)
    {
	//Media is open or closed.
    }

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

    @param sender
    @param e
    @see IGXMedia.Trace Traceseealso>
    */
    @Override
    void onTrace(Object sender, TraceEventArgs e)
    {
	//Send and received data is shown here.
    }
    
    /** 
    Represents the method that will handle the System.ComponentModel.INotifyPropertyChanged.PropertyChanged
    event raised when a property is changed on a component.
    
    @param sender The source of the event.
    @param sender e A System.ComponentModel.PropertyChangedEventArgs that contains the event data.
    */
    @Override
    void onPropertyChanged(Object sender, PropertyChangedEventArgs e)
    {
	//Example port name is change.
    }      
}

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

Data can be also send as syncronous if needed.

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);
    }
}
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..");
    }
}

Thread safety

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

lock (gxSerial1.SyncRoot)
{
    gxSerial1.Open();
    ...
    gxSerial1.Close();
}

Project type: 
Full project
Short name: 
Gurux.Serial
Enable issue tracker: 
Yes
Has project releases: