Tscope5
cedrusbox_windows_internal.c
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // __ ______
4 // / /_______________ ____ ___ / ____/
5 // / __/ ___/ ___/ __ \/ __ \/ _ \ /___ )
6 // / /_(__ ) /__/ /_/ / /_/ / __/ ____/ /
7 // \__/____/\___/\____/ .___/\___/ /_____/
8 // /_/
9 //
10 /// \file cedrusbox_windows_internal.c
11 /// Definitions of Windows-specific cedrusbox functions.
12 ////////////////////////////////////////////////////////////////////////////////
13 
14 ////////////////////////////////////////////////////////////////////////////////
15 /// @name Windows-specific cedrusbox functions
16 //@{
17 ////////////////////////////////////////////////////////////////////////////////
18 
19 #include "../include/tscope5/timer.h"
20 
21 HANDLE _ts5_cedrusbox_handle[TS5_CEDRUSBOX_MAXPORT] = {TS5_CEDRUSBOX_NODEVICE};
22 
23 int ts5_install_cedrusbox_windows(char *calling_function);
25 unsigned int ts5_read_cedrusbox_windows(int port, char *buff, unsigned long bytes_to_read);
26 unsigned int ts5_write_cedrusbox_windows(int port, char *buff, unsigned long bytes_to_write);
27 void ts5_fflush_cedrusbox_windows(int port);
28 
29 
30 ////////////////////////////////////////////////////////////////////////////////
31 /// Windows-specific cedrusbox function.
32 ///
33 /// \param calling_function Name the function that calls for this check or installation.
34 ///
35 /// \warning This is an internal function. Do not call this function directly.
36 ////////////////////////////////////////////////////////////////////////////////
37 int ts5_install_cedrusbox_windows(char *calling_function)
38 {
39  int i, num_serial_devices=0;
40 
41  for (i=0; i<TS5_CEDRUSBOX_MAXPORT; i++) {
42 
43  // write port name
44  char portname[TS5_MAX_CHAR];
45  sprintf(portname, "COM%d", i+1);
46 
47  // try to open the port
48  _ts5_cedrusbox_handle[num_serial_devices] = CreateFile(portname,
49  GENERIC_READ | GENERIC_WRITE,
50  0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
51 
52  // if failed try next port
53  if (_ts5_cedrusbox_handle[num_serial_devices] == INVALID_HANDLE_VALUE) {
54  continue;
55  }
56 
57  // if port is open mark it as a possible cedrus device
58  ts5_log(TS5_LOGLEVEL_1,"%s: port %s is available\n", calling_function, portname);
59 
60  // read comm state
61  DCB dcb;
62 
63  if (!GetCommState(_ts5_cedrusbox_handle[num_serial_devices], &dcb)) {
64  ts5_log(TS5_LOGLEVEL_1,"%s: GetCommState failed with error code %d\n",
65  calling_function, GetLastError());
66  }
67 
68  // set comm state
69  dcb.BaudRate = 115200;
70  dcb.ByteSize = (unsigned char) 8;
71  dcb.Parity = 0;
72  dcb.StopBits = 0;
73  dcb.fBinary = 1;
74 
75  if (!SetCommState(_ts5_cedrusbox_handle[num_serial_devices], &dcb)) {
76  ts5_log(TS5_LOGLEVEL_1,"%s: SetCommState failed with error code %d\n",
77  calling_function, GetLastError());
78  }
79 
80  // set comm timeouts
81  COMMTIMEOUTS ct;
82  ct.ReadIntervalTimeout = MAXDWORD;
83  ct.ReadTotalTimeoutMultiplier = 0;
84  ct.ReadTotalTimeoutConstant = 0;
85  ct.WriteTotalTimeoutMultiplier = 0;
86  ct.WriteTotalTimeoutConstant = 5000;
87 
88  if (!SetCommTimeouts(_ts5_cedrusbox_handle[num_serial_devices], &ct)) {
89  ts5_log(TS5_LOGLEVEL_1,"%s: SetCommTimeouts failed with error code %d\n",
90  calling_function, GetLastError());
91  }
92 
93  // setup comm
94  if (!SetupComm(_ts5_cedrusbox_handle[num_serial_devices], 2048, 2048)) {
95  ts5_log(TS5_LOGLEVEL_1,"%s: SetupComm failed with error code %d\n",
96  calling_function, GetLastError());
97  }
98 
99  // if we get here mark it as a possible cedrusbox
100  num_serial_devices++;
101  }
102 
103  return num_serial_devices;
104 }
105 
106 
107 ////////////////////////////////////////////////////////////////////////////////
108 /// Windows-specific cedrusbox function.
109 ///
110 /// \warning This is an internal function. Do not call this function directly.
111 ////////////////////////////////////////////////////////////////////////////////
113 {
114  int i;
115  for (i=0; i<_ts5_status.timer.num_cedrusboxes; i++) {
116 
117  int port_num = _ts5_status.timer.cedrusbox[i].port_num;
118 
119  if (!CloseHandle(_ts5_cedrusbox_handle[port_num])) {
120  ts5_log(TS5_LOGLEVEL_1,"ts5_uninstall_cedrusbox_windows:\n");
121  ts5_log(TS5_LOGLEVEL_1,"\tcould not uninstall cedrusbox %d\n", i);
122  }
123 
124  ts5_log(TS5_LOGLEVEL_1, "ts5_uninstall_cedrusbox_windows: uninstalled cedrusbox %d\n", i);
125  }
126 }
127 
128 
129 ////////////////////////////////////////////////////////////////////////////////
130 /// Windows-specific cedrusbox function.
131 ///
132 /// \warning This is an internal function. Do not call this function directly.
133 ////////////////////////////////////////////////////////////////////////////////
134 unsigned int ts5_read_cedrusbox_windows(int port, char *buff, unsigned long bytes_to_read)
135 {
136  unsigned long bytes_read;
137 
138  if (!ReadFile(_ts5_cedrusbox_handle[port], buff, bytes_to_read, &bytes_read, NULL)) {
139  ts5_fatal("ts5_read_cedrusbox_windows: read error\n");
140  }
141 
142  return bytes_read;
143 }
144 
145 
146 ////////////////////////////////////////////////////////////////////////////////
147 /// Windows-specific cedrusbox function.
148 ///
149 /// \warning This is an internal function. Do not call this function directly.
150 ////////////////////////////////////////////////////////////////////////////////
151 unsigned int ts5_write_cedrusbox_windows(int port, char *buff, unsigned long bytes_to_write)
152 {
153  unsigned long bytes_written;
154 
155  if (!WriteFile(_ts5_cedrusbox_handle[port], buff, bytes_to_write, &bytes_written, NULL)) {
156  ts5_fatal("ts5_write_cedrusbox_windows: write error 1\n");
157  }
158 
159  if (bytes_written != bytes_to_write) {
160  ts5_fatal("ts5_write_cedrusbox_windows: write error 2\n");
161  }
162 
163  ts5_wait(0.10);
164 
165  return bytes_written;
166 }
167 
168 
169 ////////////////////////////////////////////////////////////////////////////////
170 /// Windows-specific cedrusbox function.
171 ///
172 /// \warning This is an internal function. Do not call this function directly.
173 ////////////////////////////////////////////////////////////////////////////////
175 {
176  char buff[TS5_MAX_CHAR];
177  unsigned long bytes_read;
178 
179  do {
180  ReadFile(_ts5_cedrusbox_handle[port], buff, 1, &bytes_read, NULL);
181  } while (bytes_read > 0);
182 }
183 
184 //@}
185 
186 
void ts5_fflush_cedrusbox_windows(int port)
Windows-specific cedrusbox function.
void ts5_wait(double waittime)
Wait for a number of seconds.
Definition: timer.c:220
int ts5_install_cedrusbox_windows(char *calling_function)
Windows-specific cedrusbox function.
unsigned int ts5_write_cedrusbox_windows(int port, char *buff, unsigned long bytes_to_write)
Windows-specific cedrusbox function.
void ts5_log(const unsigned int level, const char *format,...)
Send info to a logging window.
Definition: system.c:45
void ts5_uninstall_cedrusbox_windows()
Windows-specific cedrusbox function.
unsigned int ts5_read_cedrusbox_windows(int port, char *buff, unsigned long bytes_to_read)
Windows-specific cedrusbox function.
void ts5_fatal(const char *format,...)
Exit safely with an error message.
Definition: system.c:533