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;public class ListSerialPortApp {public static void main(String[] args) {try (DeepsySerialportManager serialportManager = DeepsySerialportManager.getInstance("192.168.0.50")) {List<Serialport> serialportList = serialportManager.getSerialPortList();for (Serialport s : serialportList) {System.out.printf("Serial Port id : %s\n", s.getPortid());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;public class SetSerialPortConfigurationApp {public static void main(String[] args) {try (DeepsySerialportManager serialportManager = DeepsySerialportManager.getInstance("192.168.0.50")) {List<Serialport> serialportList = serialportManager.getSerialPortList();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");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");//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.*;class MySerialPortObserver extends SerialportObserver {@Overridepublic void notifyMessagereceived(MessagereceivedEvent event) {byte[] data = ((MessagereceivedEvent) event).getData();String portId = ((MessagereceivedEvent) event).getPortid();}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();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 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();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();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;// 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();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();if (message.equals("quit")) break;byte[] data;try {data = message.getBytes("ISO-8859-1");} catch (UnsupportedEncodingException ue) {data = message.getBytes();}// 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();}}