Classes | |
| interface | MessagereceivedEvent |
| Message received event. More... | |
| interface | Serialport |
| Serialport. More... | |
| interface | SerialportConfig |
| Serialport Configuration. More... | |
| interface | SerialportEvent |
| Serialport event. More... | |
| enum | SerialportFlowcontrol |
| interface | SerialportInfo |
| Serialport Information. More... | |
| class | SerialportLowLevelException |
| Serialport low level exception. More... | |
| interface | SerialportManager |
| Serialport Manager. More... | |
| class | SerialportNotAvailableException |
| Serialport not available exception. More... | |
| class | SerialportObserver |
| Serialport observer. More... | |
| enum | SerialportParity |
| enum | SerialportProtocol |
Detailed Description
To work with Deepsy serialport service it is necessary to use DeepsyserialportManager located in com.gmv.its.deepsy.serialport for getting the initial serialportManager object.
DeepsySerialportManager.getInstance has an optional parameter indicating the ip address of remote service. For instance:
SerialportManager is an AutoCloseable interface. An object that may hold resources (such as file or socket handles) until it is closed. The close() method of an AutoCloseable object is called automatically when exiting a try-with-resources block for which the object has been declared in the resource specification header. This construction ensures prompt release, avoiding resource exhaustion exceptions and errors that may otherwise occur. To obtain a list of the serial port objects:
If this object is not inside a try with resources block, it is recommended to call to serialportManager.close() in order to finalize properly;
Examples
-
Get SerialPorts
package com.gmv.its.dpyjavaexamples.serialport;import java.util.List;import com.gmv.its.deepsy.hal.serialport.*;import com.gmv.its.deepsy.serialport.*;public class ListSerialPortApp {public static void main(String[] args) {try (DeepsySerialportManager serialportManager = DeepsySerialportManager.getInstance("192.168.0.50")) {List<Serialport> serialportList = serialportManager.getSerialPortList();System.out.println("There are " + serialportList.size() + " SerialPorts");for (Serialport s : serialportList) {System.out.printf("Serial Port id : %s\n", s.getPortid());System.out.printf("%20s : %s\n", "Alias", s.getConfiguration().getAlias());System.out.printf("%20s : %s\n", "Protocol", s.getConfiguration().getProtocol());System.out.printf("%20s : %s\n", "Baud Rate", s.getConfiguration().getBaudrate());System.out.printf("%20s : %s\n", "Data bits", s.getConfiguration().getDatabits());System.out.printf("%20s : %s\n", "Stop Bits", s.getConfiguration().getStopbits());System.out.printf("%20s : %s\n", "Parity", s.getConfiguration().getParity());System.out.printf("%20s : %s\n", "Flow Control", s.getConfiguration().getFlowcontrol());System.out.printf("%20s : %s\n", "Address", s.getInformation().getAddress());System.out.printf("%20s : %s\n", "RS485 pinout", s.getInformation().getRS485Pinout());System.out.printf("%20s : %s\n", "RS485 txen", s.getInformation().getRS485txen());System.out.printf("%20s : \n", "Allowed Protocols");s.getInformation().getProtocolsAllowed().forEach(p -> System.out.printf("%20s\n", "- " + p.toString()));}} catch (SerialportNotAvailableException e) {e.printStackTrace();} catch (SerialportLowLevelException e) {e.printStackTrace();}}}
-
Set SerialPort Configuration
package com.gmv.its.dpyjavaexamples.serialport;import java.util.List;import com.gmv.its.deepsy.hal.serialport.*;import com.gmv.its.deepsy.serialport.*;public class SetSerialPortConfigurationApp {public static void main(String[] args) {try (DeepsySerialportManager serialportManager = DeepsySerialportManager.getInstance("192.168.0.50")) {List<Serialport> serialportList = serialportManager.getSerialPortList();System.out.println("There are " + serialportList.size() + " SerialPorts");Serialport serialPort = serialportList.get(0);if (serialPort != null) {String portID = serialPort.getPortid();String currentAlias = serialPort.getConfiguration().getAlias();SerialportProtocol currentProtocol = serialPort.getConfiguration().getProtocol();int currentBaudrate = serialPort.getConfiguration().getBaudrate();System.out.println("Initial configuration");System.out.printf("%20s : %s\n", "Alias", currentAlias);System.out.printf("%20s : %s\n", "Protocol", currentProtocol);System.out.printf("%20s : %d\n", "Baud Rate", currentBaudrate);SerialportConfig configuration = serialPort.getConfiguration();configuration.setAlias("New Alias");configuration.setProtocol(SerialportProtocol.RS485);configuration.setBaudrate(115200);serialPort.setConfiguration(configuration);Serialport serialPort2 = serialportManager.getSerialPortList().get(0);System.out.println("New configuration");System.out.printf("%20s : %s\n", "Alias", serialPort2.getConfiguration().getAlias());System.out.printf("%20s : %s\n", "Protocol", serialPort2.getConfiguration().getProtocol());System.out.printf("%20s : %d\n", "Baud Rate", serialPort2.getConfiguration().getBaudrate());//Set configuration as it wasSerialportConfig currentConfiguration = serialPort.getConfiguration();currentConfiguration.setAlias(currentAlias);currentConfiguration.setProtocol(currentProtocol);currentConfiguration.setBaudrate(currentBaudrate);serialPort.setConfiguration(currentConfiguration);}} catch (SerialportNotAvailableException e) {e.printStackTrace();} catch (SerialportLowLevelException e) {e.printStackTrace();}}}
-
Subscribe to SerialPort events
package com.gmv.its.dpyjavaexamples.serialport;import java.util.List;import java.io.*;import com.gmv.its.deepsy.hal.CannotSubscribeException;import com.gmv.its.deepsy.hal.serialport.*;import com.gmv.its.deepsy.serialport.*;class MySerialPortObserver extends SerialportObserver {@Overridepublic void notifyMessagereceived(MessagereceivedEvent event) {byte[] data = ((MessagereceivedEvent) event).getData();String portId = ((MessagereceivedEvent) event).getPortid();System.out.println("MessageReceived Event. SerialPort " + portId + ".");System.out.println(String.format("%20s: %s", "New Data", stringToHex(data)));System.out.println(String.format("%20s: %s", "New Data Byte", convertBytesToInt(data)));System.out.println(String.format("%20s: %s\n", "New Data Hex", convertBytesToHex(data)));}public static String stringToHex(byte[] data) {String result = "";for (byte b : data) {if (Character.isISOControl((char)b) || Byte.toUnsignedInt(b) > 127) {result += "\\x" + String.format("%02X", b).toUpperCase();} else {result += (char)b;}}return result;}public static String convertBytesToInt(byte[] bytes) {StringBuilder sb = new StringBuilder();for (int i = 0; i < bytes.length; i++) {sb.append(String.format("%d", Byte.toUnsignedInt(bytes[i])));if (i != bytes.length - 1) {sb.append("-");}}return sb.toString();}public static String convertBytesToHex(byte[] bytes) {StringBuilder sb = new StringBuilder();for (int i = 0; i < bytes.length; i++) {sb.append(String.format("%02x", bytes[i]).toUpperCase());if (i != bytes.length - 1) {sb.append("-");}}return sb.toString();}}public class SubscribeSerialPortEventsApp {public static void main(String[] args) {try (DeepsySerialportManager serialportManager = DeepsySerialportManager.getInstance("192.168.0.50")) {List<Serialport> serialportList = serialportManager.getSerialPortList();System.out.println("There are " + serialportList.size() + " SerialPorts");Serialport serialPort = serialportList.get(0);if (serialPort != null) {serialPort.subscribe(new MySerialPortObserver());System.out.println("Press any key for exit\n");InputStreamReader isr = new InputStreamReader(System.in);BufferedReader br = new BufferedReader(isr);br.readLine();}} catch (SerialportNotAvailableException e) {e.printStackTrace();} catch (SerialportLowLevelException e) {e.printStackTrace();} catch (CannotSubscribeException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();}}}
-
Send Message
package com.gmv.its.dpyjavaexamples.serialport;import java.io.UnsupportedEncodingException;import java.util.List;import com.gmv.its.deepsy.hal.serialport.*;import com.gmv.its.deepsy.serialport.*;import java.io.ByteArrayOutputStream;import java.io.IOException;public class SendSerialPortMessageApp {public static void main(String[] args) {try (DeepsySerialportManager serialportManager = DeepsySerialportManager.getInstance("192.168.1.50")) {List<Serialport> serialportList = serialportManager.getSerialPortList();System.out.println("There are " + serialportList.size() + " SerialPorts");Serialport serialPort = serialportList.get(0);if (serialPort != null) {String message = "Hey\u002C\nhow are you\u003f\nBye.";byte[] dataHead = { 0x05, 0x10, 0x1E };byte[] dataFromStr;try {dataFromStr = message.getBytes("ISO-8859-1");} catch (UnsupportedEncodingException ue) {dataFromStr = message.getBytes();}ByteArrayOutputStream outputStream = new ByteArrayOutputStream();outputStream.write(dataHead);outputStream.write(dataFromStr);byte[] data = outputStream.toByteArray();System.out.println("Message sent to \"" + serialPort.getPortid() + "\":");System.out.println(String.format("%20s: %s", "New Data", stringToHex(data)));System.out.println(String.format("%20s: %s\n", "New Data Hex", convertBytesToHex(data)));serialPort.send(data);}} catch (IOException e) {e.printStackTrace();} catch (SerialportNotAvailableException e) {e.printStackTrace();} catch (SerialportLowLevelException e) {e.printStackTrace();}}public static String stringToHex(byte[] data) {String result = "";for (byte b : data) {if (Character.isISOControl((char)b) || Byte.toUnsignedInt(b) > 127) {result += "\\x" + String.format("%02X", b).toUpperCase();} else {result += (char)b;}}return result;}public static String convertBytesToHex(byte[] bytes) {StringBuilder sb = new StringBuilder();for (int i = 0; i < bytes.length; i++) {sb.append(String.format("%02x", bytes[i]).toUpperCase());if (i != bytes.length - 1) {sb.append("-");}}return sb.toString();}}
-
Send multiple messages using console
package com.gmv.its.dpyjavaexamples.serialport;import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.UnsupportedEncodingException;import java.util.List;import com.gmv.its.deepsy.hal.serialport.*;import com.gmv.its.deepsy.serialport.*;// WARNING// When using console, special characters like '\n', '\t' or unicode ('\u002C')// will be interpreted as literals, so if you write down '\n' in this example// don't expect receive a new line characterpublic class SendSerialPortMessageFromConsoleApp {public static void main(String[] args) {try (DeepsySerialportManager serialportManager = DeepsySerialportManager.getInstance("192.168.0.50")) {List<Serialport> serialportList = serialportManager.getSerialPortList();System.out.println("There are " + serialportList.size() + " SerialPorts");Serialport serialPort = serialportList.get(0);if (serialPort != null) {String portID = serialPort.getPortid();InputStreamReader isr = new InputStreamReader(System.in);BufferedReader br = new BufferedReader(isr);while (true){System.out.println("Write the message to be send to the port: " + portID);System.out.println("\tWrite \"quit\" to close");String message = br.readLine();byte[] data;try {data = message.getBytes("ISO-8859-1");} catch (UnsupportedEncodingException ue) {data = message.getBytes();}System.out.println("Message sent to \"" + serialPort.getPortid() + "\":");System.out.println(String.format("%20s: %s", "New Data", stringToHex(data)));System.out.println(String.format("%20s: %s\n", "New Data Hex", convertBytesToHex(data)));// Send messageserialPort.send(data);}}} catch (SerialportNotAvailableException e) {e.printStackTrace();} catch (SerialportLowLevelException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();}}public static String stringToHex(byte[] data) {String result = "";for (byte b : data) {if (Character.isISOControl((char)b) || Byte.toUnsignedInt(b) > 127) {result += "\\x" + String.format("%02X", b).toUpperCase();} else {result += (char)b;}}return result;}public static String convertBytesToHex(byte[] bytes) {StringBuilder sb = new StringBuilder();for (int i = 0; i < bytes.length; i++) {sb.append(String.format("%02x", bytes[i]).toUpperCase());if (i != bytes.length - 1) {sb.append("-");}}return sb.toString();}}