c++ - Read/Write bytes within two processes (apps) using virtual serial communication on Android -
i have written c/c++ application (ndk) android can open virtual port serial communication. writes , reads bytes port within same process. far working , didn´t have root device in order this. simple programm following:
int fd = open("/dev/ptmx", o_rdwr | o_noctty); if (fd == -1) { logw("error opening file"); return 1; } grantpt(fd); unlockpt(fd); char* pts_name = ptsname(fd); logi("ptsname: %s\n", pts_name); char* inputbyte = "this test\n"; char byte; int numofbyteswritten = write(fd, inputbyte, strlen(inputbyte)); logi("number of bytes written: %d\n", numofbyteswritten); while (read(fd, &byte, 1) == 1) { logi("byte: %c\n", byte); } close(fd);
now problem if want same thing within 2 processes (two apps) doesn´t work. 1 process opens /dev/ptmx, , other process should open /dev/pts directory. everytime slave tries open /dev/pts directory error "error opening file" (-1). have rooted device , still not working. have sleep function after opening /dev/ptmx directory in order wait slave.
what should do? ideas?
i have solved problem post solution in case else interested:
(1) phone should rooted.
(2) master opens port in form "/dev/pts/n" (variable pts_name in example) n number. port given by:
int fd = open("/dev/ptmx", o_rdwr | o_noctty); if (fd == -1) { logw("error opening file"); return 1; } char* pts_name = ptsname(fd); logi("ptsname: %s\n", pts_name);
(3) give permissions port (this can done programmatically or adb shell). shell be:
- su
- chmod 666 pts_name (for example chmod 666 /dev/pts/4).
(4) execute slave opens port. example int fd = open("/dev/pts/4", o_rdwr);
(5) voilĂ !
Comments
Post a Comment