public static void main(String args[]) throws InterruptedException { // !! ATTENTION !! // By default, the serial port is configured as a console port // for interacting with the Linux OS shell. If you want to use // the serial port in a software program, you must disable the // OS from using this port. Please see this blog article by // Clayton Smith for step-by-step instructions on how to disable // the OS console for this port: // http://www.irrational.net/2012/04/19/using-the-raspberry-pis-serial-port/ System.out.println("<--Pi4J--> Serial Communication Example ... started."); System.out.println(" ... connect using settings: 38400, N, 8, 1."); System.out.println(" ... data received on serial port should be displayed below."); // create an instance of the serial communications class final Serial serial = SerialFactory.createInstance(); // create and register the serial data listener serial.addListener( new SerialDataListener() { @Override public void dataReceived(SerialDataEvent event) { // print out the data received to the console System.out.print(event.getData()); } }); try { // open the default serial port provided on the GPIO header serial.open(Serial.DEFAULT_COM_PORT, 38400); // continuous loop to keep the program running until the user terminates the program for (; ; ) { try { // write a formatted string to the serial transmit buffer serial.write("CURRENT TIME: %s", new Date().toString()); // write a individual bytes to the serial transmit buffer serial.write((byte) 13); serial.write((byte) 10); // write a simple string to the serial transmit buffer serial.write("Second Line"); // write a individual characters to the serial transmit buffer serial.write('\r'); serial.write('\n'); // write a string terminating with CR+LF to the serial transmit buffer serial.writeln("Third Line"); } catch (IllegalStateException ex) { ex.printStackTrace(); } // wait 1 second before continuing Thread.sleep(1000); } } catch (SerialPortException ex) { System.out.println(" ==>> SERIAL SETUP FAILED : " + ex.getMessage()); return; } }
/** * A Sample Command Line Interface (CLI) for FONA/Arduino * * @param args * @throws InterruptedException */ public static void main(String args[]) throws InterruptedException { if (args.length > 0) { System.out.print("Called with"); for (String s : args) System.out.println(" " + s); System.out.println(); } SampleClient client = new SampleClient(); final ReadWriteFONA fona = new ReadWriteFONA(client); fona.openSerialInput(); fona.startListening(); try { System.out.println("Hit 'Q' to quit."); // System.out.println("Hit [return] when ready to start."); // userInput(""); final Thread me = Thread.currentThread(); Thread userInputThread = new Thread() { public void run() { displayMenu(); boolean loop = true; while (loop) { String userInput = ""; userInput = userInput("So? > "); if ("Q".equalsIgnoreCase(userInput)) loop = false; else { // channel.sendSerial(userInput); // Private if ("?".equals(userInput)) displayMenu(); else if ("b".equals(userInput)) fona.requestBatteryState(); else if ("a".equals(userInput)) fona.requestADC(); else if ("C".equals(userInput)) fona.requestSIMCardNumber(); else if ("i".equals(userInput)) fona.requestRSSI(); else if ("n".equals(userInput)) fona.requestNetworkStatus(); else if ("N".equals(userInput)) fona.requestNumberOfMessage(); else if ("r".equals(userInput)) { String _smsn = userInput("Mess #:"); fona.readMessNum(Integer.parseInt(_smsn)); } else if ("d".equals(userInput)) { String _smsn = userInput("Mess #:"); fona.deleteMessNum(Integer.parseInt(_smsn)); } else if ("s".equals(userInput)) { String to = userInput("Send to > "); String payload = userInput("Message content > "); sendAndWait4Ack(fona, to, payload); } else System.out.println("Duh?"); } } synchronized (me) { me.notify(); } } }; userInputThread.start(); synchronized (me) { me.wait(); } System.out.println("Bye!"); fona.closeChannel(); } catch (SerialPortException ex) { System.out.println(" ==>> Serial Setup failed : " + ex.getMessage()); return; } System.exit(0); }