added rs232 handler
[nao-ulib.git] / src / rs232.c
bloba27cca5e9511a3b584fffdec486274d9065b1a85
1 /*
2 * nao-ulib
3 * Copyright 2011 Daniel Borkmann <dborkma@tik.ee.ethz.ch>
4 * Subject to the GPL.
5 * Nao-Team HTWK,
6 * Faculty of Computer Science, Mathematics and Natural Sciences,
7 * Leipzig University of Applied Sciences (HTWK Leipzig)
8 */
10 #include <stdio.h>
11 #include <unistd.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <fcntl.h>
15 #include <termios.h>
17 #include "rs232.h"
18 #include "die.h"
20 int open_sport(int number)
22 int fd;
23 char port_name[64];
24 struct termios term;
26 if (number < 0)
27 return -EINVAL;
29 memset(port_name, 0, sizeof(port_name));
30 sprintf(port_name, "/dev/ttyS%d", number);
31 info("Opening port %s!\n", port_name);
33 fd = open(port_name, O_RDWR | O_NOCTTY | O_NDELAY);
34 if (fd < 0)
35 panic("Cannot open serial port!\n");
37 tcgetattr(fd, &term);
38 tcflush(fd, TCIFLUSH);
40 term.c_cflag = B9600 | CS8 |CREAD | CLOCAL | HUPCL;
42 cfsetospeed(&term, B9600);
43 tcsetattr(fd, TCSANOW, &term);
45 info("cflag=%08x\n", term.c_cflag);
46 info("oflag=%08x\n", term.c_oflag);
47 info("iflag=%08x\n", term.c_iflag);
48 info("lflag=%08x\n", term.c_lflag);
49 info("line=%02x\n", term.c_line);
51 return fd;
54 ssize_t write_sport(int fd, char *out, size_t len)
56 return write(fd, out, len);
59 ssize_t read_sport(int fd, char *in, size_t len)
61 return read(fd, in, len);
64 void close_sport(int fd)
66 close(fd);