假如你的USB串口路径为/dev/ttyUSB0,你要在ttyUSB0上运行bash的话。
首先 配置波特率
stty -F /dev/ttyUSB0 115200
然后 启动bash
runon /dev/ttyUSB0 bash
runon工具源码如下
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
void copy_fd(int from, int to) {
if (from == to) {
fprintf(stderr, "Impossible fd=%d\n", from);
exit(1);
}
close(to);
if (dup2(from, to) != to) {
fprintf(stderr, "dup2(%d, %d) failed\n", from, to);
exit(1);
}
}
void vfork_child(char *argv[]) {
if (vfork() == 0) {
// child
setsid();
ioctl(STDIN_FILENO, TIOCSCTTY, 0);
execvp(argv[0], argv);
exit(1);
}
}
int main(int argc, char *argv[]) {
int fd_iodev = 0;
pid_t rfork = 0;
if (argc < 3) {
printf("runon iodev prog [args...]\n\n");
printf("usage: runon /dev/ttyUSB0 login\n\n");
exit(1);
}
const char* iodev = argv[1];
argc -= 2;
argv += 2;
fd_iodev = open(iodev, O_RDWR);
if (fd_iodev < 0) {
perror(iodev);
exit(1);
}
copy_fd(fd_iodev, STDIN_FILENO);
copy_fd(fd_iodev, STDOUT_FILENO);
copy_fd(fd_iodev, STDERR_FILENO);
close(fd_iodev);
vfork_child(argv);
return 0;
}