00001
00013 #include "scip_handler.h"
00014 #include "serial_errno.h"
00015 #include "serial_ctrl.h"
00016 #include "serial_utils.h"
00017 #include "urg_errno.h"
00018 #include <stdio.h>
00019 #include <stdlib.h>
00020 #include <string.h>
00021 #include <ctype.h>
00022
00023 #if defined(WINDOWS_OS)
00024 #define snprintf _snprintf
00025 #endif
00026
00027
00028
00029
00030
00032 enum {
00033 ScipTimeout = 1000,
00034 EachTimeout = 100,
00035 };
00036
00037
00038
00039 int scip_send(serial_t *serial, const char *send_command)
00040 {
00041 int n = (int)strlen(send_command);
00042 return serial_send(serial, send_command, n);
00043 }
00044
00045
00051 int scip_recv(serial_t *serial, const char *command_first,
00052 int* return_code, int expected_ret[], int timeout)
00053 {
00054 char recv_ch = '\0';
00055 int ret_code = 0;
00056 int n;
00057 int i;
00058
00059
00060 char buffer[ScipLineWidth];
00061
00062
00063 n = serial_getLine(serial, buffer, ScipLineWidth, timeout);
00064 if (n < 0) {
00065 return UrgSerialRecvFail;
00066 }
00067
00068
00069 if (! ((n == 1) && (buffer[0] == 0x00))) {
00070 if (strncmp(buffer, command_first, 2)) {
00071
00072 return UrgMismatchResponse;
00073 }
00074 }
00075
00076
00077 n = serial_getLine(serial, buffer, ScipLineWidth, timeout);
00078
00079
00080 n = serial_recv(serial, &recv_ch, 1, timeout);
00081 if ((n == 1) && (! serial_isLF(recv_ch))) {
00082 serial_ungetc(serial, recv_ch);
00083 }
00084
00085
00086 ret_code = strtol(buffer, NULL, 16);
00087 if (return_code != NULL) {
00088 *return_code = ret_code;
00089 }
00090 for (i = 0; expected_ret[i] != -1; ++i) {
00091 if (ret_code == expected_ret[i]) {
00092 return 0;
00093 }
00094 }
00095 return ret_code;
00096 }
00097
00098
00099
00100 int scip_scip20(serial_t *serial)
00101 {
00102 int expected_ret[] = { 0x0, 0xE, -1 };
00103 int ret;
00104
00105 ret = scip_send(serial, "SCIP2.0\n");
00106 if (ret != 8) {
00107 return ret;
00108 }
00109
00110 return scip_recv(serial, "SC", NULL, expected_ret, ScipTimeout);
00111 }
00112
00113
00114
00115 int scip_qt(serial_t *serial, int *return_code, int wait_reply)
00116 {
00117 int expected_ret[] = { 0x0, -1 };
00118 int ret;
00119
00120 ret = scip_send(serial, "QT\n");
00121 if (ret != 3) {
00122 return ret;
00123 }
00124
00125 if (wait_reply == ScipNoWaitReply) {
00126 return 0;
00127 }
00128
00129 ret = scip_recv(serial, "QT", return_code, expected_ret, ScipTimeout);
00130 if (return_code && (*return_code == 0xE)) {
00131 *return_code = -(*return_code);
00132 return UrgScip10;
00133 }
00134
00135 return ret;
00136 }
00137
00138
00139
00140 int scip_pp(serial_t *serial, urg_parameter_t *parameters)
00141 {
00142 int send_n;
00143 int ret = 0;
00144 int expected_reply[] = { 0x0, -1 };
00145 int n;
00146 int i;
00147
00148 char buffer[ScipLineWidth];
00149
00150 send_n = scip_send(serial, "PP\n");
00151 if (send_n != 3) {
00152 return SerialSendFail;
00153 }
00154
00155
00156 ret = scip_recv(serial, "PP", NULL, expected_reply, ScipTimeout);
00157 if (ret < 0) {
00158 return ret;
00159 }
00160
00161
00162 for (i = 0; i < UrgParameterLines; ++i) {
00163 n = serial_getLine(serial, buffer, ScipLineWidth, ScipTimeout);
00164 if (n <= 0) {
00165 return ret;
00166 }
00167
00168
00169
00170 if (i == 0) {
00171 strncpy(parameters->sensor_type, &buffer[5], 8);
00172 parameters->sensor_type[8] = '\0';
00173
00174 } else if (i == 1) {
00175 parameters->distance_min_ = atoi(&buffer[5]);
00176
00177 } else if (i == 2) {
00178 parameters->distance_max_ = atoi(&buffer[5]);
00179
00180 } else if (i == 3) {
00181 parameters->area_total_ = atoi(&buffer[5]);
00182
00183 } else if (i == 4) {
00184 parameters->area_min_ = atoi(&buffer[5]);
00185
00186 } else if (i == 5) {
00187 parameters->area_max_ = atoi(&buffer[5]);
00188
00189 } else if (i == 6) {
00190 parameters->area_front_ = atoi(&buffer[5]);
00191
00192 } else if (i == 7) {
00193 parameters->scan_rpm_ = atoi(&buffer[5]);
00194 }
00195 }
00196
00197 return 0;
00198 }
00199
00200
00201
00202 int scip_vv(serial_t *serial, char *lines[], int lines_max)
00203 {
00204 int send_n;
00205 int ret = 0;
00206 int expected_reply[] = { 0x0, -1 };
00207 int n;
00208 int i;
00209
00210
00211 for (i = 0; i < lines_max; ++i) {
00212 *lines[i] = '\0';
00213 }
00214
00215
00216 send_n = scip_send(serial, "VV\n");
00217 if (send_n != 3) {
00218 return SerialSendFail;
00219 }
00220
00221
00222 ret = scip_recv(serial, "VV", NULL, expected_reply, ScipTimeout);
00223 if (ret < 0) {
00224 return ret;
00225 }
00226
00227
00228 for (i = 0; i < lines_max; ++i) {
00229 n = serial_getLine(serial, lines[i], ScipLineWidth, ScipTimeout);
00230 if (n <= 0) {
00231 return ret;
00232 }
00233 }
00234
00235 serial_skip(serial, ScipTimeout, EachTimeout);
00236 return ret;
00237 }
00238
00239
00240
00241 int scip_ss(serial_t *serial, long baudrate)
00242 {
00243 int expected_reply[] = { 0x0, 0x3, 0x4, -1 };
00244 int send_n;
00245 int ret;
00246
00247
00248
00249
00250
00251 char buffer[] = "SSxxxxxx\n";
00252 snprintf(buffer, 10, "SS%06ld\n", baudrate);
00253 send_n = scip_send(serial, buffer);
00254 if (send_n != 9) {
00255 return SerialSendFail;
00256 }
00257
00258
00259 ret = scip_recv(serial, "SS", NULL, expected_reply, ScipTimeout);
00260 if (ret < 0) {
00261 return ret;
00262 }
00263
00264 return 0;
00265 }