Step 7: read the C++ program output as input to TotalCross application
// Input from programString input;try {// Read line by line the buffered streamLineReader lineReader =newLineReader(Stream.asStream(process.getInputStream()));while ((input =lineReader.readLine()) !=null) {label.setText(input); }} catch (IOException ioe) {ioe.printStackTrace();};// Add label to windowadd(label, CENTER, CENTER);
Step 8: run TotalCross: Package with VS Code plugin or run mvn package in your terminal.
Step 9: copy C++ binary to target folder (something like):
Step 10: run your program!!!
See more
See our article about how to run RS232 protocol. See the full code:
packagecom.totalcross;importjava.io.IOException; importjava.lang.Process;importtotalcross.ui.Label; importtotalcross.ui.MainWindow; importtotalcross.ui.gfx.Color; importtotalcross.io.LineReader; importtotalcross.io.Stream; importtotalcross.sys.Settings;publicclassRunningCPPextendsMainWindow {publicRunningCPP() {setUIStyle(Settings.MATERIAL_UI); } @OverridepublicvoidinitUI() {// Label to show the resultsLabel label; label =newLabel();label.setBackForeColors(Color.WHITE,Color.BLACK);// Process initializationProcess process =null;// Output to programbyte[] output ="Take the output!!!\n".getBytes(); // convert string to// byte array try { process =Runtime.getRuntime().exec("./io"); // execute your// application (sh like)process.getOutputStream().write(output,0,output.length); // write output into // output stremprocess.waitFor(); // blocking method // (wait io finish) } catch (IOException ioe) {ioe.printStackTrace(); } catch (InterruptedException ie) {ie.printStackTrace(); };// Input from programString input;try {LineReader lineReader =newLineReader(Stream.asStream(process.getInputStream()));while ((input =lineReader.readLine()) !=null) {label.setText(input); } } catch (IOException ioe) {ioe.printStackTrace(); };// Add label to windowadd(label, CENTER, CENTER); }}