Tscope5
serialport.c
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // __ ______
4 // / /_______________ ____ ___ / ____/
5 // / __/ ___/ ___/ __ \/ __ \/ _ \ /___ )
6 // / /_(__ ) /__/ /_/ / /_/ / __/ ____/ /
7 // \__/____/\___/\____/ .___/\___/ /_____/
8 // /_/
9 //
10 /// \file serialport.c
11 /// Definitions of serial port functions
12 /// \example serialport01.c
13 /// \example serialport02.c
14 ////////////////////////////////////////////////////////////////////////////////
15 
16 
17 #include "../include/tscope5/serialport.h"
18 #include "../include/tscope5/timer.h"
19 #include "../include/tscope5/serialport_internal.h"
20 #include "../include/tscope5/system_internal.h"
21 
22 
23 ////////////////////////////////////////////////////////////////////////////////
24 /// @name Trigger input/output
25 /// The serial ports can be used as trigger input/output devices.
26 /// See timer.c for more information about trigger input/output.
27 //@{
28 ////////////////////////////////////////////////////////////////////////////////
29 
30 
31 ////////////////////////////////////////////////////////////////////////////////
32 /// Define a serial port as a trigger input device
33 ///
34 /// \param portname Name of the serial port
35 /// (COM1, ... on Windows, /etc/tty... on Posix systems).
36 ///
37 /// \return The device number of the port
38 /// (the first serial port opened by Tscope5 gets number 1, etc.).
39 ////////////////////////////////////////////////////////////////////////////////
41 {
42  ts5_check_serialport("ts5_define_serialport_trigger_input");
43 
44  int portnum = ts5_check_serialport2("ts5_define_serialport_trigger_input",
45  portname);
46 
47  ts5_log(TS5_LOGLEVEL_3, "ts5_define_serialport_trigger_input(%s)\n",
48  portname);
49 
50  _ts5_status.timer.serialport_is_trigger_device = 1;
51  _ts5_status.timer.serialport[portnum].is_trigger_input_device = 1;
52 
53  return portnum+1;
54 }
55 
56 
57 ////////////////////////////////////////////////////////////////////////////////
58 /// Turn on serial port input simulation.
59 ///
60 /// \param device Number of the serial port.
61 /// \param value Input value that has to be simulated
62 /// (0-255, 0 turns the simulation off).
63 /// \param interval Interval in seconds between two triggers.
64 ////////////////////////////////////////////////////////////////////////////////
65 void ts5_simulate_serialport_trigger_input(int device, unsigned char value,
66  double interval)
67 {
68  ts5_check_serialport("ts5_simulate_serialport_trigger_input");
69  ts5_log(TS5_LOGLEVEL_3, "ts5_simulate_serialport_trigger_input(%d,%d,%f)\n",
70  device, value, interval);
71 
72  if (device<1 || device>_ts5_status.timer.num_serialports) {
73  ts5_fatal("%s: %s %d, %s %d\n", "ts5_simulate_serialport_trigger_input",
74  "device argument is", device,
75  "number of serial ports is",
76  _ts5_status.timer.num_serialports);
77  }
78 
79  _ts5_status.timer.serialport[device-1].simulate_trigger_input =
80  value;
81 
82  _ts5_status.timer.serialport[device-1].trigger_simulation_interval =
83  interval;
84 }
85 
86 
87 ////////////////////////////////////////////////////////////////////////////////
88 /// Define a serial port as a trigger output device
89 ///
90 /// \param portname Name of the serial port
91 /// (COM1, ... on Windows, /etc/tty... on Posix systems).
92 ///
93 /// \return The device number of the port
94 /// (the first serial port opened by Tscope5 gets number 1, etc.).
95 ////////////////////////////////////////////////////////////////////////////////
97 {
98  ts5_check_serialport("ts5_define_serialport_trigger_output");
99 
100  int portnum = ts5_check_serialport2("ts5_define_serialport_trigger_output",
101  portname);
102 
103  ts5_log(TS5_LOGLEVEL_3, "ts5_define_serialport_trigger_output(%s)\n",
104  portname);
105 
106  _ts5_status.timer.serialport_is_trigger_device = 1;
107  _ts5_status.timer.serialport[portnum].is_trigger_output_device = 1;
108  return portnum+1;
109 }
110 
111 
112 ////////////////////////////////////////////////////////////////////////////////
113 /// Send a trigger trough a serial port
114 ///
115 /// \param device Number of the serial port (devices are numbered from 1).
116 /// \param value The trigger value that has to be sent (0-255).
117 ////////////////////////////////////////////////////////////////////////////////
118 void ts5_send_serialport_trigger(int device, unsigned char value)
119 {
120  ts5_check_serialport("ts5_send_serialport_trigger");
121  ts5_log(TS5_LOGLEVEL_3, "ts5_send_serialport_trigger(%d,%d)\n",
122  device, value);
123 
124  if (device<1 || device>_ts5_status.timer.num_serialports) {
125  ts5_fatal("%s: %s %d, %s %d\n", "ts5_send_serialport_trigger",
126  "device argument is", device,
127  "number of serial ports is",
128  _ts5_status.timer.num_serialports);
129  }
130 
131  char serial_buff[TS5_MAX_CHAR];
132  sprintf(serial_buff, "%c", value);
133 
134  #ifdef TS5_WINDOWS
135 
136  unsigned long bytes_to_write = strlen(serial_buff);
137  unsigned long bytes_written;
138 
139  if (!WriteFile(*(_ts5_status.timer.serialport[device-1].port),
140  serial_buff, bytes_to_write, &bytes_written, NULL)) {
141  ts5_fatal("ts5_send_serialport_trigger: write error 1\n");
142  }
143 
144  if (bytes_written != bytes_to_write) {
145  ts5_fatal("ts5_send_serialport_trigger: write error 2\n");
146  }
147 
148 
149  #else
150 
151  unsigned int bytes_to_write = strlen(serial_buff);
152  unsigned int bytes_written;
153 
154  bytes_written =
155  write(*(_ts5_status.timer.serialport[device-1].port),
156  serial_buff, bytes_to_write);
157 
158  if (bytes_written != bytes_to_write) {
159  ts5_fatal("ts5_send_serialport_trigger: write error\n");
160  }
161 
162 
163  #endif
164 }
165 
166 
167 ////////////////////////////////////////////////////////////////////////////////
168 /// Get a pointer to a serial port
169 ///
170 /// \param device Number of the serial port (devices are numbered from 1).
171 ///
172 /// \return pointer to the serial port.
173 ////////////////////////////////////////////////////////////////////////////////
174 TS5_SERIALPORT* ts5_get_serialport(int device)
175 {
176  ts5_check_serialport("ts5_get_serialport");
177  ts5_log(TS5_LOGLEVEL_3, "ts5_get_serialport(%d)\n", device);
178 
179  if (device < 1 || device>_ts5_status.timer.num_serialports) {
180  ts5_fatal("%s: %s %d, %s %d\n", ts5_get_serialport,
181  "device argument is", device,
182  "number of opened serial ports is",
183  _ts5_status.timer.num_serialports);
184  }
185 
186  return _ts5_status.timer.serialport[device-1].port;
187 }
188 
189 
190 
191 ////////////////////////////////////////////////////////////////////////////////
192 //@}
193 ////////////////////////////////////////////////////////////////////////////////