00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <sys/types.h>
00019 #include <sys/socket.h>
00020 #include <netinet/in.h>
00021 #include <arpa/inet.h>
00022 #include <cstdio>
00023 #include <cstdlib>
00024 #include <cstring>
00025 #include <unistd.h>
00026 #include <iostream>
00027 #include <errno.h>
00028
00029 #include "lms1xx.h"
00030
00031 LMS1xx::LMS1xx() :
00032 connected(false) {
00033 debug = false;
00034 }
00035
00036 LMS1xx::~LMS1xx() {
00037
00038 }
00039
00040 void LMS1xx::connect(std::string host, int port) {
00041 if (!connected) {
00042 sockDesc = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
00043 if (sockDesc) {
00044 struct sockaddr_in stSockAddr;
00045 int Res;
00046 stSockAddr.sin_family = PF_INET;
00047 stSockAddr.sin_port = htons(port);
00048 Res = inet_pton(AF_INET, host.c_str(), &stSockAddr.sin_addr);
00049
00050 int ret = ::connect(sockDesc, (struct sockaddr *) &stSockAddr,
00051 sizeof stSockAddr);
00052 if (ret == 0) {
00053 connected = true;
00054 }
00055 }
00056 }
00057 }
00058
00059 void LMS1xx::disconnect() {
00060 if (connected) {
00061 close(sockDesc);
00062 connected = false;
00063 }
00064 }
00065
00066 bool LMS1xx::isConnected() {
00067 return connected;
00068 }
00069
00070 void LMS1xx::startMeas() {
00071 char buf[100];
00072 sprintf(buf, "%c%s%c", 0x02, "sMN LMCstartmeas", 0x03);
00073
00074 write(sockDesc, buf, strlen(buf));
00075
00076 int len = read(sockDesc, buf, 100);
00077
00078
00079
00080
00081
00082
00083 }
00084
00085 void LMS1xx::stopMeas() {
00086 char buf[100];
00087 sprintf(buf, "%c%s%c", 0x02, "sMN LMCstopmeas", 0x03);
00088
00089 write(sockDesc, buf, strlen(buf));
00090
00091 int len = read(sockDesc, buf, 100);
00092
00093
00094
00095
00096
00097
00098 }
00099
00100 status_t LMS1xx::queryStatus() {
00101 char buf[100];
00102 sprintf(buf, "%c%s%c", 0x02, "sRN STlms", 0x03);
00103
00104 write(sockDesc, buf, strlen(buf));
00105
00106 int len = read(sockDesc, buf, 100);
00107
00108
00109
00110
00111
00112
00113 int ret;
00114 sscanf((buf + 10), "%d", &ret);
00115
00116 return (status_t) ret;
00117 }
00118
00119 void LMS1xx::login() {
00120 char buf[100];
00121 sprintf(buf, "%c%s%c", 0x02, "sMN SetAccessMode 03 F4724744", 0x03);
00122
00123 write(sockDesc, buf, strlen(buf));
00124
00125 int len = read(sockDesc, buf, 100);
00126
00127
00128
00129
00130
00131
00132 }
00133
00134 scanCfg LMS1xx::getScanCfg() const {
00135 scanCfg cfg;
00136 char buf[100];
00137 sprintf(buf, "%c%s%c", 0x02, "sRN LMPscancfg", 0x03);
00138
00139 write(sockDesc, buf, strlen(buf));
00140
00141 int len = read(sockDesc, buf, 100);
00142
00143
00144
00145
00146
00147
00148
00149 sscanf(buf + 1, "%*s %*s %X %*d %X %X %X", &cfg.scaningFrequency,
00150 &cfg.angleResolution, &cfg.startAngle, &cfg.stopAngle);
00151 return cfg;
00152 }
00153
00154 void LMS1xx::setScanCfg(const scanCfg &cfg) {
00155 char buf[100];
00156 sprintf(buf, "%c%s %X +1 %X %X %X%c", 0x02, "sMN mLMPsetscancfg",
00157 cfg.scaningFrequency, cfg.angleResolution, cfg.startAngle,
00158 cfg.stopAngle, 0x03);
00159
00160 write(sockDesc, buf, strlen(buf));
00161
00162 int len = read(sockDesc, buf, 100);
00163
00164 buf[len - 1] = 0;
00165 }
00166
00167 void LMS1xx::setScanDataCfg(const scanDataCfg &cfg) {
00168 char buf[100];
00169 sprintf(buf, "%c%s %02X 00 %d %d 0 %02X 00 %d %d 0 %d +%d%c", 0x02,
00170 "sWN LMDscandatacfg", cfg.outputChannel, cfg.remission ? 1 : 0,
00171 cfg.resolution, cfg.encoder, cfg.position ? 1 : 0,
00172 cfg.deviceName ? 1 : 0, cfg.timestamp ? 1 : 0, cfg.outputInterval, 0x03);
00173 if(debug)
00174 printf("%s\n", buf);
00175 write(sockDesc, buf, strlen(buf));
00176
00177 int len = read(sockDesc, buf, 100);
00178 buf[len - 1] = 0;
00179 }
00180
00181 void LMS1xx::scanContinous(int start) {
00182 char buf[100];
00183 sprintf(buf, "%c%s %d%c", 0x02, "sEN LMDscandata", start, 0x03);
00184
00185 write(sockDesc, buf, strlen(buf));
00186
00187 int len = read(sockDesc, buf, 100);
00188
00189 if (buf[0] != 0x02)
00190 printf("invalid packet recieved\n");
00191
00192 if (debug) {
00193 buf[len] = 0;
00194 printf("%s\n", buf);
00195 }
00196
00197 if (start = 0) {
00198 for (int i = 0; i < 10; i++)
00199 read(sockDesc, buf, 100);
00200 }
00201 }
00202
00203 bool LMS1xx::getData(scanData& data) {
00204 char buf[20000];
00205 fd_set rfds;
00206 struct timeval tv;
00207 int retval, len = 0;
00208 int bytes_read = 0;
00209 int inc = 0;
00210 int r_off = 0;
00211
00212 do{
00213 FD_ZERO(&rfds);
00214 FD_SET(sockDesc, &rfds);
00215 tv.tv_sec = 1;
00216 tv.tv_usec = 0;
00217 bytes_read = 0; inc = 0;
00218 retval = select(sockDesc + 1, &rfds, NULL, NULL, &tv);
00219 if(retval)
00220 {
00221 bytes_read = read(sockDesc, buf, 1);
00222 if(bytes_read > 1)
00223 {
00224
00225 inc = bytes_read;
00226 inc --;
00227 }
00228 else if(bytes_read < 1)
00229 {
00230
00231 }
00232 }
00233 else
00234 {
00235
00236 }
00237
00238 }while(buf[0+inc] != 0x02);
00239 len += bytes_read;
00240
00241
00242 do {
00243 bool bFoundEnd = false;
00244 bool bFoundStart = false;
00245 if(len >= 20000)
00246 {
00247 int i;
00248 for(i = 0; i < len; i++)
00249 {
00250 if(buf[i] == 0x03)
00251 {
00252 len = i+1;
00253 bFoundEnd = true;
00254
00255 }
00256 if(bFoundEnd && buf[i] == 0x02)
00257 {
00258
00259 bFoundStart = true;
00260 }
00261 }
00262 if(!bFoundEnd)
00263 {
00264 char flushBuffer[100000];
00265 int bytesFlushed = recv(sockDesc, flushBuffer, 100000, 0);
00266
00267 return false;
00268 }
00269 }
00270 if(!bFoundEnd)
00271 {
00272 FD_ZERO(&rfds);
00273 FD_SET(sockDesc, &rfds);
00274
00275 tv.tv_sec = 0;
00276 tv.tv_usec = 100000;
00277 bytes_read = 0;
00278 retval = select(sockDesc + 1, &rfds, NULL, NULL, &tv);
00279 if (retval)
00280 {
00281 bytes_read = read(sockDesc, buf + len, 20000 - len);
00282 if(bytes_read < 1)
00283 {
00284
00285 }
00286 else
00287 len += bytes_read;
00288 }
00289 else
00290 {
00291
00292 }
00293 }
00294 } while ((buf[len - 1] != 0x03));
00295
00296
00297
00298 buf[len - 1] = 0;
00299 char* tok = strtok(buf, " ");
00300 tok = strtok(NULL, " ");
00301 tok = strtok(NULL, " ");
00302 tok = strtok(NULL, " ");
00303 tok = strtok(NULL, " ");
00304 tok = strtok(NULL, " ");
00305 tok = strtok(NULL, " ");
00306 tok = strtok(NULL, " ");
00307 tok = strtok(NULL, " ");
00308 tok = strtok(NULL, " ");
00309 tok = strtok(NULL, " ");
00310 tok = strtok(NULL, " ");
00311 tok = strtok(NULL, " ");
00312 tok = strtok(NULL, " ");
00313 tok = strtok(NULL, " ");
00314 tok = strtok(NULL, " ");
00315 tok = strtok(NULL, " ");
00316 tok = strtok(NULL, " ");
00317 tok = strtok(NULL, " ");
00318 int NumberEncoders;
00319 sscanf(tok, "%d", &NumberEncoders);
00320 for (int i = 0; i < NumberEncoders; i++) {
00321 tok = strtok(NULL, " ");
00322 tok = strtok(NULL, " ");
00323 }
00324
00325 tok = strtok(NULL, " ");
00326 int NumberChannels16Bit;
00327 sscanf(tok, "%d", &NumberChannels16Bit);
00328 if (debug)
00329 printf("NumberChannels16Bit : %d\n", NumberChannels16Bit);
00330 for (int i = 0; i < NumberChannels16Bit; i++) {
00331 int type = -1;
00332 char content[6];
00333 tok = strtok(NULL, " ");
00334 sscanf(tok, "%s", content);
00335 if (!strcmp(content, "DIST1")) {
00336 type = 0;
00337 } else if (!strcmp(content, "DIST2")) {
00338 type = 1;
00339 } else if (!strcmp(content, "RSSI1")) {
00340 type = 2;
00341 } else if (!strcmp(content, "RSSI2")) {
00342 type = 3;
00343 }
00344 tok = strtok(NULL, " ");
00345 tok = strtok(NULL, " ");
00346 tok = strtok(NULL, " ");
00347 tok = strtok(NULL, " ");
00348 tok = strtok(NULL, " ");
00349 int NumberData;
00350 sscanf(tok, "%X", &NumberData);
00351
00352 if (debug)
00353 printf("NumberData : %d\n", NumberData);
00354
00355 if (type == 0) {
00356 data.dist_len1 = NumberData;
00357 } else if (type == 1) {
00358 data.dist_len2 = NumberData;
00359 } else if (type == 2) {
00360 data.rssi_len1 = NumberData;
00361 } else if (type == 3) {
00362 data.rssi_len2 = NumberData;
00363 }
00364
00365 for (int i = 0; i < NumberData; i++) {
00366 int dat;
00367 tok = strtok(NULL, " ");
00368 sscanf(tok, "%X", &dat);
00369
00370 if (type == 0) {
00371 data.dist1[i] = dat;
00372 } else if (type == 1) {
00373 data.dist2[i] = dat;
00374 } else if (type == 2) {
00375 data.rssi1[i] = dat;
00376 } else if (type == 3) {
00377 data.rssi2[i] = dat;
00378 }
00379
00380 }
00381 }
00382
00383 tok = strtok(NULL, " ");
00384 int NumberChannels8Bit;
00385 sscanf(tok, "%d", &NumberChannels8Bit);
00386 if (debug)
00387 printf("NumberChannels8Bit : %d\n", NumberChannels8Bit);
00388 for (int i = 0; i < NumberChannels8Bit; i++) {
00389 int type = -1;
00390 char content[6];
00391 tok = strtok(NULL, " ");
00392 sscanf(tok, "%s", content);
00393 if (!strcmp(content, "DIST1")) {
00394 type = 0;
00395 } else if (!strcmp(content, "DIST2")) {
00396 type = 1;
00397 } else if (!strcmp(content, "RSSI1")) {
00398 type = 2;
00399 } else if (!strcmp(content, "RSSI2")) {
00400 type = 3;
00401 }
00402 tok = strtok(NULL, " ");
00403 tok = strtok(NULL, " ");
00404 tok = strtok(NULL, " ");
00405 tok = strtok(NULL, " ");
00406 tok = strtok(NULL, " ");
00407 int NumberData;
00408 sscanf(tok, "%X", &NumberData);
00409
00410 if (debug)
00411 printf("NumberData : %d\n", NumberData);
00412
00413 if (type == 0) {
00414 data.dist_len1 = NumberData;
00415 } else if (type == 1) {
00416 data.dist_len2 = NumberData;
00417 } else if (type == 2) {
00418 data.rssi_len1 = NumberData;
00419 } else if (type == 3) {
00420 data.rssi_len2 = NumberData;
00421 }
00422 for (int i = 0; i < NumberData; i++) {
00423 int dat;
00424 tok = strtok(NULL, " ");
00425 sscanf(tok, "%X", &dat);
00426
00427 if (type == 0) {
00428 data.dist1[i] = dat;
00429 } else if (type == 1) {
00430 data.dist2[i] = dat;
00431 } else if (type == 2) {
00432 data.rssi1[i] = dat;
00433 } else if (type == 3) {
00434 data.rssi2[i] = dat;
00435 }
00436 }
00437 }
00438 return true;
00439 }
00440
00441 void LMS1xx::saveConfig() {
00442 char buf[100];
00443 sprintf(buf, "%c%s%c", 0x02, "sMN mEEwriteall", 0x03);
00444
00445 write(sockDesc, buf, strlen(buf));
00446
00447 int len = read(sockDesc, buf, 100);
00448
00449
00450
00451
00452
00453
00454 }
00455
00456 void LMS1xx::startDevice() {
00457 char buf[100];
00458 sprintf(buf, "%c%s%c", 0x02, "sMN Run", 0x03);
00459
00460 write(sockDesc, buf, strlen(buf));
00461
00462 int len = read(sockDesc, buf, 100);
00463
00464
00465
00466
00467
00468
00469 }