配置串口访问权限 linux,如何在Linux上正确设置串口通信

我正在尝试从FPGA板读取数据和向FPGA板写入数据.电路板本身带有一个驱动器,无论何时插入电路板,都会创建一个名为ttyUSB0的终端设备.在FPGA上,实现了异步接收器和发送器,它们似乎有效.

但是,C方似乎存在问题.我一直在使用一些测试向量来测试FPGA是否正在输出正确的信息.我注意到了一些事情:

>设备有时无法正确打开

>终端属性有时无法检索或设置.

>读取有时是非阻塞的,并且不会检索正确的值.

关于程序可能失败的任何建议或意见将非常有帮助.

#include // Standard input/output definitions

#include // String function definitions

#include // UNIX standard function definitions

#include // File control definitions

#include // Error number definitions

#include // POSIX terminal control definitions

int open_port(void){

int fd; // File descriptor for the port

fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY);

if (fd == -1){

fprintf(stderr, "open_port: Unable to open /dev/ttyUSB0 %s\n",strerror(errno));

exit(EXIT_FAILURE);

}

return (fd);

}

int main(void){

int fd = 0; // File descriptor

struct termios options; // Terminal options

fd = open_port(); // Open tty device for RD and WR

fcntl(fd, F_SETFL); // Configure port reading

tcgetattr(fd, &options); // Get the current options for the port

cfsetispeed(&options, B230400); // Set the baud rates to 230400

cfsetospeed(&options, B230400);

options.c_cflag |= (CLOCAL | CREAD); // Enable the receiver and set local mode

options.c_cflag &= ~PARENB; // No parity bit

options.c_cflag &= ~CSTOPB; // 1 stop bit

options.c_cflag &= ~CSIZE; // Mask data size

options.c_cflag |= CS8; // Select 8 data bits

options.c_cflag &= ~CRTSCTS; // Disable hardware flow control

// Enable data to be processed as raw input

options.c_lflag &= ~(ICANON | ECHO | ISIG);

// Set the new attributes

tcsetattr(fd, TCSANOW, &options);

// Simple read and write code here//

// Close file descriptor & exit

close(fd)

return EXIT_SUCCESS

}

UPDATE

我根据第一个答案修改了我的代码.这就是我现在拥有的:

#include // Error number definitions

#include // C99 fixed data types

#include // Standard input/output definitions

#include // C standard library

#include // String function definitions

#include // UNIX standard function definitions

#include // File control definitions

#include // POSIX terminal control definitions

// Open usb-serial port for reading & writing

int open_port(void){

int fd; // File descriptor for the port

fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY);

if (fd == -1){

fprintf(stderr, "open_port: Unable to open /dev/ttyUSB0 %s\n",strerror(errno));

exit(EXIT_FAILURE);

}

return fd;

}

int main(void){

int fd = 0; // File descriptor

struct termios options; // Terminal options

int rc; // Return value

fd = open_port(); // Open tty device for RD and WR

// Get the current options for the port

if((rc = tcgetattr(fd, &options)) < 0){

fprintf(stderr, "failed to get attr: %d, %s\n", fd, strerror(errno));

exit(EXIT_FAILURE);

}

// Set the baud rates to 230400

cfsetispeed(&options, B230400);

// Set the baud rates to 230400

cfsetospeed(&options, B230400);

cfmakeraw(&options);

options.c_cflag |= (CLOCAL | CREAD); // Enable the receiver and set local mode

options.c_cflag &= ~CSTOPB; // 1 stop bit

options.c_cflag &= ~CRTSCTS; // Disable hardware flow control

options.c_cc[VMIN] = 1;

options.c_cc[VTIME] = 2;

// Set the new attributes

if((rc = tcsetattr(fd, TCSANOW, &options)) < 0){

fprintf(stderr, "failed to set attr: %d, %s\n", fd, strerror(errno));

exit(EXIT_FAILURE);

}

// Simple Read/Write Code Here//

// Close file descriptor & exit

close(fd);

return EXIT_SUCCESS;

}

为了澄清,接收器和发送器使用8个数据位,1个停止位,没有奇偶校验位.

解决方法:

你应该删除fcntl(mainfd,F_SETFL)语句,因为它不是必需的并且没有被错误地实现(F_GETFL没有先前完成并缺少第三个参数).

尝试使用cfmakeraw设置非规范模式,因为您的初始化代码不完整:

options->c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP

| INLCR | IGNCR | ICRNL | IXON);

options->c_oflag &= ~OPOST;

对于非规范模式,您还需要定义

options.c_cc[VMIN] = 1;

options.c_cc[VTIME] = 2;

1和2只是建议值.

在所有系统调用后添加返回状态测试.

rc = tcgetattr(mainfd, &options);

if (rc < 0) {

printf("failed to get attr: %d, %s\n", mainfd, strerror(errno));

exit (-3);

}

尝试使用较慢的波特率(例如115200甚至9600)进行测试.

标签:c-3,linux,embedded,serial-port,termios

来源: https://codeday.me/bug/20190520/1142838.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值