Android 通过C程序串口通信
Android serial communication via C program
我在网络指南上搜索了通过 USB 端口从 Android 设备和 C 程序进行串行通信的内容,但没有发现任何特别之处。有人知道我该如何实现这种沟通吗?
我并不是真的在寻找 Android 项目串口通信的指南,而是通过串口发送或接收数据,允许我从终端读取或发送。
编辑我正在尝试搜索更多内容,但找不到任何特别的内容。
我注意到当我通过 USB 连接我的 Samsung Galaxy SII plus 时,我发现
/dev/ttyACM0
/dev/ttyACM1
这可能与我的 phone(读取写入流?)相关联,因为当 SII 未连接时它们不存在。我通过 Play Story 下载了一个串行监视器应用程序并创建了这个简单的程序来将一些基本文本发送到我的 phone:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
int open_port(void);
int main(void) {
fprintf(stderr, "Starting reading serial port... ");
int port = open_port();
fprintf(stderr, "%d... [DONE]\n", port);
return 0;
}
int open_port(void) {
int fd, n; /* File descriptor for the port */
fd = open("/dev/ttyACM0", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1) {
perror("open_port: Unable to open /dev/ttyS0 - ");
} else {
fcntl(fd, F_SETFL, 0);
}
n = write(fd, "ATZ", 3);
if (n < 0) {
fputs("write() of 4 bytes failed!\n", stderr);
}
return (fd);
}
当我写入数据时没有任何失败,但我的 phone 没有收到任何东西!
您无法从 C 代码打开 Android 上的串行端口,因为 Android 不允许设备直接访问常规应用程序。
所以你必须使用Java。这里有两个 Android 库,用于向您的应用程序添加对 USB 转串口电缆的支持:
https://github.com/mik3y/usb-serial-for-android
https://github.com/ksksue/FTDriver
这是一个开源应用程序,它使用以下库之一:
https://play.google.com/store/apps/details?id=jp.ksksue.app.terminal&hl=en
但是如果您在 Android 上获得了 root,您可以使用与在 Linux 上使用的相同的 C 代码通过串行通信,您甚至可以在 echo "data data" > /dev/ttyACM0
Android shell.
我在网络指南上搜索了通过 USB 端口从 Android 设备和 C 程序进行串行通信的内容,但没有发现任何特别之处。有人知道我该如何实现这种沟通吗?
我并不是真的在寻找 Android 项目串口通信的指南,而是通过串口发送或接收数据,允许我从终端读取或发送。
编辑我正在尝试搜索更多内容,但找不到任何特别的内容。
我注意到当我通过 USB 连接我的 Samsung Galaxy SII plus 时,我发现
/dev/ttyACM0
/dev/ttyACM1
这可能与我的 phone(读取写入流?)相关联,因为当 SII 未连接时它们不存在。我通过 Play Story 下载了一个串行监视器应用程序并创建了这个简单的程序来将一些基本文本发送到我的 phone:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
int open_port(void);
int main(void) {
fprintf(stderr, "Starting reading serial port... ");
int port = open_port();
fprintf(stderr, "%d... [DONE]\n", port);
return 0;
}
int open_port(void) {
int fd, n; /* File descriptor for the port */
fd = open("/dev/ttyACM0", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1) {
perror("open_port: Unable to open /dev/ttyS0 - ");
} else {
fcntl(fd, F_SETFL, 0);
}
n = write(fd, "ATZ", 3);
if (n < 0) {
fputs("write() of 4 bytes failed!\n", stderr);
}
return (fd);
}
当我写入数据时没有任何失败,但我的 phone 没有收到任何东西!
您无法从 C 代码打开 Android 上的串行端口,因为 Android 不允许设备直接访问常规应用程序。
所以你必须使用Java。这里有两个 Android 库,用于向您的应用程序添加对 USB 转串口电缆的支持:
https://github.com/mik3y/usb-serial-for-android
https://github.com/ksksue/FTDriver
这是一个开源应用程序,它使用以下库之一:
https://play.google.com/store/apps/details?id=jp.ksksue.app.terminal&hl=en
但是如果您在 Android 上获得了 root,您可以使用与在 Linux 上使用的相同的 C 代码通过串行通信,您甚至可以在 echo "data data" > /dev/ttyACM0
Android shell.