Android-SerialPort-Tool
Android-SerialPort-Tool copied to clipboard
How to set multiple device path
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
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
I have cloned this project (https://github.com/licheedev/SerialWorker) but error
Do you have any suggestion?
@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;
}
}
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 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
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
@ped2019-dev
DoorSerialWorker and CardSerialWorker are just demo codes, they are not for common usage.
Imitate and impletement your own SerialWorker
s、SandData
s and RecvData
s.
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