Android-SerialPort-Tool icon indicating copy to clipboard operation
Android-SerialPort-Tool copied to clipboard

How to set multiple device path

Open ped2019-dev opened this issue 5 years ago • 8 comments

Hello

I have some question about set multiple device path. i saw the dropdownlist device path on UI but it select only one device path. So if I adjust your code (don't you dropdownlist selection) below

use both two device path name same time mDevice.setPath("/dev/ttySC0"); // fix device path name in code , don't use dropdownlist selection mDevice.setPath("/dev/ttySC1");

How can I set multiple device path ? (I have two devices run on same time)

Thank you very much for your kindness

ped2019-dev avatar Dec 23 '19 04:12 ped2019-dev

Just create multi SerialPort instances, and make sure each SerialPort reads data on a separate thread. Encapsulate your SerialPort creatation、reading and writing code into a Worker class like this:

https://github.com/licheedev/SerialWorker

Some refactor with Android-SerialPort-Tool

https://github.com/licheedev/SerialWorker/blob/master/serialport_tool_copy/src/main/java/com/licheedev/serialtool/comn/SerialPortManager.java#L39

licheedev avatar Dec 23 '19 05:12 licheedev

I have cloned this project (https://github.com/licheedev/SerialWorker) but error S__11370732

Do you have any suggestion?

ped2019-dev avatar Dec 23 '19 11:12 ped2019-dev

@ped2019-dev Sorry, I forgot to modify this copy demo. New code separates setDevie() and openSerial().

    public SerialPort open(String devicePath, String baudrateString) {

        mSerialWorker.setDevice(devicePath, Integer.parseInt(baudrateString));

        try {
            return mSerialWorker.openSerial();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

licheedev avatar Dec 23 '19 11:12 licheedev

ok , it runs without error but how to use multiple device path name

I tried to use this code same time below

mOpened_machine1 = SerialPortManager.instance.open("/dev/ttySC0","9600") !=null mOpened_machine2 = SerialPortManager.instance.open("/dev/ttySC1","9600") != null

but it can get data from machine1 but cannot get data from machine2.

How to run both device path name in same time ( I used Serial Worker project that have multiple thread )

ped2019-dev avatar Dec 25 '19 03:12 ped2019-dev

@ped2019-dev You should change your mind and write your own code.

Create multi SerialWorker instances at the same time.

https://github.com/licheedev/SerialWorker/blob/661363c3c55cd17596109eb48a0976f1cb3e8d78/app/src/main/java/com/licheedev/serialworkerdemo/serial/SerialManager.java#L74

Open these SerialWorkers later.

https://github.com/licheedev/SerialWorker/blob/661363c3c55cd17596109eb48a0976f1cb3e8d78/app/src/main/java/com/licheedev/serialworkerdemo/serial/SerialManager.java#L124

licheedev avatar Dec 25 '19 03:12 licheedev

ok, I have tried the SerialWorker Project (Door,Card) , it ok for multiple devices ("/dev/ttySC01","/dev/ttySC02") but how to get command and send command in the MainActivity.java in this project , I am trying to use the eventbus but not success

1577257547854

ped2019-dev avatar Dec 25 '19 07:12 ped2019-dev

@ped2019-dev DoorSerialWorker and CardSerialWorker are just demo codes, they are not for common usage. Imitate and impletement your own SerialWorkers、SandDatas and RecvDatas.

licheedev avatar Dec 25 '19 07:12 licheedev

ok, I tried to implement SerialWorker on MainActivity.java (for sending&receiving Command) but not success . for the Android SerialPort Tool master
I can use this command SerialPortManager.instance().sendCommand("02"); to send data

and use

@subscribe public void OnDataReceived(RecvMessage event){ Log.i("get cmd",event.getCommand()); }

for receiving data.

in this project I don't know how to implement SerialWorker in main activity? Do you have any suggestion?

Thank you

ped2019-dev avatar Dec 25 '19 10:12 ped2019-dev