19 #include <sys/types.h>
20 #include <sys/ioctl.h>
25 #include "../include/tscope5/timer.h"
27 int _ts5_cedrusbox_fd[TS5_CEDRUSBOX_MAXPORT] = {TS5_CEDRUSBOX_NODEVICE};
28 struct termios _ts5_cedrusbox_oldoptions[TS5_CEDRUSBOX_MAXPORT];
46 int num_serial_devices = 0;
51 dp = opendir (
"/dev");
54 ts5_fatal(
"%s: could not get directory listing\n", calling_function);
58 char device_path[TS5_CEDRUSBOX_MAXPORT][TS5_MAX_CHAR];
60 while ((ep = readdir (dp)) && num_serial_devices<TS5_CEDRUSBOX_MAXPORT) {
62 char devicename[TS5_MAX_CHAR];
65 strcpy(devicename,
"tty.usbserial");
68 strcpy(devicename,
"ttyUSB");
72 if (strstr(ep->d_name, devicename)!=0) {
74 sprintf(device_path[num_serial_devices],
"/dev/%s", ep->d_name);
75 ts5_log(TS5_LOGLEVEL_1,
"%s: found a usb serial device at %s\n",
76 calling_function, device_path[num_serial_devices]);
79 _ts5_cedrusbox_fd[num_serial_devices] = open(device_path[num_serial_devices],
80 O_RDWR | O_NOCTTY | O_NDELAY);
82 if (_ts5_cedrusbox_fd[num_serial_devices] == -1) {
83 ts5_fatal(
"%s: could not open usb serial device at %s\n",
84 calling_function, device_path[num_serial_devices]);
88 if (ioctl(_ts5_cedrusbox_fd[num_serial_devices], TIOCEXCL) == -1) {
89 ts5_fatal(
"%s: could not get exclusivity on usb serial device at %s\n",
90 calling_function, device_path[num_serial_devices]);
94 struct termios options;
95 tcgetattr(_ts5_cedrusbox_fd[num_serial_devices] ,
96 &_ts5_cedrusbox_oldoptions[num_serial_devices]);
97 bzero (&options,
sizeof (options));
99 cfsetispeed(&options, B115200);
100 cfsetospeed(&options, B115200);
102 options.c_iflag = IGNBRK;
105 options.c_cflag |= CLOCAL | CREAD;
106 options.c_iflag = IGNPAR;
108 options.c_cflag &= ~(PARENB|PARODD) ;
109 options.c_cflag &= ~CSTOPB ;
110 options.c_cflag &= ~CSIZE;
111 options.c_cflag |= CS8;
112 options.c_cflag &= ~CRTSCTS;
113 options.c_iflag &= ~(IXON | IXOFF | IXANY);
115 if (tcsetattr (_ts5_cedrusbox_fd[num_serial_devices], TCSANOW, &options) == -1) {
116 ts5_fatal(
"%s: could not set options on usb serial device at %s\n",
117 calling_function, device_path[num_serial_devices]);
120 if (fcntl(_ts5_cedrusbox_fd[num_serial_devices], F_SETFL, FNDELAY) == -1) {
121 ts5_fatal(
"%s: could not set delay on usb serial device at %s\n",
122 calling_function, device_path[num_serial_devices]);
126 num_serial_devices++;
131 return num_serial_devices;
143 for (i=0; i<_ts5_status.timer.num_cedrusboxes; i++) {
145 if (_ts5_cedrusbox_fd[i] > 0) {
147 tcdrain(_ts5_cedrusbox_fd[i]);
148 tcsetattr(_ts5_cedrusbox_fd[i], TCSANOW, &_ts5_cedrusbox_oldoptions[i]);
150 if (close(_ts5_cedrusbox_fd[i]) == -1) {
151 ts5_log(TS5_LOGLEVEL_1,
"ts5_uninstall_cedrusbox_posix:\n");
152 ts5_log(TS5_LOGLEVEL_1,
"\tcould not uninstall cedrusbox %d\n", i);
155 ts5_log(TS5_LOGLEVEL_1,
"ts5_uninstall_cedrusbox_posix: uninstalled cedrusbox %d\n", i);
168 unsigned int bytes_read;
169 bytes_read = read(_ts5_cedrusbox_fd[port], buff, bytes_to_read);
171 if ((
int)bytes_read == -1) {
172 ts5_fatal(
"ts5_read_cedrusbox_posix: read error\n");
186 unsigned int bytes_written;
187 bytes_written = write(_ts5_cedrusbox_fd[port], buff, bytes_to_write);
189 if (bytes_written != bytes_to_write) {
190 ts5_fatal(
"ts5_write_cedrusbox_posix: write error\n");
195 return bytes_written;
206 char buff[TS5_MAX_CHAR];
207 unsigned int bytes_read;
210 bytes_read = read (_ts5_cedrusbox_fd[port], buff, 6);
211 }
while (bytes_read > 0);
int ts5_install_cedrusbox_posix(char *calling_function)
Posix-specific (Mac OS X, Linux) cedrusbox function.
void ts5_wait(double waittime)
Wait for a number of seconds.
void ts5_uninstall_cedrusbox_posix()
Posix-specific (Mac OS X, Linux) cedrusbox function.
void ts5_log(const unsigned int level, const char *format,...)
Send info to a logging window.
unsigned int ts5_read_cedrusbox_posix(int port, char *buff, unsigned long bytes_to_read)
Posix-specific (Mac OS X, Linux) cedrusbox function.
void ts5_fflush_cedrusbox_posix(int port)
Posix-specific (Mac OS X, Linux) cedrusbox function.
void ts5_fatal(const char *format,...)
Exit safely with an error message.
unsigned int ts5_write_cedrusbox_posix(int port, char *buff, unsigned long bytes_to_write)
Posix-specific (Mac OS X, Linux) cedrusbox function.