summaryrefslogtreecommitdiffstats
path: root/kstars/kstars/indi/temmadriver.c
blob: 554644937788b12d67036ca730fffe68886ca2c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#if 0
	Temma INDI driver
	Copyright (C) 2004 Francois Meyer (dulle @ free.fr)
	Remi Petitdemange for the temma protocol
  Reference site is http://dulle.free.fr/alidade/indi.php

	This library is free software; you can redistribute it and/or
	modify it under the terms of the GNU Lesser General Public
	License as published by the Free Software Foundation; either
	version 2.1 of the License, or (at your option) any later version.

	This library is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
	Lesser General Public License for more details.

	You should have received a copy of the GNU Lesser General Public
	License along with this library; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <math.h>
#include <sys/time.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <time.h>

#include "indicom.h"
#include "indidevapi.h"
#include "eventloop.h"
#include "indicom.h"
#include "temmadriver.h"


	char errormes[][128]={
		"I/O Timeout",
		"Error reading from io port",
		"Error writing to io port",
		"Unrecognized message"
	};

char answer[32];
int fd;
int read_ret, write_ret;
unsigned char buffer[256];
unsigned char buffer2[256];


#if 1
/* Initlization routine */
static void mountInit()
{
	static int inited;		/* set once mountInit is called */

	if (inited)
		return;

	/* setting default comm port */	
	PortT->text = realloc(PortT->text, 10);
	TemmaNoteT[0].text = realloc( TemmaNoteT[0].text, 64);
	TemmaNoteT[1].text = realloc( TemmaNoteT[1].text, 64);
	
	if (!PortT->text || !TemmaNoteT[0].text || !TemmaNoteT[1].text){
		fprintf(stderr,"Memory allocation error");
		return;
	}
	strcpy(PortT->text, "/dev/ttyS0");
	strcpy(TemmaNoteT[0].text, "Experimental Driver");
	strcpy(TemmaNoteT[1].text, "http://dulle.free.fr/alidade/indi.php");

	inited = 1;
}
#endif

void ISGetProperties (const char *dev) {
	if (dev && strcmp (mydev, dev))
		return;

	mountInit();

	IDDefSwitch (&powSw, NULL);
	IDDefNumber (&eqTemma, NULL);
	IDDefNumber (&eqNum, NULL);
	IDDefSwitch (&OnCoordSetSw, NULL);
	IDDefSwitch (&abortSlewSw, NULL);
	IDDefText   (&TemmaNoteTP, NULL);
	IDDefSwitch (&RAmotorSw, NULL);
	IDDefSwitch (&trackmodeSw, NULL);
	IDDefText (&Port, NULL);
	IDDefText (&TemmaVersion, NULL);
	IDDefNumber (&Time, NULL);
	IDDefNumber (&SDTime, NULL);
	IDDefNumber (&cometNum, NULL);
	IDDefNumber (&geoNum, NULL);
}

void ISNewBLOB (const char *dev, const char *name, int sizes[], char *blobs[], char *formats[], char *names[], int n)
{
 dev=dev;name=name;sizes=sizes;blobs=blobs;formats=formats;names=names;n=n;
}

void ISNewText (const char *dev, const char *name, char *texts[], char *names[], int n) {
        /*IText *tp;*/

	if (strcmp (dev, mydev))
		return;

	if (!strcmp (name, Port.name)) {
		IUSaveText(PortT, texts[0]);
		Port.s = IPS_OK;
		IDSetText (&Port, NULL);
	}
	return;
}

/* client is sending us a new value for a Numeric vector property */
void ISNewNumber (const char *dev, const char *name, double values[], char *names[], int n) {
	/* ignore if not ours */
	if (strcmp (dev, mydev))
		return;

	if (!strcmp (name, eqNum.name)) {
		/* new equatorial target coords */
	        /*double newra = 0, newdec = 0;*/
		int i, nset;

		/* Check power, if it is off, then return */
		if (power[0].s != ISS_ON)
		{
			eqNum.s = IPS_IDLE;
			IDSetNumber(&eqNum, "Power is off");
			return;
		}

		for (nset = i = 0; i < n; i++) {
			/* Find numbers with the passed names in the eqNum property */
			INumber *eqp = IUFindNumber (&eqNum, names[i]);

			/* If the number found is Right ascension (eq[0]) then process it */
			if (eqp == &eq[0])
			{
				currentRA = (values[i]);
				nset += currentRA >= 0 && currentRA <= 24;
			}
			/* Otherwise, if the number found is Declination (eq[1]) then process it */ 
			else if (eqp == &eq[1]) {
				currentDec = (values[i]);
				nset += currentDec >= -90 && currentDec <= 90;
			}
		} /* end for */

		/* Did we process the two numbers? */
		if (nset == 2) {
		        /*char r[32], d[32];*/

			/* Set the mount state to BUSY */
			eqNum.s = IPS_BUSY;

			if (SLEWSW==ISS_ON || TRACKSW==ISS_ON){
				IDSetNumber(&eqNum, "Moving to RA Dec %f %f", currentRA,
						currentDec);
				do_TemmaGOTO();
			}

			if (SYNCSW==ISS_ON){
				IDSetNumber(&eqNum, "Syncing to RA Dec %f %f", currentRA, currentDec);
				set_TemmaCurrentpos();
			}
			eqNum.s = IPS_OK;
			IDSetNumber(&eqNum, "Synced");
		}
		/* We didn't process the two number correctly, report an error */
		else {
			/* Set property state to idle */
			eqNum.s = IPS_IDLE;

			IDSetNumber(&eqNum, "RA or Dec absent or bogus.");
		}
	return;
	}
}

/* client is sending us a new value for a Switch property */
void ISNewSwitch (const char *dev, const char *name, ISState *states, char *names[], int n)
{
	ISwitch *sp;

	/* ignore if not ours */
	if (strcmp (dev, mydev))
		return;

	if (!strcmp (name, powSw.name)) {
		sp = IUFindSwitch (&powSw, names[0]);
		if (!sp)
			return;

		fprintf(stderr,"new state %s\n",names[0]);

		sp->s = states[0];
		if (!strcmp(names[0],"CONNECT")) {
			connectMount();
		}		
		if (!strcmp(names[0],"DISCONNECT")) {
			disconnectMount();
		}		
	}

	if (!strcmp (name, abortSlewSw.name)) {
		if (POWSW){
			TemmaabortSlew(); 
			IDSetSwitch (&abortSlewSw, "Abort slew");
		}
		else {
			IDSetSwitch (&abortSlewSw, "Power is off");
		}
	}

	if (!strcmp (name, RAmotorSw.name)) {
		if (POWSW){
			sp = IUFindSwitch (&RAmotorSw, names[0]);
			if (!sp)
				return;

			fprintf(stderr,"new state %s\n",names[0]);

			sp->s = states[0];

			RAmotorSw.s = IPS_BUSY;
			if (!strcmp(names[0],"RUN")) {
				set_TemmaStandbyState(0);
			}

			if (!strcmp(names[0],"STOP")) {
				set_TemmaStandbyState(1);
			}	

			if(get_TemmaStandbyState(buffer)){
				RAmotorSw.s = IPS_IDLE;
				IDSetSwitch (&RAmotorSw, "Error writing to port");
				return;
			}
		}
		else {
			IDSetSwitch (&RAmotorSw, "Power is off");
		}
	}

	if (!strcmp (name, OnCoordSetSw.name)) {
		if (POWSW){
			IUResetSwitches(&OnCoordSetSw);
			sp = IUFindSwitch (&OnCoordSetSw, names[0]);
			if (!sp)
				return;

			fprintf(stderr,"new state %s\n",names[0]);

			sp->s = states[0];

			IUResetSwitches(&OnCoordSetSw);
			IUUpdateSwitches(&OnCoordSetSw, states, names, n);
			/*			    currentSet = getOnSwitch(&OnCoordSetSw); */
			OnCoordSetSw.s = IPS_OK;
			IDSetSwitch(&OnCoordSetSw, NULL);
		}
		else {
			IDSetSwitch (&OnCoordSetSw, "Power is off");
		}
	}
}

double calcLST(char *strlst){
	time_t computertime;
	struct tm gmt;
	double jd, gmst;
	double lst;
	/*int a,b;*/

	time(&computertime);
	gmtime_r(&computertime, &gmt);
	gmt.tm_mon+=1;
	gmt.tm_year+=1900;
	currentUTC=(double)gmt.tm_hour+(double)gmt.tm_min/60+(double)gmt.tm_sec/3600;

	jd=UTtoJD(&gmt);
	gmst=JDtoGMST(jd);

	lst=(gmst-longitude)/15;	

	lst=(lst/24-(int)(lst/24))*24;

	if(lst>=24) 
		lst-=24;
	sprintf(strlst,"%.2d%.2d%.2d", (int)lst,
			((int)(lst*60))%60,
			((int)(lst*3600))%60);
	currentLST=lst;
	IDSetNumber (&SDTime, NULL);
	IDSetNumber (&Time, NULL);
	
	return 0;
}

#if 1
/* update the mount over time */
void readMountcurrentpos (void *p)
{
	char result[32];

	if(POWSW){
		get_TemmaCurrentpos(result);
		calcLST(result);

		/* This is a temporary workaround to allow
		   clients with only one eq property to work */
		currentRA=temmaRA;
		currentDec=temmaDec;

		/* again */
		IEAddTimer (POLLMS, readMountcurrentpos, NULL);
	}
}
#endif

static void connectMount () {
	fprintf(stderr,"opening mount port %s\n",PortT->text);
	if (power[0].s == ISS_ON){
		if (Port.s != IPS_OK){
			powSw.s = IPS_IDLE;
			power[0].s = ISS_OFF;

			IDSetSwitch (&powSw, "Port not set.");
			return;
		}
		else {
			if (TemmaConnect(PortT->text)==0){
				IDSetText (&Port, "Port is opened.");
				if( !get_TemmaVERSION(buffer)){
					powSw.s = IPS_OK;
					power[0].s = ISS_ON;
					power[1].s = ISS_OFF;
					snprintf(buffer2,sizeof( buffer2 ),"%s",buffer+4);

					IDSetText(&TemmaVersion , "Temma version set");
					TemmaVersionT->text = realloc(TemmaVersionT->text, strlen(buffer2)+1);
					if (!TemmaVersionT->text){
						fprintf(stderr,"Memory allocation error");
						return;
					}
					IDSetSwitch (&powSw, "Mount is ready");
					IDSetSwitch (&powSw, VERSION);

					strcpy(TemmaVersionT->text, buffer2);
					TemmaVersion.s = IPS_OK; 
					IDSetText (&TemmaVersion, NULL);
					IDLog("%s", buffer2);
					/* start timer to read mount coords */
					IEAddTimer (POLLMS, readMountcurrentpos, NULL);
				}
				else {
					powSw.s = IPS_IDLE;
					power[0].s = ISS_OFF;
					power[1].s = ISS_ON;
					IDSetText(&Port , "Com error");
					IDSetSwitch (&powSw, "Port not set.");
				}

				if(get_TemmaStandbyState(answer)){
					IDSetSwitch (&RAmotorSw, "Error writing to port");
					return;
				}
			}
			else {
				powSw.s = IPS_IDLE;
				power[0].s = ISS_OFF;
				power[1].s = ISS_ON;

				IDSetSwitch (&powSw, "Failed to open port.");
			}
		}
	}
}

static void disconnectMount () {
	fprintf(stderr,"closing mount port %s\n",PortT->text);
	if (power[1].s == ISS_ON){
		if (Port.s != IPS_OK){
			powSw.s = IPS_IDLE;
			power[0].s = ISS_OFF;

			IDSetSwitch (&powSw, "Port not set.");
			return;
		}
		else {
			TemmaDisconnect();
			powSw.s = IPS_IDLE;
			power[0].s = ISS_OFF;

			IDSetSwitch (&powSw, "Port is closed.");
		}
	}
	else {
		fprintf(stderr, "Already disconnected \n");
	}
}

int TemmaConnect(const char *device) {
	fprintf(stderr, "Connecting to device %s\n", device);

	if (openPort(device) < 0){
		fprintf(stderr, "Error connecting to device %s\n", device);
		return -1;
	}
	else{
		return 0;
	}
}

int TemmaDisconnect() {
	fprintf(stderr, "Disconnected.\n");
	close(fd);
	
	return 0;
}

int set_CometTracking(int RArate, int DECrate){
#if 0
	Set Comet Tracking
	LM+/-99999,+/-9999
	RA : Adjust Sidereal time by seconds per Day	

	DEC : Adjust DEC tracking by Minutes Per Day			
	valeur DEC min=-600 /max=+600 (+/-10 deg / jour)

	Example:
	LM+120,+30 would slow the RA speed by 86164/86284 and 		
	the Dec would track at 30 Minutes a day.
	To stop tracking either send a LM0,0 (or a PS 
	sauf erreur on constate en faite l inverse en RA
	retour Vsideral => LM0,0 ou LM+0,+0
#endif
	char local_buffer[16];

	if (RArate<-21541){
	RArate=-21541;
	}

	if (RArate>21541){
	RArate=21541;
	}

	if (DECrate<-600){
	DECrate=-600;
	}

	if (DECrate>600){
	DECrate=600;
	}

	snprintf(local_buffer, sizeof( local_buffer ), "%+6d,%+5d", RArate, DECrate);
	set_TemmaCometTracking(local_buffer);
	
	return 0;
}

int TemmaabortSlew() {
	if (portWrite("PS") < 0)
		return -1;

	return 0;
}

int do_TemmaGOTO() {
	/* Temma Sync */
	char command[16];
	char sign;
	double dec;

	calcLST(buffer);
	set_TemmaLST(buffer);

	dec=fabs(currentDec);
	if (currentDec>0){
		sign='+';
	}
	else {
		sign='-';
	}

	snprintf(buffer, sizeof(buffer),"%.2d%.2d%.2d%c%.2d%.2d%.1d",
			(int)currentRA,(int)(currentRA*(double)60)%60,((int)(currentRA*(double)6000))%100,sign,
			(int)dec,(int)(dec*(double)60)%60,((int)(dec*(double)600))%10);
	fprintf(stderr,"Goto %s\n", buffer);

	snprintf(command,14,"P%s",buffer);
	buffer[14]=0;
	fprintf(stderr,"Goto command:%s\n", command);
	portWrite(command);

	portRead(buffer,-1,TEMMA_TIMEOUT);
	if(command[0]=='R'){
		return 0;
	}
	else
		return -1;
}

int extractRA(char *buf){
	int r,h,m,s;
	/*double dra;*/

	r=atoi(buf);
	h=r/10000;
	m=r/100-100*h;
	s=(r%100)*.6;
	temmaRA=((double)h+((double)m + (double)s/60)/60);
	IDSetNumber (&eqTemma, NULL);
	/*	fprintf(stderr,"extractRA: %s %d %d %d %d %lf\n",buf,r,h,m,s,dra);*/
	return 0;
}

int extractDEC(char *buf){
	int dec,d,m,s;
	/*double ddec;*/

	dec=atoi(buf+1);
	d=dec/1000;
	m=dec/10-100*d;
	s=(dec%10)*6;
	temmaDec=((double)d+((double)m + (double)s/60)/60);
	if (*buf=='-')	
		temmaDec*=-1;

	IDSetNumber (&eqTemma, NULL);


	/*	fprintf(stderr,"extractDEC: %s %d %d %d %d %lf\n",buf,dec,d,m,s,ddec);*/
	return 0;
}


int get_TemmaCurrentpos(char *local_buffer){
	char buf[16];

	if (portWrite("E") < 0)
		return -1;

	if(portRead(local_buffer,-1,TEMMA_TIMEOUT)==SUCCESS){
		if(strstr(local_buffer, "E")==local_buffer){
			strncpy(buf,local_buffer+1,6);
			buf[6]=0;
			extractRA(buf);

			strncpy(buf,local_buffer+7,6);
			buf[6]=0;
			extractDEC(buf);
			return 0;
		}
		else {
			return -1;
		}
	}
	
	return 0;
}

int set_TemmaCurrentpos(void) {
	/* Temma Sync */
	char buf[16], sign;
	double dec;

	calcLST(buf);
	set_TemmaLST(buf);
	portWrite("Z");
	calcLST(buf);
	set_TemmaLST(buf);

	dec=fabs(currentDec);
	if (currentDec>0){
		sign='+';
	}
	else {
		sign='-';
	}

	sprintf(buffer,"%.2d%.2d%.2d%c%.2d%.2d%.1d",
			(int)currentRA,(int)(currentRA*(double)60)%60,((int)(currentRA*(double)6000))%100,sign,
			(int)dec,(int)(dec*(double)60)%60,((int)(dec*(double)600))%10);
	fprintf(stderr,"sync to %s %f %f\n", buffer,currentRA,dec);

	snprintf(buf, sizeof(buf), "D%s",buffer);
	buf[13]=0;
	portWrite(buf);
	*buffer=0;

	portRead(buffer,-1,TEMMA_TIMEOUT);
	if(buffer[0]=='R'){
		return 0;
	}
	else{
		return -1;
	}
}

int do_TemmaSLEW(char mode){
  	/*char command[16];*/

	sprintf(buffer,"M%c",mode); /* see bit definition in Temmadriver.h */

	if (portWrite(buffer) < 0)
		return -1;
	
	return 0;
}

int get_TemmaVERSION(char *local_buffer){
	int err;

	if ((err=portWrite("v")) < 0){
		return err;
	}

	portRead(local_buffer,-1,TEMMA_TIMEOUT);
	if(strstr(local_buffer, "ver")==local_buffer){
		return 0;
	}
	else
		return EREAD;
}

int get_TemmaGOTOstatus(char *local_buffer){
	/* 
	   0 no ongoing goto
	   1 ongoing Goto
	   -1 error
	 */
	if (portWrite("s") < 0)
		return -1;

	portRead(local_buffer,-1,TEMMA_TIMEOUT);
	if(strstr(local_buffer, "s")==local_buffer){
		return 0;
	}
	else
		return -1;
}

int get_TemmaBOTHcorrspeed(char *local_buffer){
	if (portWrite("lg") < 0)
		return -1;
	portRead(local_buffer,-1,TEMMA_TIMEOUT);
	if(strstr(local_buffer, "lg")==local_buffer){
		return 0;
	}
	else
		return -1;
}

int get_TemmaDECcorrspeed(char *local_buffer){
	if (portWrite("lb") < 0)
		return -1;
	portRead(local_buffer,-1,TEMMA_TIMEOUT);
	if(strstr(local_buffer, "lb")==local_buffer){
		return 0;
	}
	else
		return -1;
}

int set_TemmaDECcorrspeed(char *local_buffer){
	char command[16];

	snprintf(command, 4, "LB%s",local_buffer);

	if (portWrite(command) < 0)
		return -1;
	return 0;
}

int get_TemmaRAcorrspeed(char *local_buffer){
	if (portWrite("la") < 0)
		return -1;

	portRead(local_buffer,-1,TEMMA_TIMEOUT);
	if(strstr(local_buffer, "la")==local_buffer){
		return 0;
	}
	else
		return -1;
}

int set_TemmaRAcorrspeed(char *local_buffer){
	char command[16];

	snprintf(command, 4,"LA%s",local_buffer);

	if (portWrite(command) < 0)
		return -1;
	return 0;
}

int get_TemmaLatitude(char *local_buffer){
	if (portWrite("i") < 0)
		return -1;
	portRead(local_buffer,-1,TEMMA_TIMEOUT);
	if(local_buffer[0]=='i'){
		return 0;
	}
	else 
		return -1;
}

int set_TemmaLatitude(char *local_buffer){
	char command[16];
	double lat;
	char sign;

	lat=fabs(latitude);
	if (latitude>0){
		sign='+';
	}
	else {
		sign='-';
	}

	sprintf(command,"I%c%.2d%.2d%.1d", sign,
			(int)lat,
			(int)(lat*(double)60)%60,
			((int)(lat*(double)600))%10);

	if (portWrite(command) < 0)
		return -1;
	return 0;
}

int get_TemmaLST(char *local_buffer){
	if (portWrite("g") < 0)
		return -1;

	portRead(local_buffer,-1,TEMMA_TIMEOUT);
	if(local_buffer[0]=='g'){
		return 0;
	}
	else 
		return -1;
}


int set_TemmaLST(char *local_buffer){
	char command[16];
	snprintf(command,7,"T%s",local_buffer);

	if (portWrite(command) < 0)
		return -1;
	return 0;
}


int get_TemmaCometTracking(char *local_buffer){
	if (portWrite("lm") < 0)
		return -1;
	portRead(local_buffer,-1,TEMMA_TIMEOUT);
	if(strstr(local_buffer, "lm")==local_buffer){
		return 0;
	}
	else 
		return -1;
}

int set_TemmaStandbyState(int on){
	if (on){
		return portWrite("STN-ON");
	}
	else{
		return portWrite("STN-OFF");
	}
	return 0;
}

int get_TemmaStandbyState(unsigned char *local_buffer){
	int nb;
	int status;

	if ((nb=portWrite("STN-COD")) < 0){
		IDSetSwitch (&RAmotorSw, "I/O error when asking RAmotor status");
		return -1;
	}

	if((status=portRead(local_buffer,-1,TEMMA_TIMEOUT)==SUCCESS)){
		if(strstr(local_buffer, "stn")==local_buffer){
			local_buffer[7]=0;
			if (strstr(local_buffer,"on")){ /* stanby on */
				RAmotorSw.s = IPS_OK;
				RAmotor[0].s = ISS_OFF;
				RAmotor[1].s = ISS_ON;
				IDSetSwitch (&RAmotorSw, "RA motor is off.");
			}
			else{
				if (strstr(local_buffer,"off")){ /* stanby off */ 
					RAmotorSw.s = IPS_OK;
					RAmotor[0].s = ISS_ON;
					RAmotor[1].s = ISS_OFF;
					IDSetSwitch (&RAmotorSw, "RA motor is on.");
				}
				else {
					RAmotorSw.s = IPS_OK;
					IDSetSwitch (&RAmotorSw, "I/O error when getting RAmotor status");
				}
			}
		}
		return 0;
	}
	if (status<=ETIMEOUT && status >=ECOMMAND){
		IDSetSwitch(&RAmotorSw, "%s", errormes[ETIMEOUT - status]);
	}
	
	return -1;
}

int set_TemmaCometTracking(char *local_buffer){
	char command[16];

	snprintf(command,15,"LM%s",local_buffer);
	if (portWrite(command) < 0)
		return -1;
	return 0;
}

int set_TemmaSolarRate(void){
	if (portWrite("LK") < 0)
		return -1;
	return 0;
}

int set_TemmaStellarRate(void){
	if (portWrite("LL") < 0)
		return -1;
	return 0;
}


int switch_Temmamountside(void){
	if (portWrite("PT") < 0)
		return -1;
	return 0;
}

/**********************************************************************
 * Comm
 **********************************************************************/

int openPort(const char *portID) {
	struct termios ttyOptions;

	if ( (fd = open(portID, O_RDWR)) == -1)
		return -1;
	memset(&ttyOptions, 0, sizeof(ttyOptions));
	tcgetattr(fd, &ttyOptions);

	/* 8 bit, enable read */
	ttyOptions.c_cflag |= CS8;
	/* parity */
	ttyOptions.c_cflag |= PARENB;
	ttyOptions.c_cflag &= ~PARODD;
	ttyOptions.c_cflag &= ~CSTOPB;
	ttyOptions.c_cflag |= CRTSCTS;

	/* set baud rate */
	cfsetispeed(&ttyOptions, B19200);
	cfsetospeed(&ttyOptions, B19200);

	/* set input/output flags */
	ttyOptions.c_iflag = IGNBRK;

	/* Read at least one byte */
	ttyOptions.c_cc[VMIN] = 1;
	ttyOptions.c_cc[VTIME] = 5;

	/* Misc. */
	ttyOptions.c_lflag = 0;
	ttyOptions.c_oflag = 0;

	/* set attributes */
	tcsetattr(fd, TCSANOW, &ttyOptions);

	/* flush the channel */
	tcflush(fd, TCIOFLUSH);
	return (fd);
}

int portWrite(char * buf) {
  int nbytes=strlen(buf); /*, totalBytesWritten;*/
	int bytesWritten = 0; 
	/*int retry=10;*/

	bytesWritten = write(fd, buf, nbytes);
	bytesWritten += write(fd, "\r\n", 2);
	/*fprintf(stderr,"portwrite :%d octets %s\n", bytesWritten, buf);*/

	if (bytesWritten!=nbytes+2){
		perror("write error: ");
		IDLog("Error writing to port");
		return EWRITE;
	}
	return (bytesWritten);
}

int portRead(char *buf, int nbytes, int timeout) {
	/*
	   A very basic finite state machine monitors
	   the bytes read ; 
	   state 0 : read regular bytes
	   state 1 : just read a \n, waiting for a \r
	   state 2 : read a \n and a \r, command is over.

	   Not sure it is useful here but I use a more
	   sophisticated version of this with a GPS receiver
	   and it is robust and reliable

	   We return a null terminated string.
	 */

  int bytesRead = 0, state=0, /*i=0,*/ current=0;
  /*int totalBytesRead = 0;*/
	int err;

	if ( (err = TemmareadOut(timeout)) ){
		switch (err){
			case ETIMEOUT:
				IDLog("Error: timeout while reading");
				return err;
		}
	}

	while (read(fd,buf+bytesRead,1)==1){
		/*		fprintf(stderr,"%c",buf[bytesRead]); */
		fflush(NULL);
		switch (state) {
			case 0:
				if(buf[bytesRead]==13)
					state=1;
				break;

			case 1:
				if(buf[bytesRead]==10)
					state=2;
				else
					if(buf[bytesRead]==13)
						state=1;
					else
						state=0;
				break;
		}

		++current;

		if (state==2){
			/*process(buf);*/
			buf[bytesRead+1]=0;
			state=current=0;
			return SUCCESS;
		}

		bytesRead=current;
	}
	return state;
}

int TemmareadOut(int timeout) {
	struct timeval tv;
	fd_set readout;
	int retval;

	FD_ZERO(&readout);
	FD_SET(fd, &readout);

	/* wait for 'timeout' seconds */
	tv.tv_sec = timeout;
	tv.tv_usec = 0;

	/* Wait till we have a change in the fd status */
	retval = select (fd+1, &readout, NULL, NULL, &tv);

	/* Return 0 on successful fd change */
	if (retval > 0)
		return 0;
	/* Return -1 due to an error */
	else if (retval == EREAD)
		return retval;
	/* Return -2 if time expires before anything interesting happens */
	else {
		return ETIMEOUT;
	}
}