import java.util.*; import jdk.dio.*; import jdk.dio.uart.*; import java.io.*; import java.nio.*; import java.nio.charset.*; public class TestUart { private static UART uart = null; private static TimerTask readTask = new TimerTask() { public void run() { ByteBuffer buffer = ByteBuffer.allocate(32); try { System.out.println("Reading from UART"); uart.read(buffer); } catch (Exception e) { System.out.println("RT: Caught exception: " + e); } System.out.println("Read: " + new String(buffer.array())); } }; private static TimerTask writeTask = new TimerTask() { private int pass = 0; public void run() { String msg = "Hello " + pass++; ByteBuffer buffer = ByteBuffer.wrap(msg.getBytes()); try { uart.write(buffer); } catch (Exception e) { System.out.println("WT: Caught exception: " + e); } System.out.println("Wrote: " + new String(buffer.array()));//buffer.asCharBuffer()); } }; public static void main(String[] args) { try { uart = (UART)DeviceManager.open(40); // 40 = UART device in dio.properties } catch (Exception e) { System.out.println("Caught exception: " + e); } System.out.println("Available charsets are: " + Charset.availableCharsets()); Timer wt = new Timer(); wt.schedule(writeTask, 0, 1000); Timer rt = new Timer(); rt.scheduleAtFixedRate(readTask, 500, 1000); try { Thread.sleep(30000); } catch (Exception e) { System.out.println("Ignoring: " + e); } rt.cancel(); try { Thread.sleep(5000); } catch (Exception e) { System.out.println("Ignoring: " + e); } wt.cancel(); try { uart.close(); } catch (Exception e) { System.out.println("Caught exception: " + e); } } }