Embedded cygwin termios test RS485 read write works - JohnHau/mis GitHub Wiki
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<sys/types.h> #include<sys/stat.h> #include<unistd.h> #include<fcntl.h> #include<pthread.h> #include<termios.h> #include<semaphore.h> #include<mqueue.h>
int setopt(int fd) { struct termios newtios,oldtios;
if(tcgetattr(fd,&oldtios)!= 0)
{
perror("tcgetattr failed\n");
exit(EXIT_FAILURE);
}
bzero(&newtios,sizeof(newtios));
newtios.c_cflag |= CLOCAL | CREAD;
newtios.c_cflag &= ~CSIZE;
newtios.c_cflag |= CS8;
newtios.c_cflag &= ~PARENB;
newtios.c_cflag &= ~CSTOPB;
//newtios.c_cflag |= CRTSCTS;
//newtios.c_iflag = IGNPAR | ICRNL;
// newtios.c_iflag = IGNPAR; // newtios.c_oflag = 0; // newtios.c_lflag = 0;
cfsetispeed(&newtios,B115200);
cfsetospeed(&newtios,B115200);
newtios.c_cc[VTIME] = 1;
newtios.c_cc[VMIN] = 1;
tcflush(fd,TCIFLUSH);
tcflush(fd,TCOFLUSH);
if(tcsetattr(fd,TCSANOW,&newtios) != 0)
{
perror("tcsetattr failed\n");
exit(EXIT_FAILURE);
}
return 0;
}
#define UART_NAME "/dev/ttyS7"
int FH_open_uart(const char* name) { int fd;
if((fd = open(name,O_RDWR)) <0)
//if((fd = open(name,O_RDONLY | O_NOCTTY)) <0)
{
perror("FH_open_uart failed\n");
exit(EXIT_FAILURE);
}
return fd;
}
uint8_t in_buffer[256] = {0};
int main(int argc,char* argv[]) { int fd; fd = FH_open_uart(UART_NAME); if(fd < 0) { perror("open uart failed\n"); exit(EXIT_FAILURE); }
printf("fd is %d\n",fd);
printf("reading\n");
int n = read(fd,in_buffer,5);
printf("n is %d\n",n);
printf("in_buffer is %s\n",in_buffer);
write(fd,in_buffer,n);
close(fd);
return 0;
}