1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 /* serial port functions
5 * Author: Chris Toshok <toshok@ximian.com>
17 open_serial (char* devfile)
20 struct termios newtio;
22 fd = open (devfile, O_RDWR);
27 newtio.c_cflag = CLOCAL | CREAD;
32 tcflush(fd, TCIOFLUSH);
33 tcsetattr(fd,TCSANOW,&newtio);
35 fcntl (fd, F_SETFL, O_NONBLOCK);
41 close_serial (int unix_fd)
47 read_serial (int fd, guchar *buffer, int offset, int count, int timeout)
53 ufd.events = POLLHUP | POLLIN | POLLERR;
55 poll (&ufd, 1, timeout);
57 if ((ufd.revents & POLLIN) != POLLIN) {
61 n = read (fd, buffer + offset, count);
67 write_serial (int fd, guchar *buffer, int offset, int count, int timeout)
74 ufd.events = POLLHUP | POLLOUT | POLLERR;
76 poll (&ufd, 1, timeout);
78 if ((ufd.revents & POLLOUT) != POLLOUT) {
82 n = write (fd, buffer + offset, count);
86 discard_buffer (int fd, gboolean input)
88 tcflush(fd, input ? TCIFLUSH : TCOFLUSH);
92 set_attributes (int fd, int baud_rate, int parity, int dataBits, int stopBits, int handshake)
94 struct termios newtio;
96 tcgetattr (fd, &newtio);
99 case 230400: baud_rate = B230400; break;
100 case 115200: baud_rate = B115200; break;
101 case 57600: baud_rate = B57600; break;
102 case 38400: baud_rate = B38400; break;
103 case 19200: baud_rate = B19200; break;
104 case 9600: baud_rate = B9600; break;
105 case 4800: baud_rate = B4800; break;
106 case 2400: baud_rate = B2400; break;
107 case 1800: baud_rate = B1800; break;
108 case 1200: baud_rate = B1200; break;
109 case 600: baud_rate = B600; break;
110 case 300: baud_rate = B300; break;
111 case 200: baud_rate = B200; break;
112 case 150: baud_rate = B150; break;
113 case 134: baud_rate = B134; break;
114 case 110: baud_rate = B110; break;
115 case 75: baud_rate = B75; break;
125 newtio.c_iflag &= ~IGNPAR;
126 newtio.c_cflag |= PARENB;
132 newtio.c_iflag |= IGNPAR;
133 newtio.c_cflag &= ~(PARENB | PARODD);
136 newtio.c_iflag &= ~IGNPAR;
137 newtio.c_cflag |= PARENB | PARODD;
144 newtio.c_cflag &= ~CSIZE;
146 case 5: newtio.c_cflag |= CS5; break;
147 case 6: newtio.c_cflag |= CS6; break;
148 case 7: newtio.c_cflag |= CS7; break;
151 newtio.c_cflag |= CS8;
155 newtio.c_cflag &= ~CSTOPB;
158 /* do nothing, the default is one stop bit */
160 case 1: /* OnePointFive */
164 newtio.c_cflag |= CSTOPB;
168 newtio.c_iflag &= ~IXOFF;
169 newtio.c_oflag &= ~IXON;
171 newtio.c_cflag &= ~CRTSCTS;
172 #endif /* def CRTSCTS */
177 case 1: /* RequestToSend (RTS) */
179 newtio.c_cflag |= CRTSCTS;
180 #endif /* def CRTSCTS */
182 case 2: /* RequestToSendXOnXOff (RTS + XON/XOFF) */
184 newtio.c_cflag |= CRTSCTS;
185 #endif /* def CRTSCTS */
187 case 3: /* XOnXOff */
188 newtio.c_iflag |= IXOFF;
189 // newtio.c_oflag |= IXON;
193 cfsetospeed (&newtio, baud_rate);
194 cfsetispeed (&newtio, baud_rate);
196 tcsetattr(fd,TCSADRAIN,&newtio);
202 * mono internals should not be used here.
203 * this serial stuff needs to be implemented with icalls.
204 * make this at least compile until the code is moved elsewhere
205 * defined(linux) is wrong, too
208 list_serial_devices (void)
215 list_serial_devices (void)
219 /* Linux serial files are of the form ttyS[0-9]+ */
220 GSList *l, *list = NULL;
221 GDir* dir = g_dir_open ("/dev", 0, NULL);
222 const char *filename;
225 while ((filename = g_dir_read_name (dir))) {
227 if (!strncmp (filename, "ttyS", 4))
228 list = g_slist_append (list, g_strconcat ("/dev/", filename, NULL));
234 array = mono_array_new (mono_domain_get (), mono_get_string_class (), g_slist_length (list));
235 for (l = list; l; l = l->next) {
236 mono_array_set (array, gpointer, i++, mono_string_new (mono_domain_get (), (char*)l->data));
243 #warning "list_serial_devices isn't ported to this OS"