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>
19 open_serial (char* devfile)
22 struct termios newtio;
24 fd = open (devfile, O_RDWR);
29 newtio.c_cflag = CLOCAL | CREAD;
34 tcflush(fd, TCIOFLUSH);
35 tcsetattr(fd,TCSANOW,&newtio);
37 fcntl (fd, F_SETFL, O_NONBLOCK);
43 close_serial (int unix_fd)
49 read_serial (int fd, guchar *buffer, int offset, int count, int timeout)
55 ufd.events = POLLHUP | POLLIN | POLLERR;
57 poll (&ufd, 1, timeout);
59 if ((ufd.revents & POLLIN) != POLLIN) {
63 n = read (fd, buffer + offset, count);
69 write_serial (int fd, guchar *buffer, int offset, int count, int timeout)
76 ufd.events = POLLHUP | POLLOUT | POLLERR;
78 poll (&ufd, 1, timeout);
80 if ((ufd.revents & POLLOUT) != POLLOUT) {
84 n = write (fd, buffer + offset, count);
88 discard_buffer (int fd, gboolean input)
90 tcflush(fd, input ? TCIFLUSH : TCOFLUSH);
94 set_attributes (int fd, int baud_rate, MonoParity parity, int dataBits, MonoStopBits stopBits, MonoHandshake handshake)
96 struct termios newtio;
98 tcgetattr (fd, &newtio);
101 case 230400: baud_rate = B230400; break;
102 case 115200: baud_rate = B115200; break;
103 case 57600: baud_rate = B57600; break;
104 case 38400: baud_rate = B38400; break;
105 case 19200: baud_rate = B19200; break;
106 case 9600: baud_rate = B9600; break;
107 case 4800: baud_rate = B4800; break;
108 case 2400: baud_rate = B2400; break;
109 case 1800: baud_rate = B1800; break;
110 case 1200: baud_rate = B1200; break;
111 case 600: baud_rate = B600; break;
112 case 300: baud_rate = B300; break;
113 case 200: baud_rate = B200; break;
114 case 150: baud_rate = B150; break;
115 case 134: baud_rate = B134; break;
116 case 110: baud_rate = B110; break;
117 case 75: baud_rate = B75; break;
126 case NoneParity: /* None */
127 newtio.c_iflag |= IGNPAR;
128 newtio.c_cflag &= ~(PARENB | PARODD);
131 newtio.c_iflag &= ~IGNPAR;
132 newtio.c_cflag |= PARENB | PARODD;
134 case Even: /* Even */
135 newtio.c_iflag &= ~IGNPAR;
136 newtio.c_cflag |= PARENB;
138 case Mark: /* Mark */
141 case Space: /* Space */
146 newtio.c_cflag &= ~CSIZE;
148 case 5: newtio.c_cflag |= CS5; break;
149 case 6: newtio.c_cflag |= CS6; break;
150 case 7: newtio.c_cflag |= CS7; break;
153 newtio.c_cflag |= CS8;
157 newtio.c_cflag &= ~CSTOPB;
163 /* do nothing, the default is one stop bit */
166 newtio.c_cflag |= CSTOPB;
168 case OnePointFive: /* OnePointFive */
173 newtio.c_iflag &= ~IXOFF;
174 newtio.c_oflag &= ~IXON;
176 newtio.c_cflag &= ~CRTSCTS;
177 #endif /* def CRTSCTS */
179 case NoneHandshake: /* None */
182 case XOnXOff: /* XOnXOff */
183 newtio.c_iflag |= IXOFF;
184 // newtio.c_oflag |= IXON;
186 case RequestToSend: /* RequestToSend (RTS) */
188 newtio.c_cflag |= CRTSCTS;
189 #endif /* def CRTSCTS */
191 case RequestToSendXOnXOff: /* RequestToSendXOnXOff (RTS + XON/XOFF) */
193 newtio.c_cflag |= CRTSCTS;
194 #endif /* def CRTSCTS */
198 if (cfsetospeed (&newtio, baud_rate) < 0 || cfsetispeed (&newtio, baud_rate) < 0 ||
199 tcsetattr (fd, TCSADRAIN, &newtio) < 0)
206 * mono internals should not be used here.
207 * this serial stuff needs to be implemented with icalls.
208 * make this at least compile until the code is moved elsewhere
209 * defined(linux) is wrong, too
212 list_serial_devices (void)
219 list_serial_devices (void)
223 /* Linux serial files are of the form ttyS[0-9]+ */
224 GSList *l, *list = NULL;
225 GDir* dir = g_dir_open ("/dev", 0, NULL);
226 const char *filename;
229 while ((filename = g_dir_read_name (dir))) {
231 if (!strncmp (filename, "ttyS", 4))
232 list = g_slist_append (list, g_strconcat ("/dev/", filename, NULL));
238 array = mono_array_new (mono_domain_get (), mono_get_string_class (), g_slist_length (list));
239 for (l = list; l; l = l->next) {
240 mono_array_set (array, gpointer, i++, mono_string_new (mono_domain_get (), (char*)l->data));
247 #warning "list_serial_devices isn't ported to this OS"