26 #include <libnova/julian_day.h>
34 #define IEQPRO_TIMEOUT 5
36 static bool ieqpro_debug =
false;
37 static bool ieqpro_simulation =
false;
51 ieqpro_debug = enable;
56 ieqpro_simulation = enable;
117 char initCMD[] =
":V#";
122 int nbytes_written = 0;
126 for (
int i = 0; i < 2; i++)
128 if (ieqpro_simulation)
130 strcpy(response,
"V1.00#");
131 nbytes_read = strlen(response);
135 tcflush(
fd, TCIFLUSH);
156 response[nbytes_read] =
'\0';
159 if (!strcmp(response,
"V1.00#"))
171 char cmd[] =
":GAS#";
176 int nbytes_written = 0;
180 if (ieqpro_simulation)
184 nbytes_read = strlen(response);
188 tcflush(
fd, TCIFLUSH);
207 response[nbytes_read] =
'\0';
210 if (nbytes_read == 7)
219 tcflush(
fd, TCIFLUSH);
250 char cmd[] =
":MountInfo#";
255 int nbytes_written = 0;
259 if (ieqpro_simulation)
261 strcpy(response,
"0045");
262 nbytes_read = strlen(response);
266 tcflush(
fd, TCIFLUSH);
285 response[nbytes_read] =
'\0';
288 if (nbytes_read == 4)
290 std::map<std::string, std::string> models =
292 {
"0010",
"Cube II EQ"},
293 {
"0011",
"Smart EQ Pro+"},
295 {
"0026",
"CEM25-EC"},
296 {
"0030",
"iEQ30Pro"},
298 {
"0041",
"CEM40-EC"},
300 {
"0045",
"iEQ45 Pro EQ"},
301 {
"0046",
"iEQ45 Pro AA"},
303 {
"0061",
"CEM60-EC"},
305 {
"0121",
"CEM120-EC"},
306 {
"0122",
"CEM120-EC2"},
307 {
"5010",
"Cube II AA"},
308 {
"5035",
"AZ Mount Pro"},
309 {
"5045",
"iEQ45 Pro AA"},
312 if (models.find(response) != models.end())
313 info->
Model = models[response];
315 info->
Model =
"Unknown";
317 tcflush(
fd, TCIFLUSH);
329 char cmd[] =
":FW1#";
334 int nbytes_written = 0;
338 if (ieqpro_simulation)
340 strcpy(response,
"150324150101#");
341 nbytes_read = strlen(response);
345 tcflush(
fd, TCIFLUSH);
364 response[nbytes_read] =
'\0';
367 if (nbytes_read == 13)
369 char board[8] = {0}, controller[8] = {0};
371 strncpy(board, response, 6);
372 strncpy(controller, response + 6, 6);
377 tcflush(
fd, TCIFLUSH);
389 char cmd[] =
":FW2#";
394 int nbytes_written = 0;
398 if (ieqpro_simulation)
400 strcpy(response,
"140324140101#");
401 nbytes_read = strlen(response);
405 tcflush(
fd, TCIFLUSH);
424 response[nbytes_read] =
'\0';
427 if (nbytes_read == 13)
429 char ra[8] = {0},
dec[8] = {0};
431 strncpy(
ra, response, 6);
432 strncpy(
dec, response + 6, 6);
437 tcflush(
fd, TCIFLUSH);
452 int nbytes_written = 0;
479 if (ieqpro_simulation)
482 tcflush(
fd, TCIFLUSH);
491 tcflush(
fd, TCIFLUSH);
502 int nbytes_written = 0;
519 if (ieqpro_simulation)
521 strcpy(response,
"1");
522 nbytes_read = strlen(response);
526 tcflush(
fd, TCIFLUSH);
545 response[nbytes_read] =
'\0';
548 tcflush(
fd, TCIFLUSH);
558 char cmd[] =
":MSH#";
563 int nbytes_written = 0;
567 if (ieqpro_simulation)
569 strcpy(response,
"1");
570 nbytes_read = strlen(response);
574 tcflush(
fd, TCIFLUSH);
593 response[nbytes_read] =
'\0';
596 tcflush(
fd, TCIFLUSH);
611 int nbytes_written = 0;
615 if (ieqpro_simulation)
617 strcpy(response,
"1");
618 nbytes_read = strlen(response);
622 tcflush(
fd, TCIFLUSH);
641 response[nbytes_read] =
'\0';
644 tcflush(
fd, TCIFLUSH);
654 char cmd[] =
":SZP#";
659 int nbytes_written = 0;
663 if (ieqpro_simulation)
665 strcpy(response,
"1");
666 nbytes_read = strlen(response);
670 tcflush(
fd, TCIFLUSH);
689 response[nbytes_read] =
'\0';
692 tcflush(
fd, TCIFLUSH);
707 int nbytes_written = 0;
709 snprintf(
cmd, 16,
":SR%d#", ((
int)rate) + 1);
713 if (ieqpro_simulation)
716 strcpy(response,
"1");
717 nbytes_read = strlen(response);
721 tcflush(
fd, TCIFLUSH);
740 response[nbytes_read] =
'\0';
743 tcflush(
fd, TCIFLUSH);
758 int nbytes_written = 0;
763 strcpy(
cmd,
":RT0#");
766 strcpy(
cmd,
":RT1#");
769 strcpy(
cmd,
":RT2#");
772 strcpy(
cmd,
":RT3#");
775 strcpy(
cmd,
":RT4#");
781 if (ieqpro_simulation)
784 strcpy(response,
"1");
785 nbytes_read = strlen(response);
789 tcflush(
fd, TCIFLUSH);
808 response[nbytes_read] =
'\0';
811 tcflush(
fd, TCIFLUSH);
827 int nbytes_written = 0;
834 snprintf(
cmd, 16,
":RR%c%07.4f#", sign, fabs(rate));
838 if (ieqpro_simulation)
840 strcpy(response,
"1");
841 nbytes_read = strlen(response);
845 tcflush(
fd, TCIFLUSH);
864 response[nbytes_read] =
'\0';
867 tcflush(
fd, TCIFLUSH);
883 int nbytes_written = 0;
890 snprintf(
cmd, 16,
":RD%c%07.4f#", sign, fabs(rate));
894 if (ieqpro_simulation)
896 strcpy(response,
"1");
897 nbytes_read = strlen(response);
901 tcflush(
fd, TCIFLUSH);
920 response[nbytes_read] =
'\0';
923 tcflush(
fd, TCIFLUSH);
938 int nbytes_written = 0;
940 snprintf(
cmd, 16,
":RG%02d%02d#",
static_cast<int>(raRate * 100.0),
static_cast<int>(deRate * 100.0));
944 if (ieqpro_simulation)
946 simData.ra_guide_rate = raRate;
947 simData.de_guide_rate = deRate;
948 strcpy(response,
"1");
949 nbytes_read = strlen(response);
953 tcflush(
fd, TCIFLUSH);
972 response[nbytes_read] =
'\0';
975 tcflush(
fd, TCIFLUSH);
988 char response[8] = {0};
990 int nbytes_written = 0;
994 if (ieqpro_simulation)
996 snprintf(response, 8,
"%02d%02d#",
static_cast<int>(
simData.ra_guide_rate * 100),
static_cast<int>(
simData.de_guide_rate * 100));
997 nbytes_read = strlen(response);
1001 tcflush(
fd, TCIFLUSH);
1018 if (nbytes_read > 0)
1020 response[nbytes_read - 1] =
'\0';
1023 char raRateStr[8] = {0}, deRateStr[8] = {0};
1024 strncpy(response, raRateStr, 2);
1025 strncpy(response + 2, deRateStr, 2);
1026 *raRate = atoi(raRateStr) / 100.0;
1027 *deRate = atoi(deRateStr) / 100.0;
1028 tcflush(
fd, TCIFLUSH);
1041 int nbytes_written = 0;
1064 snprintf(
cmd, 16,
":M%c%05d#", dir_c, ms);
1068 if (ieqpro_simulation)
1072 tcflush(
fd, TCIFLUSH);
1082 tcflush(
fd, TCIFLUSH);
1088 char cmd[] =
":MP1#";
1092 int nbytes_read = 0;
1093 int nbytes_written = 0;
1097 if (ieqpro_simulation)
1101 strcpy(response,
"1");
1102 nbytes_read = strlen(response);
1106 tcflush(
fd, TCIFLUSH);
1123 if (nbytes_read > 0)
1125 response[nbytes_read] =
'\0';
1128 if (!strcmp(response,
"1"))
1130 tcflush(
fd, TCIFLUSH);
1146 char cmd[] =
":MP0#";
1150 int nbytes_read = 0;
1151 int nbytes_written = 0;
1155 if (ieqpro_simulation)
1158 strcpy(response,
"1");
1159 nbytes_read = strlen(response);
1163 tcflush(
fd, TCIFLUSH);
1180 if (nbytes_read > 0)
1182 response[nbytes_read] =
'\0';
1185 tcflush(
fd, TCIFLUSH);
1199 int nbytes_read = 0;
1200 int nbytes_written = 0;
1204 if (ieqpro_simulation)
1208 strcpy(response,
"1");
1209 nbytes_read = strlen(response);
1213 tcflush(
fd, TCIFLUSH);
1230 if (nbytes_read > 0)
1232 response[nbytes_read] =
'\0';
1235 tcflush(
fd, TCIFLUSH);
1245 char cmd[] =
":MS#";
1249 int nbytes_read = 0;
1250 int nbytes_written = 0;
1254 if (ieqpro_simulation)
1258 strcpy(response,
"1");
1259 nbytes_read = strlen(response);
1263 tcflush(
fd, TCIFLUSH);
1280 if (nbytes_read > 0)
1282 response[nbytes_read] =
'\0';
1285 if (!strcmp(response,
"1"))
1287 tcflush(
fd, TCIFLUSH);
1293 tcflush(
fd, TCIFLUSH);
1304 char cmd[] =
":CM#";
1308 int nbytes_read = 0;
1309 int nbytes_written = 0;
1313 if (ieqpro_simulation)
1315 strcpy(response,
"1");
1316 nbytes_read = strlen(response);
1320 tcflush(
fd, TCIFLUSH);
1337 if (nbytes_read > 0)
1339 response[nbytes_read] =
'\0';
1342 tcflush(
fd, TCIFLUSH);
1356 int nbytes_read = 0;
1357 int nbytes_written = 0;
1359 snprintf(
cmd, 32,
":ST%d#", enabled ? 1 : 0);
1363 if (ieqpro_simulation)
1366 strcpy(response,
"1");
1367 nbytes_read = strlen(response);
1371 tcflush(
fd, TCIFLUSH);
1388 if (nbytes_read > 0)
1390 response[nbytes_read] =
'\0';
1393 tcflush(
fd, TCIFLUSH);
1407 int nbytes_read = 0;
1408 int nbytes_written = 0;
1411 int ieqValue =
ra * 60 * 60 * 1000;
1413 snprintf(
cmd, 32,
":Sr%08d#", ieqValue);
1417 if (ieqpro_simulation)
1420 strcpy(response,
"1");
1421 nbytes_read = strlen(response);
1425 tcflush(
fd, TCIFLUSH);
1442 if (nbytes_read > 0)
1444 response[nbytes_read] =
'\0';
1447 tcflush(
fd, TCIFLUSH);
1462 int nbytes_read = 0;
1463 int nbytes_written = 0;
1471 int ieqValue = fabs(
dec) * 60 * 60 * 100;
1473 snprintf(
cmd, 32,
":Sd%c%08d#", sign, ieqValue);
1477 if (ieqpro_simulation)
1480 strcpy(response,
"1");
1481 nbytes_read = strlen(response);
1485 tcflush(
fd, TCIFLUSH);
1502 if (nbytes_read > 0)
1504 response[nbytes_read] =
'\0';
1507 tcflush(
fd, TCIFLUSH);
1522 int nbytes_read = 0;
1523 int nbytes_written = 0;
1530 int longitude_arcsecs = fabs(longitude) * 60 * 60;
1531 snprintf(
cmd, 16,
":Sg%c%06d#", sign, longitude_arcsecs);
1535 if (ieqpro_simulation)
1537 strcpy(response,
"1");
1538 nbytes_read = strlen(response);
1542 tcflush(
fd, TCIFLUSH);
1559 if (nbytes_read > 0)
1561 response[nbytes_read] =
'\0';
1564 tcflush(
fd, TCIFLUSH);
1579 int nbytes_read = 0;
1580 int nbytes_written = 0;
1587 int latitude_arcsecs = fabs(latitude) * 60 * 60;
1588 snprintf(
cmd, 16,
":St%c%06d#", sign, latitude_arcsecs);
1592 if (ieqpro_simulation)
1594 strcpy(response,
"1");
1595 nbytes_read = strlen(response);
1599 tcflush(
fd, TCIFLUSH);
1616 if (nbytes_read > 0)
1618 response[nbytes_read] =
'\0';
1621 tcflush(
fd, TCIFLUSH);
1635 int nbytes_read = 0;
1636 int nbytes_written = 0;
1638 strcpy(
cmd,
":Gg#");
1642 if (ieqpro_simulation)
1644 strcpy(response,
"+172800");
1645 nbytes_read = strlen(response);
1649 tcflush(
fd, TCIFLUSH);
1665 if (nbytes_read > 0)
1667 response[nbytes_read - 1] =
'\0';
1670 tcflush(
fd, TCIFLUSH);
1672 int longitude_arcsecs = 0;
1674 if (sscanf(response,
"%d", &longitude_arcsecs) > 0)
1676 *longitude = longitude_arcsecs / 3600.0;
1694 int nbytes_read = 0;
1695 int nbytes_written = 0;
1697 strcpy(
cmd,
":Gt#");
1701 if (ieqpro_simulation)
1703 strcpy(response,
"+106200");
1704 nbytes_read = strlen(response);
1708 tcflush(
fd, TCIFLUSH);
1724 if (nbytes_read > 0)
1726 response[nbytes_read - 1] =
'\0';
1729 tcflush(
fd, TCIFLUSH);
1731 int latitude_arcsecs = 0;
1733 if (sscanf(response,
"%d", &latitude_arcsecs) > 0)
1735 *latitude = latitude_arcsecs / 3600.0;
1753 int nbytes_read = 0;
1754 int nbytes_written = 0;
1756 snprintf(
cmd, 16,
":SC%02d%02d%02d#", yy, mm, dd);
1760 if (ieqpro_simulation)
1762 strcpy(response,
"1");
1763 nbytes_read = strlen(response);
1767 tcflush(
fd, TCIFLUSH);
1784 if (nbytes_read > 0)
1786 response[nbytes_read] =
'\0';
1789 tcflush(
fd, TCIFLUSH);
1803 int nbytes_read = 0;
1804 int nbytes_written = 0;
1806 snprintf(
cmd, 16,
":SL%02d%02d%02d#", hh, mm, ss);
1810 if (ieqpro_simulation)
1812 strcpy(response,
"1");
1813 nbytes_read = strlen(response);
1817 tcflush(
fd, TCIFLUSH);
1834 if (nbytes_read > 0)
1836 response[nbytes_read] =
'\0';
1839 tcflush(
fd, TCIFLUSH);
1853 int nbytes_read = 0;
1854 int nbytes_written = 0;
1857 strcpy(
cmd,
":SDS1#");
1859 strcpy(
cmd,
":SDS0#");
1863 if (ieqpro_simulation)
1865 strcpy(response,
"1");
1866 nbytes_read = strlen(response);
1870 tcflush(
fd, TCIFLUSH);
1887 if (nbytes_read > 0)
1889 response[nbytes_read] =
'\0';
1892 tcflush(
fd, TCIFLUSH);
1907 int nbytes_read = 0;
1908 int nbytes_written = 0;
1915 int offset_minutes = fabs(offset) * 60.0;
1917 snprintf(
cmd, 16,
":SG%c%03d#", sign, offset_minutes);
1921 if (ieqpro_simulation)
1923 strcpy(response,
"1");
1924 nbytes_read = strlen(response);
1928 tcflush(
fd, TCIFLUSH);
1945 if (nbytes_read > 0)
1947 response[nbytes_read] =
'\0';
1950 tcflush(
fd, TCIFLUSH);
1960 char cmd[] =
":GEC#";
1964 int nbytes_read = 0;
1965 int nbytes_written = 0;
1969 if (ieqpro_simulation)
1971 char ra_str[16], dec_str[16];
1979 int ieqDEC = fabs(
simData.dec) * 60 * 60 * 100;
1981 snprintf(dec_str, 16,
"%c%08d", sign, ieqDEC);
1983 int ieqRA =
simData.ra * 60 * 60 * 1000;
1984 snprintf(ra_str, 16,
"%08d", ieqRA);
1986 snprintf(response, 32,
"%s%s#", dec_str, ra_str);
1987 nbytes_read = strlen(response);
1991 tcflush(
fd, TCIFLUSH);
2008 if (nbytes_read > 0)
2010 tcflush(
fd, TCIFLUSH);
2011 response[nbytes_read] =
'\0';
2014 char ra_str[16] = {0}, dec_str[16] = {0};
2016 strncpy(dec_str, response, 9);
2017 strncpy(ra_str, response + 9, 8);
2019 int ieqDEC = atoi(dec_str);
2020 int ieqRA = atoi(ra_str);
2022 *
ra = ieqRA / (60.0 * 60.0 * 1000.0);
2023 *
dec = ieqDEC / (60.0 * 60.0 * 100.0);
2034 char cmd[] =
":GLT#";
2038 int nbytes_read = 0;
2039 int nbytes_written = 0;
2047 if (ieqpro_simulation)
2049 strncpy(response,
"+1800150321173000#", 32);
2050 nbytes_read = strlen(response);
2054 tcflush(
fd, TCIFLUSH);
2071 if (nbytes_read > 0)
2073 tcflush(
fd, TCIFLUSH);
2074 response[nbytes_read] =
'\0';
2077 char utc_str[8] = {0}, yy_str[8] = {0}, mm_str[8] = {0}, dd_str[8] = {0}, hh_str[8] = {0}, minute_str[8] = {0}, ss_str[8] = {0}, dst_str[8] = {0};
2080 strncpy(utc_str, response, 4);
2082 strncpy(dst_str, response + 4, 1);
2084 strncpy(yy_str, response + 5, 2);
2086 strncpy(mm_str, response + 7, 2);
2088 strncpy(dd_str, response + 9, 2);
2090 strncpy(hh_str, response + 11, 2);
2092 strncpy(minute_str, response + 13, 2);
2094 strncpy(ss_str, response + 15, 2);
2096 *utc_hours = atoi(utc_str) / 60.0;
2097 *yy = atoi(yy_str) + 2000;
2102 *minute = atoi(minute_str);
2105 ln_zonedate localTime;
2108 localTime.years = *yy;
2109 localTime.months = *mm;
2110 localTime.days = *dd;
2111 localTime.hours = *hh;
2112 localTime.minutes = *minute;
2113 localTime.seconds = *ss;
2114 localTime.gmtoff = *utc_hours * 3600;
2116 ln_zonedate_to_date(&localTime, &utcTime);
2118 *yy = utcTime.years;
2119 *mm = utcTime.months;
2121 *hh = utcTime.hours;
2122 *minute = utcTime.minutes;
2123 *ss = utcTime.seconds;