Instrument Neutral Distributed Interface INDI  2.0.2
STAR2kdriver.c
Go to the documentation of this file.
1 /*******************************************************************************
2  created 2014 G. Schmidt
3 
4  This program is free software; you can redistribute it and/or modify it
5  under the terms of the GNU General Public License as published by the Free
6  Software Foundation; either version 2 of the License, or (at your option)
7  any later version.
8 
9  This program is distributed in the hope that it will be useful, but WITHOUT
10  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 
19  The full GNU General Public License is included in this distribution in the
20  file called LICENSE.
21 *******************************************************************************/
22 
23 #define _GNU_SOURCE // for usleep
24 
25 #include "STAR2kdriver.h"
26 
27 #ifndef _WIN32
28 #include <termios.h>
29 #endif
30 
31 #include <stdio.h>
32 #include <sys/time.h>
33 #include <unistd.h>
34 #include <fcntl.h>
35 
36 /* STAR2000 RS232 box control functions */
37 
38 int ConnectSTAR2k(char *port);
39 void DisconnectSTAR2k(void);
40 
41 void StartPulse(int direction);
42 void StopPulse(int direction);
43 
44 /* Serial communication utilities (taken from celestronprotocol.c) */
45 
46 typedef fd_set telfds;
47 
48 static int writen(int fd, char *ptr, int nbytes);
49 
50 /* some static variables */
51 
52 static int STAR2kPortFD;
53 static int STAR2kConnectFlag = 0;
54 
55 static char STAR2kOpStat = 0;
56 
57 /************************* STAR2000 control functions *************************/
58 
59 int ConnectSTAR2k(char *port)
60 {
61 #ifdef _WIN32
62  return (-1);
63 #else
64  struct termios tty;
65 
66  char initCmd[] = { 0x0D, 0x00 };
67 
68  fprintf(stderr, "Connecting to port: %s\n", port);
69 
70  if (STAR2kConnectFlag != 0)
71  return (0);
72 
73  /* Make the connection */
74 
75  STAR2kPortFD = open(port, O_RDWR);
76  if (STAR2kPortFD == -1)
77  return (-1);
78 
79  tcgetattr(STAR2kPortFD, &tty);
80  cfsetospeed(&tty, (speed_t)B9600);
81  cfsetispeed(&tty, (speed_t)B9600);
82  tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;
83  tty.c_iflag = IGNBRK;
84  tty.c_lflag = 0;
85  tty.c_oflag = 0;
86  tty.c_cflag |= CLOCAL | CREAD;
87  tty.c_cc[VMIN] = 1;
88  tty.c_cc[VTIME] = 5;
89  tty.c_iflag &= ~(IXON | IXOFF | IXANY);
90  tty.c_cflag &= ~(PARENB | PARODD);
91  tcsetattr(STAR2kPortFD, TCSANOW, &tty);
92 
93  /* Flush the input (read) buffer */
94 
95  tcflush(STAR2kPortFD, TCIOFLUSH);
96 
97  /* initialize connection */
98 
99  usleep(500000);
100  writen(STAR2kPortFD, initCmd, 2);
101 
102  STAR2kOpStat = 0;
103 
104  return (0);
105 #endif
106 }
107 
108 /* Start a slew in chosen direction at slewRate */
109 /* Use auxilliary NexStar command set through the hand control computer */
110 
111 void StartPulse(int direction)
112 {
113  if (direction == NORTH)
114  {
115  STAR2kOpStat |= S2K_NORTH_1;
116  }
117  else if (direction == EAST)
118  {
119  STAR2kOpStat |= S2K_EAST_1;
120  }
121  else if (direction == SOUTH)
122  {
123  STAR2kOpStat |= S2K_SOUTH_1;
124  }
125  else if (direction == WEST)
126  {
127  STAR2kOpStat |= S2K_WEST_1;
128  }
129 
130  writen(STAR2kPortFD, &STAR2kOpStat, 1);
131 }
132 
133 void StopPulse(int direction)
134 {
135  if (direction == NORTH)
136  {
137  STAR2kOpStat &= S2K_NORTH_0;
138  }
139  else if (direction == EAST)
140  {
141  STAR2kOpStat &= S2K_EAST_0;
142  }
143  else if (direction == SOUTH)
144  {
145  STAR2kOpStat &= S2K_SOUTH_0;
146  }
147  else if (direction == WEST)
148  {
149  STAR2kOpStat &= S2K_WEST_0;
150  }
151  else if (direction == ALL)
152  {
153  STAR2kOpStat = 0;
154  }
155 
156  writen(STAR2kPortFD, &STAR2kOpStat, 1);
157 }
158 
160 {
161  StopPulse(ALL);
162 
163  if (STAR2kConnectFlag == 1)
164  close(STAR2kPortFD);
165 
166  STAR2kConnectFlag = 0;
167 }
168 
169 /******************************* Serial port utilities ************************/
170 
171 static int writen(int fd, char *ptr, int nbytes)
172 {
173  int nleft, nwritten;
174  nleft = nbytes;
175  while (nleft > 0)
176  {
177  nwritten = write(fd, ptr, nleft);
178  if (nwritten <= 0)
179  break;
180  nleft -= nwritten;
181  ptr += nwritten;
182  }
183  return (nbytes - nleft);
184 }
185 
186 /******************************************************************************/
int ConnectSTAR2k(char *port)
Definition: STAR2kdriver.c:59
void DisconnectSTAR2k(void)
Definition: STAR2kdriver.c:159
void StopPulse(int direction)
Definition: STAR2kdriver.c:133
void StartPulse(int direction)
Definition: STAR2kdriver.c:111
fd_set telfds
Definition: STAR2kdriver.c:46
#define EAST
Definition: STAR2kdriver.h:39
#define S2K_WEST_1
Definition: STAR2kdriver.h:27
#define NORTH
Definition: STAR2kdriver.h:37
#define ALL
Definition: STAR2kdriver.h:42
#define S2K_SOUTH_0
Definition: STAR2kdriver.h:33
#define WEST
Definition: STAR2kdriver.h:38
#define S2K_SOUTH_1
Definition: STAR2kdriver.h:28
#define S2K_NORTH_1
Definition: STAR2kdriver.h:29
#define S2K_NORTH_0
Definition: STAR2kdriver.h:34
#define S2K_EAST_1
Definition: STAR2kdriver.h:30
#define S2K_WEST_0
Definition: STAR2kdriver.h:32
#define SOUTH
Definition: STAR2kdriver.h:40
#define S2K_EAST_0
Definition: STAR2kdriver.h:35
int fd
Definition: intelliscope.c:43