Device I/O API 1.1

Package jdk.dio.uart

Interfaces and classes for controlling and reading and writing from/to Universal Asynchronous Receiver/Transmitter (UART), with optional Modem signals control.

See: Description

Package jdk.dio.uart Description

Interfaces and classes for controlling and reading and writing from/to Universal Asynchronous Receiver/Transmitter (UART), with optional Modem signals control.

In order to access and control a specific UART device, an application should first open and obtain an UART instance for the UART device using its numeric ID, name, type (interface) and/or properties:

Using its ID
 UART uart = (UART) DeviceManager.open(14);
 
Using its name and interface
 UART uart = DeviceManager.open("HOST", UART.class, null);
 
Or (with modem signals control properties),
 ModemUART uart = DeviceManager.open("MODEM", ModemUART.class, null);
 

Once opened, an application can read the received data bytes and write the data bytes to be transmitted through the UART using methods of the ByteChannel interface.

 ByteBuffer buffer = new ByteBuffer();
 ...
 uart.read(buffer);
 ...
 uart.write(buffer);
 ...
 

When done, the application should call the Device.close() method to close the UART device.

 uart.close();
 

The following sample code gives an example of using the UART API to communicate with some host terminal:

 try (UART host = DeviceManager.open("HOST", UART.class, (String) null);
         InputStream is = Channels.newInputStream(host);
         OutputStream os = Channels.newOutputStream(host)) {
     StringBuffer cmd = new StringBuffer();
     int c = 0;
     while (true) {
         os.write('$');
         os.write(' '); // echo prompt
         while (c != '\n' && c != '\003') { // echo input
             c = is.read();
             os.write(c);
             cmd.append(c);
         }
         if (c == '\003') { // CTL-C
             break;
         }
         process(cmd); // Process the command
     }
 } catch (IOException ioe) {
     // Handle exception
 }
 

The following sample codes give examples of using the ModemUART API to additionally control the MODEM signals:

 try (ModemUART modem = DeviceManager.open("HOST", ModemUART.class, (String) null);
         InputStream is = Channels.newInputStream(modem);
         OutputStream os = Channels.newOutputStream(modem)) {
     modem.setSignalChangeListener(new ModemSignalListener<ModemUART>() {
 
         @Override
         public void signalStateChanged(ModemSignalEvent<ModemUART> event) {
             if (event.getSignalState() == false) {
                 ModemUART modem = event.getDevice();
                 // Process MODEM hang-up...
             }
         }
     }, ModemSignalsControl.DCD_SIGNAL);
     // Process input and output...
 } catch (IOException ioe) {
     // Handle exception
 }
 

The preceding example is using a try-with-resources statement; the UART.close, InputStream.close and OutputStream.close methods are automatically invoked by the platform at the end of the statement.

Unless otherwise noted, permission and security checks that may cause a SecurityException to be thrown must be performed in priority to any other checks or operations once performed the checking of the input parameters from which the permission target names and action lists are retrieved and assembled.

Unless otherwise noted, passing a null argument to a constructor or method in any class or interface in this package will cause a NullPointerException to be thrown.

This package requires the jdk.dio.modem package.

Since:
1.0
Device I/O API 1.1