-
Notifications
You must be signed in to change notification settings - Fork 0
/
getRobotName.c
83 lines (69 loc) · 1.69 KB
/
getRobotName.c
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
#include "connectToServer.h"
#include <stdio.h>
#ifdef WIN32
#include <winsock2.h>
#else
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <errno.h>
#endif
#define MAXLENGTH 500
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
char buf[MAXLENGTH];
char numStr[20];
int i,numRobots;
double *pind;
int sockd;
/* Check for proper number of arguments */
if (nrhs > 1) {
mexErrMsgTxt("getRobotName takes at most one input argument.");
} else if (nlhs > 1) {
mexErrMsgTxt("getRobotName takes at most one output argument.");
}
sockd = ConnectTo("localhost",4765);
if (sockd < 0)
mexErrMsgTxt("Could not connect");
strcpy(buf,"getRobotName ");
if (nrhs==0) {
strcat(buf,"ALL\n");
Writeline(sockd,buf,strlen(buf));
Readline(sockd,buf,MAXLENGTH);
sscanf(buf,"%d\n",&numRobots);
}
else {
numRobots = mxGetNumberOfElements(prhs[0]);
if (numRobots>0) {
sprintf(numStr,"%d ",numRobots);
strcat(buf,numStr);
pind = mxGetPr(prhs[0]);
for (i=0;i<numRobots-1;i++) {
sprintf(numStr,"%d ",(int)pind[i]-1);
strcat(buf,numStr);
}
sprintf(numStr,"%d\n",(int)pind[i]-1);
strcat(buf,numStr);
Writeline(sockd,buf,strlen(buf));
}
}
if (numRobots == 0) {
plhs[0] = NULL;
}
else {
plhs[0] = mxCreateCellArray(1, &numRobots);
for (i=0;i<numRobots;i++) {
Readline(sockd,buf,MAXLENGTH);
if (!strncmp(buf,"Error",5)) {
mexErrMsgTxt(buf);
break;
}
buf[strlen(buf)-1] = '\0';
mxSetCell(plhs[0],i,mxCreateString(buf));
}
}
CloseConnection(sockd);
return;
}