1 | #include "stdio.h"
|
2 | #include "string.h"
|
3 | #include "unistd.h"
|
4 | #include "fcntl.h"
|
5 | #include "errno.h"
|
6 | #include "sys/types.h"
|
7 | #include "sys/stat.h"
|
8 | #include "stdlib.h"
|
9 | #include "stdarg.h"
|
10 | #include "termios.h"
|
11 | #include "linux/serial.h"
|
12 |
|
13 | #define TIOCSRS485 0x542F
|
14 |
|
15 | int main(void) {
|
16 | char txBuffer[10];
|
17 | char rxBuffer[10];
|
18 | int fd;
|
19 | struct termios tty_attributes;
|
20 | struct serial_rs485 rs485conf;
|
21 |
|
22 | if ((fd = open("/dev/ttyS0",O_RDWR|O_NOCTTY|O_NONBLOCK))<0) {
|
23 | fprintf (stderr,"Open error on %s\n", strerror(errno));
|
24 | exit(EXIT_FAILURE);
|
25 | } else {
|
26 | tcgetattr(fd,&tty_attributes);
|
27 |
|
28 | // c_cflag
|
29 | // Enable receiver
|
30 | tty_attributes.c_cflag |= CREAD;
|
31 |
|
32 | // 8 data bit
|
33 | tty_attributes.c_cflag |= CS8;
|
34 |
|
35 | // c_iflag
|
36 | // Ignore framing errors and parity errors.
|
37 | tty_attributes.c_iflag |= IGNPAR;
|
38 |
|
39 | // c_lflag
|
40 | // DISABLE canonical mode.
|
41 | // Disables the special characters EOF, EOL, EOL2,
|
42 | // ERASE, KILL, LNEXT, REPRINT, STATUS, and WERASE, and buffers by lines.
|
43 |
|
44 | // DISABLE this: Echo input characters.
|
45 | tty_attributes.c_lflag &= ~(ICANON);
|
46 |
|
47 | tty_attributes.c_lflag &= ~(ECHO);
|
48 |
|
49 | // DISABLE this: If ICANON is also set, the ERASE character erases the preceding input
|
50 | // character, and WERASE erases the preceding word.
|
51 | tty_attributes.c_lflag &= ~(ECHOE);
|
52 |
|
53 | // DISABLE this: When any of the characters INTR, QUIT, SUSP, or DSUSP are received, generate the corresponding signal.
|
54 | tty_attributes.c_lflag &= ~(ISIG);
|
55 |
|
56 | // Minimum number of characters for non-canonical read.
|
57 | tty_attributes.c_cc[VMIN]=1;
|
58 |
|
59 | // Timeout in deciseconds for non-canonical read.
|
60 | tty_attributes.c_cc[VTIME]=0;
|
61 |
|
62 | // Set the baud rate
|
63 | cfsetospeed(&tty_attributes,B300);
|
64 | cfsetispeed(&tty_attributes,B300);
|
65 |
|
66 | tcsetattr(fd, TCSANOW, &tty_attributes);
|
67 |
|
68 | // Set RS485 mode:
|
69 | rs485conf.flags |= SER_RS485_ENABLED;
|
70 | if (ioctl (fd, TIOCSRS485, &rs485conf) < 0) {
|
71 | printf("ioctl error\n");
|
72 | }
|
73 |
|
74 | txBuffer[0]='A';
|
75 | write(fd,txBuffer,1);
|
76 |
|
77 | // Read a char
|
78 | if (read(fd,&rxBuffer,1)==1) {
|
79 | printf("%c",rxBuffer[0]);
|
80 | printf("\n");
|
81 | }
|
82 | }
|
83 |
|
84 | close(fd);
|
85 | return EXIT_SUCCESS;
|
86 | }
|