Page 993 - 3-2
P. 993
// OS from using this port.
//
// Please see this blog article for instructions on how to disable
// the OS console for this port:
/ /
https://www.cube-controls.com/2015/11/02/disable-serial-port-terminal-output-on-raspbian/
// create Pi4J console wrapper/helper
// (This is a utility class to abstract some of the boilerplate code)
final Console console = new Console();
// print program title/header
console.title("<-- The Pi4J Project -->", "Serial Communication Example");
// allow for user to exit program using CTRL-C
console.promptForExit();
// create an instance of the serial communications class
// create and register the serial data listener
serial.addListener(new SerialDataEventListener() {
@Override
public void dataReceived(SerialDataEvent event) {
// NOTE! - It is extremely important to read the data received from the
// serial port. If it does not get read from the receive buffer, the
// buffer will continue to grow and consume memory.
// print out the data received to the console
try {
console.println("[HEX DATA] " + event.getHexByteString());
console.println("[ASCII DATA] " + event.getAsciiString());
} catch (IOException e) {
e.printStackTrace();
}
- 993 -