forked from Observatory-Sciences/powerPMAC_ssh
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPowerPMACcontrol.cpp
More file actions
3075 lines (2782 loc) · 110 KB
/
PowerPMACcontrol.cpp
File metadata and controls
3075 lines (2782 loc) · 110 KB
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
/********************************************
* PowrePMACcontrol.cpp
*
* A class which manages the communication with Power PMAC
* over a SSH connection.
*
* Aya Yoshimura <aya@observatorysciences.co.uk>
*
********************************************/
/**
* @mainpage
* PowerPMACcontrol is a C++ communications library which provides a set of functions
* to send commands to, and get status from a Delta Tau PowerPMAC motion controller.\n
* Communication is performed using the SSH protocol over Ethernet.\n
* Supported platforms are Linux and Windows (Microsoft Visual Studio C++).
*/
/**
* @file PowerPMACcontrol.cpp
* @brief C++ source file for the PowerPMACcontrol_ns::PowerPMACcontrol class. This file implements the class and its functions.
*
* The PowerPMACcontrol class manages the communication with Power PMAC.
* To connect to the Power PMAC, call PowerPMACcontrol_connect function with
* IP Address, user name and password parameters.
*/
//#include <sstream>
#include <fstream>
#include "PowerPMACcontrol.h"
#ifndef WIN32
#include <errno.h>
#endif
namespace PowerPMACcontrol_ns
{
/**
* @brief Destructor for the PowerPMACcontrol.
*
* If it is still connected to a Power PMAC, it will disconnect it.
*/
PowerPMACcontrol::~PowerPMACcontrol() {
debugPrint_ppmaccomm("~PowerPMACcontrol() called\n");
if (connected!=0)
{
this->PowerPMACcontrol_disconnect();
}
delete sshdriver;
#ifndef WIN32
sem_destroy(&sem_writeRead);
#endif
}
/**
* Constructor for the PowerPMACcontrol.
*/
PowerPMACcontrol::PowerPMACcontrol(){
sshdriver = NULL;
connected = 0;
// Initialise the timeout
common_timeout_ms = DEFAULT_TIMEOUT_MS;
#ifdef WIN32
ghSemaphore = CreateSemaphore(
NULL,
1,
1,
NULL);
if (this->ghSemaphore == NULL)
#else
int semaphore_ret = sem_init(&sem_writeRead, 0,1);
if (semaphore_ret != 0)
#endif
{
debugPrint_ppmaccomm("PowerPMACcontrol() : Error creating a semaphore\n");
}
else
debugPrint_ppmaccomm("PowerPMACcontrol() : a semaphore created\n");
}
/**
* @brief Attempt to create an SSH connection to Power PMAC using the username
* and the password.
*
* Once the connection has
* been established, it will start gpascii program on the Power PMAC.
*
* @param host - Host name/IP address of the Power PMAC
* @param user - User name for the SSH connection
* @param pwd - Password for the SSH connection
* @param port - Port number for the SSH connection (default 22)
* @param nominus2 - If true, remove the '-2' option on the command sent to start gpascii:
* 'gpascii -2' (nominus2 = false, default),
* 'gpascii' (nominus2 = true).
* @return If successful, PPMACcontrolNoError(0) is returned. If not,
* minus value is returned. Possible error codes are :
* - PPMACcontrolNoError(0)
* - PPMACcontrolSSHDriverError (-102)
* - PPMACcontrolSSHDriverErrorNoconn (-104)
* - PPMACcontrolSSHDriverErrorNobytes (-103)
* - PPMACcontrolSSHDriverErrorWriteTimeout (-113)
* - PPMACcontrolSSHDriverErrorReadTimeout (-112)
* - PPMACcontrolSSHDriverErrorUnknownHost (-114)
* - PPMACcontrolSSHDriverErrorSshInit (-110)
* - PPMACcontrolSSHDriverErrorSockfail (-109)
* - PPMACcontrolSSHDriverErrorSshSession (-111)
* - PPMACcontrolSSHDriverErrorPassword (-105)
* - PPMACcontrolSSHDriverErrorPublicKey (-107)
* - PPMACcontrolSSHDriverErrorPty (-106)
* - PPMACcontrolSSHDriverErrorShell (-108)
* - PPMACcontrolNoSSHDriverSet (-230)
* - PPMACcontrolSemaphoreTimeoutError (-239)
* - PPMACcontrolSemaphoreError (-240)
* - PPMACcontrolSemaphoreReleaseError (-241)
* .
*/
int PowerPMACcontrol::PowerPMACcontrol_connect(const char *host, const char *user,
const char *pwd, const char *port, const bool nominus2){
static const char *functionName = "PowerPMACcontrol::PowerPMACcontrol_connect";
if (strlen(host) > 255)
{
debugPrint_ppmaccomm("%s : Error - Host name too long\n", functionName);
//libssh2driver can't take host name longer than 255.
return PPMACcontrolInvalidHostNameError;
}
int return_val = PPMACcontrolNoError;
//get semaphore
#ifdef WIN32
DWORD dwWaitResult = WaitForSingleObject(this->ghSemaphore, INFINITE);
if (dwWaitResult == WAIT_OBJECT_0)
#else
int sem_return = sem_wait(&sem_writeRead);
if (sem_return == 0)
#endif
{
// In case it's already connected
if (sshdriver!=NULL) //If sshdriver has been created
{
printf("this connected is %d\n",this->connected );
this->PowerPMACcontrol_disconnect();
printf("this connected is %d\n",this->connected );
delete sshdriver;
}
sshdriver = new SSHDriver( host );
SSHDriverStatus ret = sshdriver->setUsername(user);
if (ret != SSHDriverSuccess)
{
if (ret == SSHDriverErrorInvalidParameter)
return_val = PPMACcontrolInvalidUserNameError;
else
return_val = PPMACcontrolSSHDriverError; //This can't happen
}
else
{
ret = sshdriver->setPassword(pwd);
if (ret != SSHDriverSuccess)
{
if (ret == SSHDriverErrorInvalidParameter)
return_val = PPMACcontrolInvalidPasswordError;
else
return_val = PPMACcontrolSSHDriverError; //This can't happen
}
else
{
debugPrint_ppmaccomm("%s : Setting port to %s\n", functionName, port);
ret = sshdriver->setPort(port);
if (ret != SSHDriverSuccess)
{
if (ret == SSHDriverErrorInvalidParameter)
return_val = PPMACcontrolInvalidPortError;
else
return_val = PPMACcontrolSSHDriverError; //This can't happen
}
else
{
ret = sshdriver->connectSSH();
if (ret != SSHDriverSuccess)
{
int estatus;
switch (ret){
case SSHDriverErrorUnknownHost:
estatus = PPMACcontrolSSHDriverErrorUnknownHost;
break;
case SSHDriverErrorSshInit:
estatus = PPMACcontrolSSHDriverErrorSshInit;
break;
case SSHDriverErrorSockfail:
estatus = PPMACcontrolSSHDriverErrorSockfail;
break;
case SSHDriverErrorSshSession:
estatus = PPMACcontrolSSHDriverErrorSshSession;
break;
case SSHDriverErrorPassword:
estatus = PPMACcontrolSSHDriverErrorPassword;
break;
case SSHDriverErrorPublicKey:
estatus = PPMACcontrolSSHDriverErrorPublicKey;
break;
case SSHDriverErrorPty:
estatus = PPMACcontrolSSHDriverErrorPty;
break;
case SSHDriverErrorShell:
estatus = PPMACcontrolSSHDriverErrorShell;
break;
case SSHDriverErrorInvalidParameter:
estatus = PPMACcontrolSSHDriverErrorInvalidParameter;
break;
default:
estatus = PPMACcontrolSSHDriverError;
}
return_val = estatus;
}
else
{
//Start the gpascii program on the powerPmac
char buff[512] = "";
size_t bytes = 0;
if (nominus2)
{
// don't use the -2 option
strcpy(buff, "gpascii\n");
}
else
{
// use the -2 option
strcpy(buff, "gpascii -2\n");
}
debugPrint_ppmaccomm("%s : Writing '%s' to the powerpmac\n", functionName, buff);
int ret2 = this->PowerPMACcontrol_write(buff, strlen(buff), &bytes, 1000);
if (ret2 != PPMACcontrolNoError)
{
debugPrint_ppmaccomm("%s : Error while writing 'gpascii -2' to the powerpmac\n", functionName);
return_val = ret2;
}
else
{
debugPrint_ppmaccomm("%s : Reading reply to 'gpascii -2' from the powerpmac\n", functionName);
ret2 = this->PowerPMACcontrol_read(buff, 512, &bytes, '\n', 2000);
if (ret2 != PPMACcontrolNoError)
{
debugPrint_ppmaccomm("%s : Error while reading reply to 'gpascii -2' command from the powerpmac\n", functionName);
return_val = ret2;
}
else
{
debugPrint_ppmaccomm("%s : Setting 'echo7' to the powerpmac\n", functionName);
strcpy(buff, "echo7\n");
debugPrint_ppmaccomm("%s : Writing 'echo7' to the powerpmac\n", functionName);
ret2 = this->PowerPMACcontrol_write(buff, strlen(buff), &bytes, 1000);
if (ret2 != PPMACcontrolNoError)
{
debugPrint_ppmaccomm("%s : Error while writing 'echo7' to the powerpmac\n", functionName);
return_val = ret2;
}
else
{
ret2 = this->PowerPMACcontrol_read(buff, 512, &bytes, '\n', 2000);
if (ret2 != PPMACcontrolNoError)
{
debugPrint_ppmaccomm("%s : Error while reading reply to 'gpascii' command from the powerpmac\n", functionName);
return_val = ret2;
}
else
this->connected = 1;
}
}
}
}
}
}
}
//Release Semaphore
#ifdef WIN32
if (!ReleaseSemaphore(ghSemaphore, 1, NULL))
{
return_val = PPMACcontrolSemaphoreReleaseError;
debugPrint_ppmaccomm("%s : Error releasing Semaphore\n",functionName);
}
else
{
debugPrint_ppmaccomm("%s : Released Semaphore\n",functionName);
}
#else
if (sem_post(&sem_writeRead)!=0)
{
return_val = PPMACcontrolSemaphoreReleaseError;
debugPrint_ppmaccomm("%s : Error releasing Semaphore\n",functionName);
}
else
{
debugPrint_ppmaccomm("%s : Released Semaphore\n",functionName);
}
#endif
return return_val;
}
#ifdef WIN32
else if (dwWaitResult == WAIT_TIMEOUT)
{
debugPrint_ppmaccomm("%s : Semaphore timed out\n",functionName);
return PPMACcontrolSemaphoreTimeoutError;
}
else
{
debugPrint_ppmaccomm("%s : Error obtaining a Semaphore (%d)\n",functionName, dwWaitResult);
return PPMACcontrolSemaphoreError;
}
#else
else
{
if (errno == ETIMEDOUT)
{
debugPrint_ppmaccomm("%s : Semaphore timed out\n",functionName);
return PPMACcontrolSemaphoreTimeoutError;
}
else
{
debugPrint_ppmaccomm("%s : Semaphore error %d\n",functionName, errno);
return PPMACcontrolSemaphoreError;
}
}
#endif
}
/**
* @brief Close the SSH connection.
*
* @return If successful, PPMACcontrolNoError(0) is returned.
* If not PPMACcontrolSSHDriverError (-102).
*/
int PowerPMACcontrol::PowerPMACcontrol_disconnect(){
// static const char *functionName = "PowerPMACcontrol::PowerPMACcontrol_disconnect";
if (this->sshdriver != NULL)
{
int ret = sshdriver->disconnectSSH();
if (ret == SSHDriverSuccess)
{
this->connected = 0;
return PPMACcontrolNoError;
}
return PPMACcontrolSSHDriverError; //sshdriver->disconnectSSH() always return No error.
}
return PPMACcontrolNoError; //Not connected. No error.
}
/**
* @brief Checks the state of the SSH connection to the Power PMAC.
*
* If a connection is open, a global status command is sent; the function will check that the command is sent and the reply received successfully.
*
* @param timeout Connection timeout in milliseconds. If not specified, the common timeout is used: default 1000 ms; this can be updated using PowerPMACcontrol_setTimeout.
* @return Return true if a connection is open and operating normally. Return false if communication fails or times out, or if no connection is open.
*/
bool PowerPMACcontrol::PowerPMACcontrol_isConnected(int timeout){
// If no timeout specified, use the common value
if (timeout == TIMEOUT_NOT_SPECIFIED)
{
timeout = common_timeout_ms;
}
if (this->connected > 0)
{
// Connection has been opened
// Send a global status command; check for successful send and receive
char cmd[128] = {0};
sprintf( cmd, "?\n");
std::string reply;
int ret = writeRead(cmd, reply, timeout);
if (ret != PPMACcontrolNoError)
{
// Communication error
return false;
}
if (reply.length() != 9)
{
// Expected reply not received
return false;
}
else
{
// Command send/receive OK
return true;
}
}
else
{
// Connection has not been opened
return false;
}
}
/**
* @brief Return the length of the communications timeout used for all functions
* @param timeout_ms Variable to receive the timeout in milliseconds
* @return Always returns PPMACcontrolNoError(0) because no communication occurs in this function
*/
int PowerPMACcontrol::PowerPMACcontrol_getTimeout(int & timeout_ms){
timeout_ms = common_timeout_ms;
return PPMACcontrolNoError;
}
/**
* @brief Set the length of the communications timeout used for all functions
* @param timeout_ms Timeout in milliseconds
* @return If successful, PPMACcontrolNoError (0) is returned.
* If an invalid timeout value is entered, PPMACcontrolInvalidParamError (-242) is returned.
*/
int PowerPMACcontrol::PowerPMACcontrol_setTimeout(int timeout_ms){
// Check validity of supplied value
if (timeout_ms > 0){
common_timeout_ms = timeout_ms;
return PPMACcontrolNoError;
}
else
{
// Return error code to indicate invalid timeout parameter supplied
return PPMACcontrolInvalidParamError;
}
}
/**
* @brief Write data to the connected SSH channel.
*
* A timeout should be specified in milliseconds.
*
* @param buffer - The string buffer to be written.
* @param bufferSize - The number of bytes to write.
* @param bytesWritten - The number of bytes that were written.
* @param timeout - A timeout in ms for the write.
* @return If successful, PPMACcontrolNoError(0) is returned. If not,
* minus value is returned. Possible error codes are :
* - PPMACcontrolNoError(0)
* - PPMACcontrolNoSSHDriverSet (-230)
* - PPMACcontrolSSHDriverError (-102)
* - PPMACcontrolSSHDriverErrorNoconn (-104)
* - PPMACcontrolSSHDriverErrorNobytes (-103)
* - PPMACcontrolSSHDriverErrorWriteTimeout (-113)
*/
int PowerPMACcontrol::PowerPMACcontrol_write(const char *buffer, size_t bufferSize, size_t *bytesWritten, int timeout)
{
static const char *functionName = "PowerPMACcontrol::PowerPMACcontrol_write";
//if (this->connected == 0)
//{
// debugPrint_ppmaccomm("%s : PMAC is not connected\n", functionName);
// return PPMACcontrolNoSSHDriverSet;
//}
if (sshdriver == NULL)
{
debugPrint_ppmaccomm("%s : SSH driver not set\n", functionName);
return PPMACcontrolNoSSHDriverSet;
}
int ret = sshdriver->write(buffer, bufferSize, bytesWritten, timeout);
if (ret == SSHDriverSuccess)
{
return PPMACcontrolNoError;
}
else
{
int estatus;
switch (ret){
case SSHDriverErrorNoconn:
estatus = PPMACcontrolSSHDriverErrorNoconn;
break;
case SSHDriverErrorNobytes:
estatus = PPMACcontrolSSHDriverErrorNobytes;
break;
case SSHDriverErrorWriteTimeout:
estatus = PPMACcontrolSSHDriverErrorWriteTimeout;
break;
default:
estatus = PPMACcontrolSSHDriverError;
}
return estatus;
}
}
/**
* @brief Read data from the connected SSH channel.
*
* A timeout should be
* specified in milliseconds. The read method will continue to
* read data from the channel until either the specified
* terminator is read or the timeout is reached.
*
* @param buffer - A string buffer to hold the read data.
* @param bufferSize - The maximum number of bytes to read.
* @param bytesWritten - The number of bytes that have been read.
* @param readTerm - A terminator to use as a check for EOM (End Of Message).
* @param timeout - A timeout in ms for the read.
* @return If successful, PPMACcontrolNoError(0) is returned. If not,
* minus value is returned. Possible error codes are :
* - PPMACcontrolNoError(0)
* - PPMACcontrolNoSSHDriverSet (-230)
* - PPMACcontrolSSHDriverError (-102)
* - PPMACcontrolSSHDriverErrorNoconn (-104)
* - PPMACcontrolSSHDriverErrorNobytes (-103)
* - PPMACcontrolSSHDriverErrorReadTimeout (-112)
* - PPMACcontrolSSHDriverErrorWriteTimeout (-113)
*/
int PowerPMACcontrol::PowerPMACcontrol_read(char *buffer, size_t bufferSize, size_t *bytesRead, int readTerm, int timeout)
{
static const char *functionName = "PowerPMACcontrol::powerpmac_connect";
//if (this->connected == 0)
if (this->sshdriver == NULL)
{
debugPrint_ppmaccomm("%s : SSH driver is not set\n", functionName);
return PPMACcontrolNoSSHDriverSet;
}
debugPrint_ppmaccomm("%s : buffer size is %d\n", functionName, bufferSize);
SSHDriverStatus ret = sshdriver->read(buffer, bufferSize, bytesRead, readTerm, timeout);
if (ret == SSHDriverSuccess)
{
return PPMACcontrolNoError;
}
else
{
int estatus;
switch (ret){
case SSHDriverErrorNoconn:
estatus = PPMACcontrolSSHDriverErrorNoconn;
break;
case SSHDriverErrorReadTimeout:
estatus = PPMACcontrolSSHDriverErrorReadTimeout;
break;
default:
estatus = PPMACcontrolSSHDriverError;
}
return estatus;
}
}
/**
* @brief Get firmware version.
*
* Power PMAC command string sent = "vers"
* @param vers - Firmware version will be written to this parameter.
* @return If version is successfully received, PPMACcontrolNoError(0) is returned. If not,
* minus value is returned. Possible error codes are :
* - PPMACcontrolNoError(0)
* - PPMACcontrolNoSSHDriverSet (-230)
* - PPMACcontrolSSHDriverError (-102)
* - PPMACcontrolSSHDriverErrorNoconn (-104)
* - PPMACcontrolSSHDriverErrorNobytes (-103)
* - PPMACcontrolSSHDriverErrorReadTimeout (-112)
* - PPMACcontrolSSHDriverErrorWriteTimeout (-113)
* - PPMACcontrolSemaphoreTimeoutError = (-239)
* - PPMACcontrolSemaphoreError = (-240)
* - PPMACcontrolSemaphoreReleaseError = (-241)
*/
int PowerPMACcontrol::PowerPMACcontrol_getVers(std::string& vers){
// static const char *functionName = "PowerPMACcontrol::powerpmac_getVers";
char cmd[128] = "";
sprintf( cmd, "vers\n");
return this->writeRead(cmd,vers);
}
/**
* @brief Get IDs of embedded software
*
* Power PMAC command string sent = "buffer"
*
* @param num - Number of motion and program names.
* @param names - Names of the programs.
* @return If names of the programs are successfully received, PPMACcontrolNoError(0) is returned. If not,
* minus value is returned. Possible error codes are :
* - PPMACcontrolNoError(0)
* - Error reported from Power PMAC (-1 to -99) -1*(Power PMAC error number)
* - PPMACcontrolNoSSHDriverSet (-230)
* - PPMACcontrolSSHDriverError (-102)
* - PPMACcontrolSSHDriverErrorNoconn (-104)
* - PPMACcontrolSSHDriverErrorNobytes (-103)
* - PPMACcontrolSSHDriverErrorReadTimeout (-112)
* - PPMACcontrolSSHDriverErrorWriteTimeout (-113)
* - PPMACcontrolPMACUnexpectedReplyError (-231)
* - PPMACcontrolSemaphoreTimeoutError = (-239)
* - PPMACcontrolSemaphoreError = (-240)
* - PPMACcontrolSemaphoreReleaseError = (-241)
*/
int PowerPMACcontrol::PowerPMACcontrol_getProgNames(int& num, std::vector<std::string>& names){
static const char *functionName = "PowerPMACcontrol::PowerPMACcontrol_getProgNames";
debugPrint_ppmaccomm("%s called", functionName);
names.clear();
num=0;
char cmd[128] = {0};
sprintf( cmd, "buffer\n");
std::string reply;
int ret = writeRead(cmd, reply);
if (ret != PPMACcontrolNoError)
return ret;
debugPrint_ppmaccomm("%s : reply length is %d", functionName, reply.length());
if (reply.length() < 1)
{
return PPMACcontrolNoError; //Nothing returned.
}
debugPrint_ppmaccomm("%s : reply compare return is is %d", functionName, reply.compare("Buffer is empty"));
//Check if the reply is 'Buffer is empty'
if ((reply.compare("Buffer is empty")) == 0)
{
debugPrint_ppmaccomm("%s : Buffer is empty", functionName);
return PPMACcontrolNoError; //It is empty
}
std::vector<std::string> progstatus;
ret = PowerPMACcontrol::splitit(reply, "\r\n", progstatus);
if (ret != PPMACcontrolNoError)
return PPMACcontrolPMACUnexpectedReplyError;
try{
for (size_t i=0; i<progstatus.size(); i++)
{
std::vector<std::string> tmp;
ret = PowerPMACcontrol::splitit(progstatus.at(i), " ", tmp);
if (ret != PPMACcontrolNoError)
{
num=0;
names.clear();
return PPMACcontrolPMACUnexpectedReplyError;
}
if (tmp.size() == 0)
{
num=0;
names.clear();
return PPMACcontrolPMACUnexpectedReplyError;
}
names.push_back(tmp.at(0));
}
}
catch(...)
{
debugPrint_ppmaccomm("%s : Error trying to get Prog Names", functionName);
num=0;
names.clear();
return PPMACcontrolPMACUnexpectedReplyError;
}
num = names.size();
return PPMACcontrolNoError;
}
/**
* @brief Get powered state of a motor
*
* Power PMAC command string sent = "Motor[<motor index>].ServoCtrl"
*
* @param mnum - Index of a motor.
* @param powered - Powered state of the motor.
* @return If powered state of the motor is successfully received,
* PPMACcontrolNoError(0) is returned. If not,
* minus value is returned. Possible error codes are :
* - PPMACcontrolNoError(0)
* - Error reported from Power PMAC (-1 to -99) -1*(Power PMAC error number)
* - PPMACcontrolNoSSHDriverSet (-230)
* - PPMACcontrolSSHDriverError (-102)
* - PPMACcontrolSSHDriverErrorNoconn (-104)
* - PPMACcontrolSSHDriverErrorNobytes (-103)
* - PPMACcontrolSSHDriverErrorReadTimeout (-112)
* - PPMACcontrolSSHDriverErrorWriteTimeout (-113)
* - PPMACcontrolPMACUnexpectedReplyError (-231)
* - PPMACcontrolSemaphoreTimeoutError = (-239)
* - PPMACcontrolSemaphoreError = (-240)
* - PPMACcontrolSemaphoreReleaseError = (-241)
*/
int PowerPMACcontrol::PowerPMACcontrol_motorPowered(int mnum, bool& powered){
static const char *functionName = "PowerPMACcontrol::PowerPMACcontrol_MotorPowered";
if (this->connected == 0)
{
debugPrint_ppmaccomm("%s : PowerPMACcontrol::write: PMAC is not connected", functionName);
return PPMACcontrolNoSSHDriverSet;
}
char cmd[512] = "";
sprintf( cmd, "Motor[%d].ServoCtrl\n", mnum);
std::string reply="";
int ret = this->writeRead(cmd,reply);
if (ret != PPMACcontrolNoError)
return ret;
if (reply.compare("1") == 0)
{
powered = true;
return PPMACcontrolNoError;
}
if (reply.compare("0") == 0)
{
powered = false;
return PPMACcontrolNoError;
}
// The reply from Power PMAC was not as expected.
// Failed to get powered state.
return PPMACcontrolPMACUnexpectedReplyError;
}
/**
* @brief Get velocity of an axis
*
* Power PMAC command string sent = "Motor[<axis index>].JogSpeed"
*
* @param axis - Index of axis.
* @param velocity - Velocity of the axis
* @return If velocity of the axis is successfully received,
* PPMACcontrolNoError(0) is returned. If not,
* minus value is returned. Possible error codes are :
* - PPMACcontrolNoError(0)
* - Error reported from Power PMAC (-1 to -99) -1*(Power PMAC error number)
* - PPMACcontrolNoSSHDriverSet (-230)
* - PPMACcontrolSSHDriverError (-102)
* - PPMACcontrolSSHDriverErrorNoconn (-104)
* - PPMACcontrolSSHDriverErrorNobytes (-103)
* - PPMACcontrolSSHDriverErrorReadTimeout (-112)
* - PPMACcontrolSSHDriverErrorWriteTimeout (-113)
* - PPMACcontrolPMACUnexpectedReplyError (-231)
* - PPMACcontrolSemaphoreTimeoutError = (-239)
* - PPMACcontrolSemaphoreError = (-240)
* - PPMACcontrolSemaphoreReleaseError = (-241)
*/
int PowerPMACcontrol::PowerPMACcontrol_axisGetVelocity(int axis, double& velocity){
static const char *functionName = "PowerPMACcontrol::PowerPMACcontrol_axisGetVelocity";
debugPrint_ppmaccomm("%s called", functionName);
char cmd[128] = "";
sprintf( cmd, "Motor[%d].JogSpeed\n", axis);
std::string reply;
int ret = this->writeRead(cmd,reply);
if (ret != PPMACcontrolNoError)
return ret;
double dval = 0.0;
std::istringstream stream(reply);
stream >> dval;
if (stream.fail())
{
//failed to convert to double value
return PPMACcontrolPMACUnexpectedReplyError;
}
velocity = dval;
return PPMACcontrolNoError;
}
/**
* @brief Set velocity of an axis
*
* Power PMAC command string sent = "Motor[<axis index>].JogSpeed=<velocity>"
*
* @param axis - Index of axis.
* @param velocity - New velocity of the axis to be set
* @return If the velocity of the axis is set successfully,
* PPMACcontrolNoError(0) is returned. If not,
* minus value is returned. Possible error codes are :
* - PPMACcontrolNoError(0)
* - Error reported from Power PMAC (-1 to -99) -1*(Power PMAC error number)
* - PPMACcontrolNoSSHDriverSet (-230)
* - PPMACcontrolSSHDriverError (-102)
* - PPMACcontrolSSHDriverErrorNoconn (-104)
* - PPMACcontrolSSHDriverErrorNobytes (-103)
* - PPMACcontrolSSHDriverErrorReadTimeout (-112)
* - PPMACcontrolSSHDriverErrorWriteTimeout (-113)
* - PPMACcontrolSemaphoreTimeoutError = (-239)
* - PPMACcontrolSemaphoreError = (-240)
* - PPMACcontrolSemaphoreReleaseError = (-241)
*/
int PowerPMACcontrol::PowerPMACcontrol_axisSetVelocity(int axis, double velocity){
static const char *functionName = "PowerPMACcontrol::PowerPMACcontrol_axisSetVelocity";
debugPrint_ppmaccomm("%s called", functionName);
char cmd[128] = {0};
sprintf( cmd, "Motor[%d].JogSpeed=%f\n", axis, velocity);
return writeRead(cmd);
}
/**
* @brief Define position of an axis.
*
* This function sends a command to kill the axis movement and set position.\n
* Power PMAC command string sent = "#<axis index>k Motor[<axis index>].Pos=<new position>"
*
* @param axis - Index of axis.
* @param newpos - New position of the axis to be defined
* @return If the position of the axis is defined successfully,
* PPMACcontrolNoError(0) is returned. If not,
* minus value is returned. Possible error codes are :
* - PPMACcontrolNoError(0)
* - Error reported from Power PMAC (-1 to -99) -1*(Power PMAC error number)
* - PPMACcontrolNoSSHDriverSet (-230)
* - PPMACcontrolSSHDriverError (-102)
* - PPMACcontrolSSHDriverErrorNoconn (-104)
* - PPMACcontrolSSHDriverErrorNobytes (-103)
* - PPMACcontrolSSHDriverErrorReadTimeout (-112)
* - PPMACcontrolSSHDriverErrorWriteTimeout (-113)
* - PPMACcontrolSemaphoreTimeoutError = (-239)
* - PPMACcontrolSemaphoreError = (-240)
* - PPMACcontrolSemaphoreReleaseError = (-241)
*/
int PowerPMACcontrol::PowerPMACcontrol_axisDefCurrentPos(int axis, double newpos){
static const char *functionName = "PowerPMACcontrol::PowerPMACcontrol_axisDefCurrentPos";
debugPrint_ppmaccomm("%s called", functionName);
char cmd[128] = {0};
sprintf( cmd, "#%dk Motor[%d].Pos=%f\n", axis, axis, newpos);
return writeRead(cmd);
}
/**
* @brief Get acceleration of an axis.
*
* If 'inf' or 'nan' is received from the Power PMAC,
* this function will return PPMACcontrolPMACUnexpectedReplyError (-231) and
* acceleration parameter will not be set.\n
* Power PMAC command string sent = "Motor[<axis index>].JogTa"
*
* @param axis - Index of axis.
* @param acceleration - Acceleration of the axis
* @return If acceleration of the axis is successfully received,
* PPMACcontrolNoError(0) is returned. If not,
* minus value is returned. Possible error codes are :
* - PPMACcontrolNoError(0)
* - Error reported from Power PMAC (-1 to -99) -1*(Power PMAC error number)
* - PPMACcontrolNoSSHDriverSet (-230)
* - PPMACcontrolSSHDriverError (-102)
* - PPMACcontrolSSHDriverErrorNoconn (-104)
* - PPMACcontrolSSHDriverErrorNobytes (-103)
* - PPMACcontrolSSHDriverErrorReadTimeout (-112)
* - PPMACcontrolSSHDriverErrorWriteTimeout (-113)
* - PPMACcontrolPMACUnexpectedReplyError (-231)
* - PPMACcontrolSemaphoreTimeoutError = (-239)
* - PPMACcontrolSemaphoreError = (-240)
* - PPMACcontrolSemaphoreReleaseError = (-241)
*/
int PowerPMACcontrol::PowerPMACcontrol_axisGetAcceleration(int axis, double& acceleration){
static const char *functionName = "PowerPMACcontrol::PowerPMACcontrol_axisGetAcceleration";
debugPrint_ppmaccomm("%s called", functionName);
char cmd[128] = "";
sprintf( cmd, "Motor[%d].JogTa\n", axis);
std::string reply;
int ret = this->writeRead(cmd,reply);
if (ret != PPMACcontrolNoError)
return ret;
std::istringstream stream(reply);
double dval;
stream >> dval;
if (stream.fail())
{
//failed to convert to double value
return PPMACcontrolPMACUnexpectedReplyError;
}
acceleration = dval;
return PPMACcontrolNoError;
}
/**
* @brief Set acceleration of an axis.
*
* Power PMAC command string sent = "Motor[<axis index>].JogTa=<new acceleration>"
*
* @param axis - Index of axis.
* @param acceleration - New acceleration of the axis to be set
* @return If the new acceleration of the axis is set successfully,
* PPMACcontrolNoError(0) is returned. If not,
* minus value is returned. Possible error codes are :
* - PPMACcontrolNoError(0)
* - Error reported from Power PMAC (-1 to -99) -1*(Power PMAC error number)
* - PPMACcontrolNoSSHDriverSet (-230)
* - PPMACcontrolSSHDriverError (-102)
* - PPMACcontrolSSHDriverErrorNoconn (-104)
* - PPMACcontrolSSHDriverErrorNobytes (-103)
* - PPMACcontrolSSHDriverErrorReadTimeout (-112)
* - PPMACcontrolSSHDriverErrorWriteTimeout (-113)
* - PPMACcontrolSemaphoreTimeoutError = (-239)
* - PPMACcontrolSemaphoreError = (-240)
* - PPMACcontrolSemaphoreReleaseError = (-241)
*/
int PowerPMACcontrol::PowerPMACcontrol_axisSetAcceleration(int axis, double acceleration){
static const char *functionName = "PowerPMACcontrol::PowerPMACcontrol_axisSetAcceleration";
debugPrint_ppmaccomm("%s called", functionName);
char cmd[128] = {0};
sprintf( cmd, "Motor[%d].JogTa=%f\n", axis, acceleration);
return writeRead(cmd);
}
/**
* @brief Get deadband of an axis.
*
* If 'inf' or 'nan' is received from the Power PMAC,
* this function will return PPMACcontrolPMACUnexpectedReplyError (-231) and
* the deadband parameter will not be set.\n
* Power PMAC command string sent = "Motor[<axis index>].Servo.OutDbOn"
*
* @param axis - Index of axis.
* @param deadband - Deadband of the axis
* @return If deadband of the axis is successfully received,
* PPMACcontrolNoError(0) is returned. If not,
* minus value is returned. Possible error codes are :
* - PPMACcontrolNoError(0)
* - Error reported from Power PMAC (-1 to -99) -1*(Power PMAC error number)
* - PPMACcontrolNoSSHDriverSet (-230)
* - PPMACcontrolSSHDriverError (-102)
* - PPMACcontrolSSHDriverErrorNoconn (-104)
* - PPMACcontrolSSHDriverErrorNobytes (-103)
* - PPMACcontrolSSHDriverErrorReadTimeout (-112)
* - PPMACcontrolSSHDriverErrorWriteTimeout (-113)
* - PPMACcontrolPMACUnexpectedReplyError (-231)
* - PPMACcontrolSemaphoreTimeoutError = (-239)
* - PPMACcontrolSemaphoreError = (-240)
* - PPMACcontrolSemaphoreReleaseError = (-241)
*/
int PowerPMACcontrol::PowerPMACcontrol_axisGetDeadband(int axis, double& deadband){
static const char *functionName = "PowerPMACcontrol::PowerPMACcontrol_axisGetDeadband";
debugPrint_ppmaccomm("%s called", functionName);
char cmd[128] = "";
sprintf( cmd, "Motor[%d].Servo.OutDbOn\n", axis);
std::string reply;
int ret = this->writeRead(cmd,reply);
if (ret != PPMACcontrolNoError)
return ret;
std::istringstream stream(reply);
double dval;
stream >> dval;
if (stream.fail())
{
//failed to convert to double value
return PPMACcontrolPMACUnexpectedReplyError;
}
deadband = dval;
return PPMACcontrolNoError;
}
/**
* @brief Set deadband of an axis.
*
* Power PMAC command string sent = "Motor[<axis index>].Servo.OutDbOn=<new deadband>"
*
* @param axis - Index of axis.
* @param deadband - New deadband of the axis to be set
* @return If the new deadband of the axis is set successfully,
* PPMACcontrolNoError(0) is returned. If not,
* minus value is returned. Possible error codes are :
* - PPMACcontrolNoError(0)
* - Error reported from Power PMAC (-1 to -99) -1*(Power PMAC error number)
* - PPMACcontrolNoSSHDriverSet (-230)
* - PPMACcontrolSSHDriverError (-102)
* - PPMACcontrolSSHDriverErrorNoconn (-104)
* - PPMACcontrolSSHDriverErrorNobytes (-103)
* - PPMACcontrolSSHDriverErrorReadTimeout (-112)
* - PPMACcontrolSSHDriverErrorWriteTimeout (-113)
* - PPMACcontrolSemaphoreTimeoutError = (-239)
* - PPMACcontrolSemaphoreError = (-240)
* - PPMACcontrolSemaphoreReleaseError = (-241)
*/
int PowerPMACcontrol::PowerPMACcontrol_axisSetDeadband(int axis, double deadband){
static const char *functionName = "PowerPMACcontrol::PowerPMACcontrol_axisSetDeadband";
debugPrint_ppmaccomm("%s called", functionName);
char cmd[128] = {0};
sprintf( cmd, "Motor[%d].Servo.OutDbOn=%f\n", axis, deadband);
return writeRead(cmd);
}
/**