ypprotocol.c
Go to the documentation of this file.
1 // Copyright (c) 2010-2016 The YP-Spur Authors, except where otherwise indicated.
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to
5 // deal in the Software without restriction, including without limitation the
6 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 // sell copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 // SOFTWARE.
20 
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/time.h>
25 #include <sys/types.h>
26 #include <unistd.h>
27 
28 #include <errno.h>
29 
30 #ifdef HAVE_CONFIG_H
31 #include <config.h>
32 #endif // HAVE_CONFIG_H
33 
34 #include <communication.h>
35 #include <ypprotocol.h>
36 #include <serial.h>
37 #include <utility.h>
38 #include <yprintf.h>
39 
40 #define RECEIVE_BUFFER_SIZE 2048
41 
42 int ss_receive(char *buf, int len, double receive_time, void *data)
43 {
44  buf[len] = 0;
45  if (len + strlen((char *)data) > RECEIVE_BUFFER_SIZE)
46  {
47  return -3;
48  }
49  strcat((char *)data, buf);
50  if (strstr((char *)data, "\n\n"))
51  {
52  return -2;
53  }
54  return 0;
55 }
56 
57 int set_baudrate(int baud)
58 {
59  /* Send & Recive Buffer */
60  char buf[RECEIVE_BUFFER_SIZE];
61  /* Temporary */
62 
63  strcpy(buf, "\n\n\n\n");
64  serial_write(buf, strlen(buf));
65  yp_usleep(50000);
67  yp_usleep(50000);
68  sprintf(buf, "SS%06d\n", baud);
69  serial_write(buf, strlen(buf));
70 
71  buf[0] = 0;
72  if (serial_recieve(ss_receive, buf) != -2)
73  return 0;
74 
75  if (strstr(buf, "\n00P\n") != NULL)
76  {
77  yp_usleep(100000);
78  return serial_change_baudrate(baud);
79  }
80  // RS-232cインターフェース未搭載
81  else if (strstr(buf, "\n04T\n") != NULL)
82  {
83  SER_BAUDRATE = 0;
84  yp_usleep(100000);
85  return 4;
86  }
87 
88  return 0;
89 }
90 
91 int vv_receive(char *buf, int len, double receive_time, void *data)
92 {
93  buf[len] = 0;
94  if (len + strlen((char *)data) > RECEIVE_BUFFER_SIZE)
95  {
96  return -3;
97  }
98  strcat((char *)data, buf);
99  if (strstr((char *)data, "\n\n"))
100  {
101  return -2;
102  }
103  return 0;
104 }
105 
111 int get_version(Ver_t *apVer)
112 {
113  /* Send & Recive Buffer */
114  char buf[RECEIVE_BUFFER_SIZE], *readpos;
115  /* Temporary */
116  char *tmp, *lf, *val;
117  char *tag, *wbuf;
118 
119  readpos = buf;
120  memset(apVer, 0, sizeof(Ver_t));
121 
122  strcpy(buf, "\n\n\n\n");
123  serial_write(buf, strlen(buf));
124  yp_usleep(50000);
125  serial_flush_in();
126  yp_usleep(50000);
127  strcpy(buf, "VV\n");
128  serial_write(buf, strlen(buf));
129 
130  buf[0] = 0;
131  if (serial_recieve(vv_receive, buf) != -2)
132  return 0;
133  if (strstr(buf, "\n00P\n") == 0)
134  return 0;
135 
136  while (1)
137  {
138  if ((lf = strchr(readpos, '\n')) == NULL)
139  break;
140  *lf = 0;
141  if ((tag = strstr(readpos, "VEND:")) != 0)
142  {
143  wbuf = (apVer->vender);
144  }
145  else if ((tag = strstr(readpos, "PROD:")) != 0)
146  {
147  wbuf = (apVer->product);
148  }
149  else if ((tag = strstr(readpos, "FIRM:")) != 0)
150  {
151  wbuf = (apVer->firmware);
152  }
153  else if ((tag = strstr(readpos, "PROT:")) != 0)
154  {
155  wbuf = (apVer->protocol);
156  }
157  else if ((tag = strstr(readpos, "SERI:")) != 0)
158  {
159  wbuf = (apVer->serialno);
160  }
161  else
162  {
163  readpos = lf + 1;
164  continue;
165  }
166  tmp = strchr(tag, ';');
167  val = strchr(tag, ':');
168  if (tmp && val)
169  {
170  *tmp = 0;
171  if (wbuf)
172  {
173  strcpy(wbuf, val + 1);
174  }
175  }
176  readpos = lf + 1;
177  }
178  return 1;
179 }
180 
186 int get_parameter(Param_t *apParam)
187 {
188  /* Send & Recive Buffer */
189  char buf[RECEIVE_BUFFER_SIZE], *readpos;
190  /* Temporary */
191  char *tmp, *lf, *val;
192  char *tag, *wbuf;
193 
194  readpos = buf;
195  memset(apParam, 0, sizeof(Param_t));
196 
197  strcpy(buf, "\n\n\n\n");
198  serial_write(buf, strlen(buf));
199  yp_usleep(50000);
200  serial_flush_in();
201  yp_usleep(50000);
202  strcpy(buf, "PP\n");
203  serial_write(buf, strlen(buf));
204 
205  buf[0] = 0;
206  if (serial_recieve(vv_receive, buf) != -2)
207  return 0;
208  if (strstr(buf, "\n00P\n") == 0)
209  return 0;
210 
211  while (1)
212  {
213  if ((lf = strchr(readpos, '\n')) == NULL)
214  break;
215  *lf = 0;
216  if ((tag = strstr(readpos, "PWMRES:")) != 0)
217  {
218  wbuf = (apParam->pwm_resolution);
219  }
220  else if ((tag = strstr(readpos, "MOTORNUM:")) != 0)
221  {
222  wbuf = (apParam->motor_num);
223  }
224  else if ((tag = strstr(readpos, "NAME:")) != 0)
225  {
226  wbuf = (apParam->robot_name);
227  }
228  else
229  {
230  readpos = lf + 1;
231  continue;
232  }
233  tmp = strchr(tag, ';');
234  val = strchr(tag, ':');
235  if (tmp && val)
236  {
237  *tmp = 0;
238  if (wbuf)
239  {
240  strcpy(wbuf, val + 1);
241  }
242  }
243  readpos = lf + 1;
244  }
245  return 1;
246 }
247 
248 int get_embedded_param(char *param)
249 {
250  /* Send & Recive Buffer */
251  char buf[RECEIVE_BUFFER_SIZE], *readpos;
252  /* Temporary */
253  char *lf;
254 
255  readpos = buf;
256  memset(buf, 0, sizeof(buf));
257 
258  strcpy(buf, "\n\n\n\n");
259  serial_write(buf, strlen(buf));
260  yp_usleep(50000);
261  serial_flush_in();
262  yp_usleep(50000);
263  strcpy(buf, "GETEMBEDDEDPARAM\n");
264  serial_write(buf, strlen(buf));
265 
266  buf[0] = 0;
267  if (serial_recieve(vv_receive, buf) != -2)
268  return 0;
269  if (strstr(buf, "\n00P\n") == 0)
270  return 0;
271 
272  while (1)
273  {
274  if ((lf = strchr(readpos, '\n')) == NULL)
275  break;
276  *lf = 0;
277  if (strstr(readpos, "00P") != 0)
278  {
279  lf++;
280  break;
281  }
282  readpos = lf + 1;
283  }
284  if (lf == NULL)
285  return 0;
286  strcpy(param, lf);
287  return 1;
288 }
#define RECEIVE_BUFFER_SIZE
Definition: ypprotocol.c:40
int serial_write(char *buf, int len)
Definition: serial.c:539
char serialno[128]
Definition: ypprotocol.h:38
double SER_BAUDRATE
Definition: serial.c:48
void yp_usleep(int usec)
Definition: utility.c:54
char product[128]
Definition: ypprotocol.h:35
char pwm_resolution[128]
Definition: ypprotocol.h:44
char protocol[128]
Definition: ypprotocol.h:37
int serial_change_baudrate(int baud)
ボーレートを変更する
Definition: serial.c:216
char vender[128]
Definition: ypprotocol.h:34
int get_embedded_param(char *param)
Definition: ypprotocol.c:248
char motor_num[128]
Definition: ypprotocol.h:45
int vv_receive(char *buf, int len, double receive_time, void *data)
Definition: ypprotocol.c:91
int ss_receive(char *buf, int len, double receive_time, void *data)
Definition: ypprotocol.c:42
int serial_recieve(int(*serial_event)(char *, int, double, void *), void *data)
Definition: serial.c:431
int get_version(Ver_t *apVer)
Get version info.
Definition: ypprotocol.c:111
char firmware[128]
Definition: ypprotocol.h:36
char robot_name[128]
Definition: ypprotocol.h:46
void serial_flush_in(void)
Definition: serial.c:385
int get_parameter(Param_t *apParam)
Get version info.
Definition: ypprotocol.c:186
int set_baudrate(int baud)
Definition: ypprotocol.c:57


yp-spur
Author(s):
autogenerated on Sat May 11 2019 02:08:24