Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
gcreate edited this page Jan 29, 2015 · 1 revision

Send string command to serial port device,wait for result ,check result and send new string command to serial port device

package testcase;

import serialportutil.AbstractSerialCommand;
import serialportutil.CommandBatch;
import serialportutil.SerialPortConf;
import serialportutil.StringOutputStream;

/**
 * Condition check <BR>
 * 1. Send command 'ls /system/bin' <BR>
 * 2. Check result <BR>
 * 3. If result include 'ps', Send command 'ps |grep sh'
 * 
 */
public class ConditionCheck extends AbstractSerialCommand {
    private StringOutputStream stringOutput = new StringOutputStream();

    @Override
    protected void setup() {
        setSerialPortConf(new SerialPortConf("COM15", 115200, 8, 1, 0));
        addOutputStream(stringOutput);
    }

    @Override
    protected void processCommand() throws Exception {

        sendCommand("ls /system/bin\n", 1000, 1);
        // Get result
        String[] items = stringOutput.toStrings();
        boolean continueProcess = false;
        for (String item : items) {
            if (item.indexOf("ps") >= 0) {
                continueProcess = true;
                System.out.println("Has 'ps' command !");
                break;
            }
        }
        if (!continueProcess) {
            return;
        }

        // Run command 'ps |grep sh' and get new result
        System.out.println("Execute 'ps |grep sh' command !");
        stringOutput.clear();
        sendCommand("ps |grep sh\n", 1000, 1);
        System.out.println(stringOutput.toString());
    }

    public static void main(String[] args) {
        CommandBatch.addClazz(ConditionCheck.class);
        CommandBatch.go();
    }
}

Output

Start>>class testcase.ConditionCheck
Connecting to COM15 [speed:115200] [databit:8] [stopbit:1] [paritybit:0]
Connected!
Has 'ps' command !
Execute 'ps |grep sh' command !
ps |grep sh
root      715   2     0      0     bf3ecc88 00000000 D Flash
shell     759   1     968    508   c002c9c0 b6f8e378 S /system/bin/sh
root      763   1     940    444   c002c9c0 b6ea1378 S /system/bin/sh
root      14156 759   956    484   c002c9c0 b6f46378 S sh
shell@root:/data # 
Close:class serialportutil.StringOutputStream
Disconnected from COM15
End<<<<class testcase.ConditionCheck

Clone this wiki locally

Morty Proxy This is a proxified and sanitized view of the page, visit original site.