<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Recent changes to Java Protocol tutorial sources</title><link>https://sourceforge.net/p/coronissdk/wiki/Java%2520Protocol%2520tutorial%2520sources/</link><description>Recent changes to Java Protocol tutorial sources</description><atom:link href="https://sourceforge.net/p/coronissdk/wiki/Java%20Protocol%20tutorial%20sources/feed" rel="self"/><language>en</language><lastBuildDate>Sat, 30 Jun 2012 21:34:04 -0000</lastBuildDate><atom:link href="https://sourceforge.net/p/coronissdk/wiki/Java%20Protocol%20tutorial%20sources/feed" rel="self" type="application/rss+xml"/><item><title>WikiPage Java Protocol tutorial sources modified by Thierry CHOMAUD</title><link>https://sourceforge.net/p/coronissdk/wiki/Java%2520Protocol%2520tutorial%2520sources/</link><description>&lt;pre&gt;--- v1
+++ v2
@@ -1,12 +1,3 @@
-Main.java source
-----------------
-
-~~~~~~~~~~~~
-package com.coronis.sdk.protocol.waveport.example;
-
-import java.text.SimpleDateFormat;
-import java.util.Date;
-
 Main.java source
 ----------------
 
@@ -18,6 +9,9 @@
  * - Send an application request to a distant device
  * - Send an application message to a distant device (with 1 relay)
  */
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
 import com.coronis.sdk.domain.Payload;
 import com.coronis.sdk.domain.RadioAddress;
 import com.coronis.sdk.protocol.api.CommandResponse;
&lt;/pre&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Thierry CHOMAUD</dc:creator><pubDate>Sat, 30 Jun 2012 21:34:04 -0000</pubDate><guid>https://sourceforge.netb12f2b66d3cb7c5c7a1b103213e5eaf0b6f97909</guid></item><item><title>WikiPage Java Protocol tutorial sources modified by Thierry CHOMAUD</title><link>https://sourceforge.net/p/coronissdk/wiki/Java%2520Protocol%2520tutorial%2520sources/</link><description>Main.java source
----------------

~~~~~~~~~~~~
package com.coronis.sdk.protocol.waveport.example;

import java.text.SimpleDateFormat;
import java.util.Date;

Main.java source
----------------

~~~~~~~~~~~~
/**
 * Main Class of Waveport protocol example
 * This example will demonstrate sequentially how to
 * - Read the radio address of connected radio modem
 * - Send an application request to a distant device
 * - Send an application message to a distant device (with 1 relay)
 */
import com.coronis.sdk.domain.Payload;
import com.coronis.sdk.domain.RadioAddress;
import com.coronis.sdk.protocol.api.CommandResponse;
import com.coronis.sdk.protocol.api.DynamicStandardParams;
import com.coronis.sdk.protocol.api.IPayload;
import com.coronis.sdk.protocol.api.IProtocolService;
import com.coronis.sdk.protocol.api.IRadioAddress;
import com.coronis.sdk.protocol.api.LocalNodeServiceCommand;
import com.coronis.sdk.protocol.api.LocalNodeServiceResponse;
import com.coronis.sdk.protocol.api.NodeServiceId;
import com.coronis.sdk.protocol.api.ProtocolException;
import com.coronis.sdk.protocol.api.RadioExchangeCommand;
import com.coronis.sdk.protocol.api.RequestResponse;
import com.coronis.sdk.protocol.api.ResponseStatus;
import com.coronis.sdk.protocol.waveport.Waveport;
import com.coronis.sdk.serialdriver.api.SerialDriverException;
import com.coronis.sdk.serialdriver.rs232driver.RS232Driver;

public class Main {

	// port ID to use
	private static String defaultPortId = "COM4";
	// radio address of the radio device to request (use the coronis radio address implementation (domain))
	private static IRadioAddress destinationAddress = new RadioAddress("11604A329237");
	private static IRadioAddress relayAddress = new RadioAddress("11604A32924C");
	// payload to send declaration (use the coronis payload implementation (domain))
	private static IPayload payload = new Payload("20");
	/**
	 * Main method
	 * @param args
	 */
	public static void main(String[] args) {
		// SerialDriver instantiation
		RS232Driver serialDriver = new RS232Driver();
    	Waveport wp = new Waveport();
		// Protocol instantiation
		IProtocolService protocol = new Waveport();
		try {
			// set port Id to use
			serialDriver.setSerialPortId(defaultPortId);
			
			// Protocol initialization (use the define serialDriver)
			protocol.setSerialDriverInstance(serialDriver);
			protocol.init();
			
			// Open the Protocol connection (will open the linked serial driver)
			protocol.open();
			
			// Read of the radio modem radioAddress
			processLocalService(protocol);
			
			// Send a radio application request (wait for radio response)
			processSendRequest(protocol);
			
			// Send a radio application message (no wait for radio response)
			processSendMessage(protocol);
			
		} catch (SerialDriverException e) {
			System.out.println(toPrintableDate(new Date())+ "serial driver exception: "+e.toString());
		} catch (ProtocolException e) {
			System.out.println(toPrintableDate(new Date())+ "protocol exception: "+e.toString());
		} finally {
			try {
				// Close the Protocol connection (will close serialDriver)
				protocol.close();
			} catch (ProtocolException e1) {
				e1.printStackTrace();
			}
		}
	}
	
	/**
	 * Read the radio address of connected radio modem
	 * @param protocol which must be opened.
	 */
	private static void processLocalService(IProtocolService protocol) {
		System.out.println(toPrintableDate(new Date())+ "process local node service: start");
		// Prepare the local node service command to execute
		LocalNodeServiceCommand localService = new LocalNodeServiceCommand(
				NodeServiceId.READ_RADIO_ADDRESS);
		// Execute the local node service
		try {
			LocalNodeServiceResponse response = protocol.requestLocalNodeService(localService);
			System.out.println(toPrintableDate(new Date())+ "process local service status: "+ response.getStatus());
			if (response.getStatus()==ResponseStatus.OK_PROTOCOL_SUCCESS) {
				System.out.println(toPrintableDate(new Date())+ "process local node service response: "+ response.getCommand().toString());
			}
		} catch (ProtocolException e) {
			System.out.println(toPrintableDate(new Date())+ "process local node service exception: "+e.toString());
		}
	}
	
	/**
	 * Send an application request to a distant device
	 * @param protocol which must be opened.
	 */
	private static void processSendRequest(IProtocolService protocol) {
		System.out.println(toPrintableDate(new Date())+ "process send request: start");
		// Prepare the request to send (without relay)
		RadioExchangeCommand request = new RadioExchangeCommand(
				payload,
				new DynamicStandardParams(destinationAddress));
		// Execute the application message
		try {
			RequestResponse response = protocol.sendApplicationRequest(request);
			System.out.println(toPrintableDate(new Date())+ "process send request status: "+ response.getStatus());
			if (response.getStatus()==ResponseStatus.OK_PROTOCOL_SUCCESS) {
				System.out.println(toPrintableDate(new Date())+ "process send request response: "+ response.getCommand().toString());
			}
		} catch (ProtocolException e) {
			System.out.println(toPrintableDate(new Date())+ "process send request exception: "+e.toString());
		}
	}
	
	/**
	 * Send an application message to a distant device (with 1 relay)
	 * @param protocol which must be opened.
	 */
	private static void processSendMessage(IProtocolService protocol) {
		System.out.println(toPrintableDate(new Date())+ "process send message: start");
		// Prepare the message to send (throw 1 relay)
		RadioExchangeCommand message = new RadioExchangeCommand(
				payload,
				new DynamicStandardParams(destinationAddress, new IRadioAddress[]{relayAddress}));
		// Execute the application message
		try {
			CommandResponse response = protocol.sendApplicationMessage(message);
			System.out.println(toPrintableDate(new Date())+ "process send message status: "+ response.getStatus());
		} catch (ProtocolException e) {
			System.out.println(toPrintableDate(new Date())+ "process send message exception: "+e.toString());
		}
	}

	/**
	 * Return a printable given date
	 * @param date
	 * @return
	 */
	public static String toPrintableDate(Date date) {
		SimpleDateFormat formatter = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss:SSS");
		return formatter.format(date).concat(" ");
	}
}
</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Thierry CHOMAUD</dc:creator><pubDate>Sat, 30 Jun 2012 21:33:03 -0000</pubDate><guid>https://sourceforge.net9c589705bfba8ac4c545b3d4f63319bf0073141a</guid></item></channel></rss>