3 \brief Process SKIP commands
5 \author Satofumi KAMIMURA
7 $Id: scip_handler.c 1714 2010-02-21 20:53:28Z satofumi $
9 \todo Check the checksum of acquired line
10 \todo Add an argument to distinguish the line that contain version information.
13 #include "scip_handler.h"
14 #include "serial_errno.h"
15 #include "serial_ctrl.h"
16 #include "serial_utils.h"
17 #include "urg_errno.h"
23 #if defined(WINDOWS_OS)
24 #define snprintf _snprintf
27 extern int snprintf(char *, size_t, const char *, ...);
30 /*! \todo Standardize with urg_ctrl.c */
32 ScipTimeout = 1000, /*!< [msec] */
33 EachTimeout = 100, /*!< [msec] */
38 int scip_send(serial_t *serial, const char *send_command)
40 int n = (int)strlen(send_command);
41 return serial_send(serial, send_command, n);
46 \brief Receive the response from command
48 \todo Test the checksum
50 int scip_recv(serial_t *serial, const char *command_first,
51 int* return_code, int expected_ret[], int timeout)
58 /* Receive the response */
59 char buffer[ScipLineWidth];
61 /* Skip the first response */
62 n = serial_getLine(serial, buffer, ScipLineWidth, timeout);
64 return UrgSerialRecvFail;
67 /* ignore 0x00 response after connection */
68 if (! ((n == 1) && (buffer[0] == 0x00))) {
69 if (strncmp(buffer, command_first, 2)) {
70 /* Treat as an error,if there is mismatch with sent characters */
71 return UrgMismatchResponse;
75 /* Read and pass the response characters. */
76 n = serial_getLine(serial, buffer, ScipLineWidth, timeout);
78 /* restore last character, and use next proccessing */
79 n = serial_recv(serial, &recv_ch, 1, timeout);
80 if ((n == 1) && (! serial_isLF(recv_ch))) {
81 serial_ungetc(serial, recv_ch);
84 /* Returns 0, if received response characters are as expected */
85 ret_code = strtol(buffer, NULL, 16);
86 if (return_code != NULL) {
87 *return_code = ret_code;
89 for (i = 0; expected_ret[i] != -1; ++i) {
90 if (ret_code == expected_ret[i]) {
98 /* Transition to SCIP 2.0 */
99 int scip_scip20(serial_t *serial)
101 int expected_ret[] = { 0x0, 0xE, -1 };
104 ret = scip_send(serial, "SCIP2.0\n");
109 return scip_recv(serial, "SC", NULL, expected_ret, ScipTimeout);
113 /* Send QT command */
114 int scip_qt(serial_t *serial, int *return_code, int wait_reply)
116 int expected_ret[] = { 0x0, -1 };
119 ret = scip_send(serial, "QT\n");
124 if (wait_reply == ScipNoWaitReply) {
128 ret = scip_recv(serial, "QT", return_code, expected_ret, ScipTimeout);
129 if (return_code && (*return_code == 0xE)) {
130 *return_code = -(*return_code);
138 /* Get PP information */
139 int scip_pp(serial_t *serial, urg_parameter_t *parameters)
143 int expected_reply[] = { 0x0, -1 };
147 char buffer[ScipLineWidth];
149 send_n = scip_send(serial, "PP\n");
151 return SerialSendFail;
154 /* Receive the response */
155 ret = scip_recv(serial, "PP", NULL, expected_reply, ScipTimeout);
160 /* Reception of parameter characters */
161 for (i = 0; i < UrgParameterLines; ++i) {
162 n = serial_getLine(serial, buffer, ScipLineWidth, ScipTimeout);
167 /* !!! It is necessary to check the character string like AMIN */
170 strncpy(parameters->sensor_type, &buffer[5], 8);
171 parameters->sensor_type[8] = '\0';
174 parameters->distance_min_ = atoi(&buffer[5]);
177 parameters->distance_max_ = atoi(&buffer[5]);
180 parameters->area_total_ = atoi(&buffer[5]);
183 parameters->area_min_ = atoi(&buffer[5]);
186 parameters->area_max_ = atoi(&buffer[5]);
189 parameters->area_front_ = atoi(&buffer[5]);
192 parameters->scan_rpm_ = atoi(&buffer[5]);
200 /* Reception of VV response*/
201 int scip_vv(serial_t *serial, char *lines[], int lines_max)
205 int expected_reply[] = { 0x0, -1 };
209 /* Initialize by an empty message */
210 for (i = 0; i < lines_max; ++i) {
214 /* Send VV command */
215 send_n = scip_send(serial, "VV\n");
217 return SerialSendFail;
220 /* Receive response */
221 ret = scip_recv(serial, "VV", NULL, expected_reply, ScipTimeout);
226 /* Receive version information */
227 for (i = 0; i < lines_max; ++i) {
228 n = serial_getLine(serial, lines[i], ScipLineWidth, ScipTimeout);
234 serial_skip(serial, ScipTimeout, EachTimeout);
239 /* Change baud rate according to SS command */
240 int scip_ss(serial_t *serial, long baudrate)
242 int expected_reply[] = { 0x0, 0x3, 0x4, -1 };
246 /* !!! Should be treated as an error if baud rate is not with in range of
249 /* Send SS command */
250 char buffer[] = "SSxxxxxx\n";
251 snprintf(buffer, 10, "SS%06ld\n", baudrate);
252 send_n = scip_send(serial, buffer);
254 return SerialSendFail;
257 /* Receive response */
258 ret = scip_recv(serial, "SS", NULL, expected_reply, ScipTimeout);