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