14 #include "../include/tscope5/serialport_internal.h"
15 #include "../include/tscope5/system_internal.h"
16 #include "../include/tscope5/timer_internal.h"
19 #include <sys/types.h>
20 #include <sys/ioctl.h>
42 ts5_log(TS5_LOGLEVEL_6,
"%s: ts5_check_serialport\n", calling_function);
46 _ts5_status.timer.serialport_is_trigger_device = 1;
63 ts5_log(TS5_LOGLEVEL_6,
"%s: ts5_check_serialport2(%s)\n",
64 calling_function, portname);
70 for (i=0; i<_ts5_status.timer.num_serialports; i++) {
71 if (!strcmp(portname, _ts5_status.timer.serialport[i].portname)) {
80 port = (TS5_SERIALPORT *)al_malloc(
sizeof(TS5_SERIALPORT));
83 *port = CreateFile(portname,
84 GENERIC_READ | GENERIC_WRITE,
85 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
87 if (*port == INVALID_HANDLE_VALUE) {
88 ts5_fatal(
"%s: could not open serial port %s\n",
89 calling_function, portname);
93 *port = open(portname, O_RDWR | O_NOCTTY | O_NDELAY);
96 ts5_fatal(
"%s: could not open serial port %s\n",
97 calling_function, portname);
101 if (ioctl(*port, TIOCEXCL) == -1) {
102 ts5_fatal(
"%s: could not get exclusivity on serial port %s\n",
103 calling_function, portname);
109 _ts5_status.timer.num_serialports++;
110 portnum = _ts5_status.timer.num_serialports - 1;
113 TS5_SERIALPORT_STATUS *serialport_status;
114 serialport_status = al_realloc(_ts5_status.timer.serialport,
115 _ts5_status.timer.num_serialports *
sizeof(TS5_SERIALPORT_STATUS));
117 if (serialport_status == NULL) {
119 "failed to allocate memory for serial port settings");
122 _ts5_status.timer.serialport =
123 (TS5_SERIALPORT_STATUS *) serialport_status;
126 strcpy(_ts5_status.timer.serialport[portnum].portname, portname);
127 _ts5_status.timer.serialport[portnum].port = port;
128 _ts5_status.timer.serialport[portnum].is_trigger_input_device = 0;
129 _ts5_status.timer.serialport[portnum].is_trigger_output_device = 0;
130 _ts5_status.timer.serialport[portnum].simulate_trigger_input = 0;
131 _ts5_status.timer.serialport[portnum].trigger_simulation_interval = 2.0;
132 _ts5_status.timer.serialport[portnum].last_trigger_simulation =
157 ts5_log(TS5_LOGLEVEL_1,
"%s: Installing Tscope5 serialport\n",
160 al_init_user_event_source(
161 &_ts5_data.timer.serialport_trigger_event_source);
163 al_register_event_source(_ts5_data.timer.trigger_queue,
164 &_ts5_data.timer.serialport_trigger_event_source);
166 al_register_event_source(_ts5_data.timer.trigger_log,
167 &_ts5_data.timer.serialport_trigger_event_source);
169 _ts5_data.timer.serialport_thread =
172 al_start_thread(_ts5_data.timer.serialport_thread);
188 ts5_log(TS5_LOGLEVEL_1,
"Uninstalling Tscope5 serialport\n");
191 al_join_thread(_ts5_data.timer.serialport_thread, NULL);
192 al_destroy_thread(_ts5_data.timer.serialport_thread);
195 al_unregister_event_source(_ts5_data.timer.trigger_queue,
196 &_ts5_data.timer.serialport_trigger_event_source);
198 al_unregister_event_source(_ts5_data.timer.trigger_log,
199 &_ts5_data.timer.serialport_trigger_event_source);
202 al_destroy_user_event_source(
203 &_ts5_data.timer.serialport_trigger_event_source);
207 for (i=0; i<_ts5_status.timer.num_serialports; i++) {
210 if (!CloseHandle(*(_ts5_status.timer.serialport[i].port))) {
211 ts5_log(TS5_LOGLEVEL_1,
"%s %s %d\n",
212 "ts5_uninstall_serialport",
213 "could not uninstall serial port", i);
217 tcdrain(*(_ts5_status.timer.serialport[i].port));
219 if (close(*(_ts5_status.timer.serialport[i].port)) == -1) {
220 ts5_log(TS5_LOGLEVEL_1,
"%s %s %d\n",
221 "ts5_uninstall_serialport",
222 "could not uninstall serial port", i);
227 al_free(_ts5_status.timer.serialport[i].port);
230 al_free(_ts5_status.timer.serialport);
232 _ts5_status.timer.serialport_is_trigger_device = 0;
247 ALLEGRO_EVENT serialport_event;
248 double looptime = al_get_time();
251 while (!al_get_thread_should_stop(_ts5_data.timer.serialport_thread)) {
254 for (i=0; i<_ts5_status.timer.num_serialports; i++) {
259 if (_ts5_status.timer.serialport[i].is_trigger_input_device) {
261 char serial_buff[TS5_MAX_CHAR];
265 unsigned long bytes_to_read=1, bytes_read;
267 if (!ReadFile(*(_ts5_status.timer.serialport[i].port),
268 serial_buff, bytes_to_read, &bytes_read, NULL)) {
269 ts5_fatal(
"ts5_serialport_threadfunc: read error\n");
272 num_bytes = bytes_read;
275 unsigned int bytes_to_read=1, bytes_read;
277 bytes_read = read(*(_ts5_status.timer.serialport[i].port),
278 serial_buff, bytes_to_read);
280 if ((
int)bytes_read == -1) {
281 ts5_fatal(
"ts5_serialport_threadfunc: read error\n");
284 num_bytes = bytes_read;
290 serialport_event.user.type = TS5_EVENT_SERIALPORT_TRIGGER;
292 serialport_event.user.data1 = i;
293 serialport_event.user.data2 = serial_buff[0];
294 serialport_event.user.data3 =
295 (intptr_t)((now-looptime)*1000000.0);
298 &_ts5_data.timer.serialport_trigger_event_source,
299 &serialport_event, NULL);
304 if (_ts5_status.timer.serialport[i].simulate_trigger_input) {
306 if (now - _ts5_status.timer.serialport[i]
307 .last_trigger_simulation
308 >= _ts5_status.timer.serialport[i]
309 .trigger_simulation_interval) {
311 serialport_event.user.type = TS5_EVENT_SERIALPORT_TRIGGER;
313 serialport_event.user.data1 = i;
315 serialport_event.user.data2 =
316 _ts5_status.timer.serialport[i].simulate_trigger_input;
318 serialport_event.user.data3 =
319 (intptr_t)((now-looptime)*1000000.0);
322 &_ts5_data.timer.serialport_trigger_event_source,
323 &serialport_event, NULL);
325 _ts5_status.timer.serialport[i].last_trigger_simulation =