source: BAORadio/libindi/libindi/drivers/telescope/magellandriver.c @ 646

Last change on this file since 646 was 646, checked in by frichard, 12 years ago
File size: 5.3 KB
Line 
1#if 0
2    MAGELLAN Driver
3    Copyright (C) 2011 Onno Hommes  (ohommes@alumni.cmu.edu)
4
5    This library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    This library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with this library; if not, write to the Free Software
17    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18
19#endif
20
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <stdarg.h>
25#include <math.h>
26#include <sys/time.h>
27#include <unistd.h>
28#include <fcntl.h>
29#include <time.h>
30
31#include "indicom.h"
32#include "indidevapi.h"
33#include "magellandriver.h"
34
35#ifndef _WIN32
36#include <termios.h>
37#endif
38
39/**************************************************************************
40 Connection Diagnostics
41 **************************************************************************/
42char ACK(int fd);
43int check_magellan_connection(int fd);
44
45/**************************************************************************
46 Get Commands: store data in the supplied buffer.
47 Return 0 on success or -1 on failure
48 **************************************************************************/
49 
50/* Get Double from Sexagisemal */
51int getCommandSexa(int fd, double *value, const char *cmd);
52
53/* Get String */
54static int getCommandString(int fd, char *data, const char* cmd);
55
56/* Get Calender data */
57int getCalenderDate(int fd, char *date);
58
59
60/**************************************************************************
61 Driver Implementations
62 **************************************************************************/
63
64/*
65   Check the Magellan Connection using any set command
66   Magellan 1 does not support any sey but still sends
67   'OK' for it
68*/
69int check_magellan_connection(int fd)
70{
71  int i=0;
72 
73  /* Magellan I ALways Response OK for :S?# */
74  char ack[4] = ":S?#";
75
76  char Response[64];
77  int nbytes_read=0;
78
79  #ifdef INDI_DEBUG
80  IDLog("Testing telescope's connection...\n");
81  #endif
82
83  if (fd <= 0) return MAGELLAN_ERROR;
84
85  for (i=0; i < CONNECTION_RETRIES; i++)
86  {
87    if (write(fd, ack, 4) < 0) return MAGELLAN_ERROR;
88   
89    tty_read(fd, Response, 2, MAGELLAN_TIMEOUT, &nbytes_read);
90   
91    if (nbytes_read == 2) return MAGELLAN_OK;
92   
93    usleep(50000);
94  }
95
96  tcflush(fd, TCIFLUSH);
97  return MAGELLAN_ERROR;
98}
99
100
101/**********************************************************************
102* GET FUNCTIONS
103**********************************************************************/
104
105char ACK(int fd)
106{
107 
108  /* Magellan I ALways Response OK for :S?# (any set will do)*/
109  char ack[4] = ":S?#";
110  char Response[64];
111  int nbytes_read=0;
112  int i=0;
113  int result = MAGELLAN_ERROR;
114
115  /* Check for Comm handle */
116  if (fd > 0) 
117  {
118    for (i=0; i < CONNECTION_RETRIES; i++)
119    {
120      /* Send ACK string to Magellan */
121      if (write(fd, ack, 4) >= 0)
122      {
123        /* Read Response */
124        tty_read(fd, Response, 2, MAGELLAN_TIMEOUT, &nbytes_read);
125       
126        /* If the two byte OK is returned we have an Ack */
127        if (nbytes_read == 2) 
128        {
129          result = MAGELLAN_ACK;
130          break; /* Force quick success return */
131        }
132        else usleep(50000);
133      }
134    }
135  }
136
137  tcflush(fd, TCIFLUSH);
138  return result;
139}
140
141int getCommandSexa(int fd, double *value, const char * cmd)
142{
143  char temp_string[16];
144  int result = MAGELLAN_ERROR;
145  int nbytes_write=0, nbytes_read=0;
146 
147  if ( (tty_write_string(fd, cmd, &nbytes_write)) == TTY_OK)
148  {
149    if ((tty_read_section(fd, temp_string, '#', MAGELLAN_TIMEOUT, &nbytes_read)) == TTY_OK)
150    {
151      temp_string[nbytes_read - 1] = '\0';
152
153      if (f_scansexa(temp_string, value))
154      {
155        #ifdef INDI_DEBUG
156        IDLog("unable to process [%s]\n", temp_string);
157        #endif
158      }
159      else
160      {
161        result = MAGELLAN_OK;
162      }
163    }
164  }
165 
166  tcflush(fd, TCIFLUSH);
167  return result;
168}
169
170static int getCommandString(int fd, char *data, const char* cmd)
171{
172    int nbytes_write=0, nbytes_read=0;
173    int result = MAGELLAN_ERROR;
174   
175   if ( (tty_write_string(fd, cmd, &nbytes_write)) == TTY_OK)
176   {
177      if ((tty_read_section(fd, data, '#', MAGELLAN_TIMEOUT, &nbytes_read)) == TTY_OK)
178      {
179        data[nbytes_read - 1] = '\0';
180        result = MAGELLAN_OK;
181      }
182   }
183
184   tcflush(fd, TCIFLUSH);
185   return result;
186}
187
188
189int getCalenderDate(int fd, char *date)
190{
191
192 int dd, mm, yy;
193 char century[3];
194 int result;
195
196 if ( (result = getCommandString(fd, date, "#:GC#")) )
197 {
198    /* Magellan format is MM/DD/YY */
199    if ((sscanf(date, "%d%*c%d%*c%d", &mm, &dd, &yy)) > 2)
200    {
201      /* Consider years 92 or more to be in the last century, anything less
202        in the 21st century.*/
203      if (yy > CENTURY_THRESHOLD) strncpy(century, "19", 3);
204      else strncpy(century, "20", 3);
205
206      /* Format must be YYYY/MM/DD format */
207      snprintf(date, 16, "%s%02d/%02d/%02d", century, yy, mm, dd);
208    }
209 }
210
211 return result;
212}
213
214
Note: See TracBrowser for help on using the repository browser.