|
发表于 2015-7-21 00:49:44
|
显示全部楼层
我写的串口通信的代码,只能发出去,arduino也能接收到,就是arduino发送回来的,这边接收不到,请大神帮忙看下代码
#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_cc[VTIME] = 0;
options.c_cc[VMIN] = 0;
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, "hello123", 9);
printf("send: %d\n", sendLen);
tcflush(fd, TCOFLUSH);
//read data
char readC[5];
sleep(100);
read(fd, readC, 5);
printf("%s\n", readC);
close(fd);
} |
|