11 #define NOT_USED(x) (x = x)
13 struct termios original
;
16 * close_serial_port - Close a serial port
18 * @param: the port to be closed
20 * Close the specified serial port and sets the
21 * its termios structure to the default settings.
23 static void close_serial_port(int fd
)
25 tcsetattr(fd
, TCSANOW
, &original
);
30 * open_serial_port - Open a serial port
32 * @param port: the port to be opened
34 * @return On success a file descriptor,
37 static int open_serial_port(const char *port
)
47 fd
= open(port
, O_RDWR
| O_NOCTTY
);
51 ret
= tcgetattr(fd
, &original
);
55 memset(&tio
, 0, sizeof(struct termios
));
56 tio
.c_cflag
= CS8
| CREAD
| CRTSCTS
| CLOCAL
| HUPCL
;
57 tio
.c_iflag
= IGNPAR
| IGNBRK
;
63 /* If it fails, we will use default settings. */
64 cfsetispeed(&tio
, B115200
);
65 cfsetospeed(&tio
, B115200
);
67 ret
= tcflush(fd
, TCIFLUSH
);
71 ret
= tcsetattr(fd
, TCSANOW
, &tio
);
83 int main(int argc
, char *argv
[])
88 char *cmd
, *p
, buf
[128];
94 fprintf(stderr
, "No cmd.\n");
98 fd
= open_serial_port(argv
[1]);
104 for (i
= 0; i
< (int) strlen(cmd
); i
++) {
106 bytes
= write(fd
, &cmd
[i
], (size_t) 1);
110 } else if (bytes
!= 1) {
111 fprintf(stderr
, "Warning wrote %d instead of 1\n", bytes
);
117 memset(p
, '\0', count
);
120 while ((p
- buf
) < (unsigned int) sizeof(buf
) && !strchr(buf
, '%')) {
121 bytes
= read(fd
, p
, count
);
130 close_serial_port(fd
);