Device Access API
Proposal for Java ME 8

Package com.oracle.deviceaccess.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 com.oracle.deviceaccess.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 numerical ID, name, type (interface) and/or properties:

Using its ID
 UART uart = (UART) PeripheralManager.open(14);
 
Using its name and interface
 UART uart = PeripheralManager.open("HOST", UART.class, null);
 
Or (with modem signals control properties),
 ModemUART uart = PeripheralManager.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 Peripheral.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 = PeripheralManager.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); // Procees 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 = PeripheralManager.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.getPeripheral();
                 // Process MODEM hang-up...
             }
         }
     }, ModemSignalsControl.DCD_SIGNAL);
     // Process input and output...
 } catch (IOException ioe) {
     // Handle exception
 }
 
Note that the preceding example is using a try-with-resources statement and that the UART.close(), InputStream.close() and OutputStream.close() methods are automatically invoked by the platform at the end of the statement.

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.

Device Access API
Proposal for Java ME 8