帮我看下这个linux的串口读写程序,可以写出去,但是读不到
#include<stdio.h>#include<stdlib.h>#include<unistd.h>#include<sys/types.h>#include<sy...
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <string.h>
#define FALSE -1
#define TRUE 0
int main(){
//open port
int fd = open("/dev/ttyACM0", O_RDWR | O_NOCTTY | O_NDELAY);
if(FALSE == fd){
perror("cant't open port\n");
exit(1);
}
if(fcntl(fd, F_SETFL, 0) < 0){
printf("fcntl faild\n");
exit(1);
}else{
printf("fcntl=%d\n", fcntl(fd, F_SETFL, 0));
}
if(0 == isatty(STDIN_FILENO)){
printf("standard input is not a terminal device\n");
exit(1);
}else{
printf("isatty success\n");
}
//set port
struct termios options;
if(tcgetattr(fd, &options) != 0){
perror("SetupSerial 1");
exit(1);
}
cfsetispeed(&options, 9600);
cfsetospeed(&options, 9600);
options.c_cflag |= CLOCAL;
options.c_cflag |= CREAD;
options.c_cflag |= CRTSCTS;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
options.c_cflag |= PARENB;
options.c_cflag &= ~PARODD;
options.c_iflag |= INPCK;
options.c_cflag &= ~CSTOPB;
tcsetattr(fd, TCSANOW, &options);
//send data
int sendLen = write(fd, "hello", 5);
printf("send: %d\n", sendLen);
tcflush(fd, TCOFLUSH);
//read data
char readC[5];
read(fd, readC, 5);
printf("%s\n", readC);
close(fd);
} 展开
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <string.h>
#define FALSE -1
#define TRUE 0
int main(){
//open port
int fd = open("/dev/ttyACM0", O_RDWR | O_NOCTTY | O_NDELAY);
if(FALSE == fd){
perror("cant't open port\n");
exit(1);
}
if(fcntl(fd, F_SETFL, 0) < 0){
printf("fcntl faild\n");
exit(1);
}else{
printf("fcntl=%d\n", fcntl(fd, F_SETFL, 0));
}
if(0 == isatty(STDIN_FILENO)){
printf("standard input is not a terminal device\n");
exit(1);
}else{
printf("isatty success\n");
}
//set port
struct termios options;
if(tcgetattr(fd, &options) != 0){
perror("SetupSerial 1");
exit(1);
}
cfsetispeed(&options, 9600);
cfsetospeed(&options, 9600);
options.c_cflag |= CLOCAL;
options.c_cflag |= CREAD;
options.c_cflag |= CRTSCTS;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
options.c_cflag |= PARENB;
options.c_cflag &= ~PARODD;
options.c_iflag |= INPCK;
options.c_cflag &= ~CSTOPB;
tcsetattr(fd, TCSANOW, &options);
//send data
int sendLen = write(fd, "hello", 5);
printf("send: %d\n", sendLen);
tcflush(fd, TCOFLUSH);
//read data
char readC[5];
read(fd, readC, 5);
printf("%s\n", readC);
close(fd);
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询