Serial port write()到/dev/ttyam0始终返回0
我正在尝试使用RPi串行接口。 尝试写入/dev/ttyam0时调用write()始终使用以下代码返回0Serial port write()到/dev/ttyam0始终返回0,serial-port,raspberry-pi,Serial Port,Raspberry Pi,我正在尝试使用RPi串行接口。 尝试写入/dev/ttyam0时调用write()始终使用以下代码返回0 int main(int argc,char **argv) { struct termios cur,bak; int fd,count; char *tx_buf; tx_buf = (char*) malloc(20); fd = open("/dev/ttyAMA0",O_RDWR | O_NOCTTY | O_NDELAY); tcgetattr(fd,&
int main(int argc,char **argv) {
struct termios cur,bak;
int fd,count;
char *tx_buf;
tx_buf = (char*) malloc(20);
fd = open("/dev/ttyAMA0",O_RDWR | O_NOCTTY | O_NDELAY);
tcgetattr(fd,&cur); /* store current attribute */
bak = cur; /* backup structure */
cur.c_cflag = B9600 | CS8 | CLOCAL | CREAD;
cur.c_iflag = IGNPAR;
cur.c_oflag = 0;
cur.c_lflag = 0;
tcflush(fd,TCIFLUSH);
tcsetattr(fd,TCSANOW,&cur);
/* TX */
strncpy(tx_buf,"hello",5);
count = write(fd,tx_buf,5);
printf("wrotten:%5d\n",count);
tcsetattr(fd,TCSANOW,&bak);
close(fd);
return 0;
}
有什么问题吗?我应该从哪里开始解决问题?尝试从打开的系统调用中删除
O\u NDELAY
选项;i、 e.执行阻塞而不是非阻塞写入。或者在write()
之后插入一个tcdrain()
系统调用。如果不需要,请不要使用非阻塞I/O。终端初始化代码可能对原始作者有效,但它不可移植。请参阅并尝试从打开的系统调用中删除O\u NDELAY
选项;i、 e.执行阻塞而不是非阻塞写入。或者在write()
之后插入一个tcdrain()
系统调用。如果不需要,请不要使用非阻塞I/O。终端初始化代码可能对原始作者有效,但它不可移植。看到和