2.Cannot open /dev/ttyS0: Permission denied解决
出现了Cannot open /dev/ttyS0: Permission denied提示字样
解决方法如下:
1.由于tty属于“dialout”组别,比如用户名是admin123,
先命令查看下用户隶属的组别
groups admin123
2.如果没有隶属“dialout”,那么把用户加入进去
sudo gpasswd --add admin123 dialout
3.logout 再登录系统激活功能
3.linux在shell下的串口的一些操作
echo aaa > /dev/ttyS0 | 向com1写数据 |
---|---|
cat /dev/ttyS0 | 向com1读数据 |
cat /proc/tty/drivers/serial | 查看串口信息 |
stty -F | 设置串口参数 |
stty linux 命令在线中文手册
Linux深入探索01-stty与键盘信号
#include <termios.h>
#include <unistd.h>
// 最核心的配置结构体
typedef unsigned int tcflag_t;
struct termios {
tcflag_t c_cflag; /* 控制标志 */
tcflag_t c_iflag; /* 输入标志 */
tcflag_t c_oflag; /* 输出标志 */
tcflag_t c_lflag; /* 本地标志 */
tcflag_t c_cc[NCCS]; /* 控制字符 */
};
fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY);
struct termios opt;
tcgetattr(fd, &opt); // 获取上述配置结构体
// 设置波特率相关接口
speed_t cfgetispeed(const struct termios *termios_p);
speed_t cfgetospeed(const struct termios *termios_p);
int cfsetispeed(struct termios *termios_p, speed_t speed);
int cfsetospeed(struct termios *termios_p, speed_t speed);
int cfsetspeed(struct termios *termios_p, speed_t speed);
// 设置奇偶校验位
opt.c_cflag &= ~PARENB;
// 设置数据位
opt.c_cflag &= ~CSIZE; // 先用 CSIZE 来做屏蔽字段
opt.c_cflag |= CS8;
// 设置停止位
opt.c_cflag &= ~CSTOPB;
// 设置硬件控制流
opt.c_cflag |= CRTSCTS;
// 抛弃存储在 fd 里的未接收的数据
tcflush(fd, TCIFLUSH);
// 将配置信息写入文件描述符 fd
tcsetattr(fd, TCSANOW, &opt);