AUBO SDK  0.26.0
serial.h
Go to the documentation of this file.
1/** @file serial.h
2 * @brief 串口通信
3 */
4#ifndef AUBO_SDK_SERIAL_INTERFACE_H
5#define AUBO_SDK_SERIAL_INTERFACE_H
6
7#include <vector>
8#include <memory>
9
10#include <aubo/type_def.h>
11#include <aubo/global_config.h>
12
13namespace arcs {
14namespace common_interface {
15
16/**
17 * @defgroup Serial Serial (串口通信)
18 * \~chinese 串口通信
19 * \~english Serial control
20 */
21class ARCS_ABI_EXPORT Serial
22{
23public:
25 virtual ~Serial();
26
27 /**
28 * @ingroup Serial
29 * \chinese
30 * 打开TCP/IP以太网通信串口
31 *
32 * @param device 设备名
33 * @param baud 波特率
34 * @param stop_bits 停止位
35 * @param even 校验位
36 * @param serial_name 串口名称
37 * @return 返回值
38 *
39 * @par Python函数原型
40 * serialOpen(self: pyaubo_sdk.Serial, arg0: str, arg1: int, arg2: float,
41 * arg3: int, arg4: str) -> int
42 *
43 * @par Lua函数原型
44 * serialOpen(device: string, baud: number, stop_bits: number, even: number,
45 * serial_name: string) -> nil
46 * \endchinese
47 * \english
48 * Open TCP/IP ethernet communication serial
49 *
50 * @param device
51 * @param baud
52 * @param stop_bits
53 * @param even
54 * @param serial_name
55 * @return
56 *
57 * @par Python function prototype
58 * serialOpen(self: pyaubo_sdk.Serial, arg0: str, arg1: int, arg2: float,
59 * arg3: int, arg4: str) -> int
60 *
61 * @par Lua function prototype
62 * serialOpen(device: string, baud: number, stop_bits: number, even: number,
63 * serial_name: string) -> nil
64 * \endenglish
65 */
66
67 int serialOpen(const std::string &device, int baud, float stop_bits,
68 int even, const std::string &serial_name = "serial_0");
69
70 /**
71 * @ingroup Serial
72 * \chinese
73 * 关闭TCP/IP串口通信
74 * 关闭与服务器的串口连接。
75 *
76 * @param serial_name 串口名称
77 * @return 返回值
78 *
79 * @par Python函数原型
80 * serialClose(self: pyaubo_sdk.Serial, arg0: str) -> int
81 *
82 * @par Lua函数原型
83 * serialClose(serial_name: string) -> nil
84 * \endchinese
85 * \english
86 * Close TCP/IP serial communication
87 * Close down the serial connection to the server.
88 *
89 * @param serial_name
90 * @return
91 *
92 * @par Python function prototype
93 * serialClose(self: pyaubo_sdk.Serial, arg0: str) -> int
94 *
95 * @par Lua function prototype
96 * serialClose(serial_name: string) -> nil
97 * \endenglish
98 */
99 int serialClose(const std::string &serial_name = "serial_0");
100
101 /**
102 * @ingroup Serial
103 * \chinese
104 * 从串口读取指定数量的字节。字节为网络字节序。一次最多可读取30个值。
105 *
106 * @param variable 变量
107 * @param serial_name 串口名称
108 * @return 返回值
109 *
110 * @par Python函数原型
111 * serialReadByte(self: pyaubo_sdk.Serial, arg0: str, arg1: str) -> int
112 *
113 * @par Lua函数原型
114 * serialReadByte(variable: string, serial_name: string) -> number
115 * \endchinese
116 * \english
117 * Reads a number of bytes from the serial. Bytes are in network byte
118 * order. A maximum of 30 values can be read in one command.
119 *
120 * @param variable
121 * @param serial_name
122 * @return
123 *
124 * @par Python function prototype
125 * serialReadByte(self: pyaubo_sdk.Serial, arg0: str, arg1: str) -> int
126 *
127 * @par Lua function prototype
128 * serialReadByte(variable: string, serial_name: string) -> number
129 * \endenglish
130 */
131 int serialReadByte(const std::string &variable,
132 const std::string &serial_name = "serial_0");
133
134 /**
135 * @ingroup Serial
136 * \chinese
137 * 从串口读取指定数量的字节。字节为网络字节序。一次最多可读取30个值。
138 * 返回读取到的数字列表(int列表,长度=number+1)。
139 *
140 * @param number 读取的字节数
141 * @param variable 变量
142 * @param serial_name 串口名称
143 * @return 返回值
144 *
145 * @par Python函数原型
146 * serialReadByteList(self: pyaubo_sdk.Serial, arg0: int, arg1: str, arg2: str) -> int
147 *
148 * @par Lua函数原型
149 * serialReadByteList(number: number, variable: string, serial_name: string) -> number
150 * \endchinese
151 * \english
152 * Reads a number of bytes from the serial. Bytes are in network byte
153 * order. A maximum of 30 values can be read in one command.
154 * A list of numbers read (list of ints, length=number+1)
155 *
156 * @param number Number of bytes to read
157 * @param variable
158 * @param serial_name Serial port name
159 * @return Return value
160 *
161 * @par Python function prototype
162 * serialReadByteList(self: pyaubo_sdk.Serial, arg0: int, arg1: str, arg2: str) -> int
163 *
164 * @par Lua function prototype
165 * serialReadByteList(number: number, variable: string, serial_name: string) -> number
166 * \endenglish
167 */
168 int serialReadByteList(int number, const std::string &variable,
169 const std::string &serial_name = "serial_0");
170
171 /**
172 * @ingroup Serial
173 * \chinese
174 * 从串口读取所有数据,并将数据作为字符串返回。
175 * 字节为网络字节序。
176 *
177 * 可选参数 "prefix" 和 "suffix" 用于指定从串口提取的内容。
178 * "prefix" 指定提取子串(消息)的起始位置。直到 "prefix" 结束的数据会被忽略并从串口移除。
179 * "suffix" 指定提取子串(消息)的结束位置。串口中 "suffix" 之后的剩余数据会被保留。
180 * 例如,如果串口服务器发送字符串 "noise>hello<",控制器可以通过设置 prefix=">" 和 suffix="<" 来接收 "hello"。
181 * 通过使用 "prefix" 和 "suffix",还可以一次向控制器发送多条字符串,因为 "suffix" 定义了消息的结束位置。
182 * 例如发送 ">hello<>world<"
183 *
184 * @param variable 变量
185 * @param serial_name 串口名称
186 * @param prefix 前缀
187 * @param suffix 后缀
188 * @param interpret_escape 是否解释转义字符
189 * @return 返回值
190 *
191 * @par Python函数原型
192 * serialReadString(self: pyaubo_sdk.Serial, arg0: str, arg1: str, arg2: str, arg3: str, arg4: bool) -> int
193 *
194 * @par Lua函数原型
195 * serialReadString(variable: string, serial_name: string, prefix: string, suffix: string, interpret_escape: boolean) -> number
196 * \endchinese
197 * \english
198 * Reads all data from the serial and returns the data as a string.
199 * Bytes are in network byte order.
200 *
201 * The optional parameters "prefix" and "suffix", can be used to express
202 * what is extracted from the serial. The "prefix" specifies the start
203 * of the substring (message) extracted from the serial. The data up to
204 * the end of the "prefix" will be ignored and removed from the serial.
205 * The "suffix" specifies the end of the substring (message) extracted
206 * from the serial. Any remaining data on the serial, after the "suffix",
207 * will be preserved. E.g. if the serial server sends a string
208 * "noise>hello<", the controller can receive the "hello" by calling this
209 * script function with the prefix=">" and suffix="<". By using the
210 * "prefix" and "suffix" it is also possible send multiple string to the
211 * controller at once, because the suffix defines where the message ends.
212 * E.g. sending ">hello<>world<"
213 *
214 * @param variable
215 * @param serial_name
216 * @param prefix
217 * @param suffix
218 * @param interpret_escape
219 * @return
220 *
221 * @par Python function prototype
222 * serialReadString(self: pyaubo_sdk.Serial, arg0: str, arg1: str, arg2: str, arg3: str, arg4: bool) -> int
223 *
224 * @par Lua function prototype
225 * serialReadString(variable: string, serial_name: string, prefix: string, suffix: string, interpret_escape: boolean) -> number
226 * \endenglish
227 */
228 int serialReadString(const std::string &variable,
229 const std::string &serial_name = "serial_0",
230 const std::string &prefix = "",
231 const std::string &suffix = "",
232 bool interpret_escape = false);
233
234 /**
235 * @ingroup Serial
236 * \chinese
237 * 发送一个字节到服务器
238 * 通过串口发送字节 <value>。不期望有响应。可用于发送特殊的ASCII字符;10为换行符,2为文本开始,3为文本结束。
239 *
240 * @param value 字节值
241 * @param serial_name 串口名称
242 * @return 返回值
243 *
244 * @par Python函数原型
245 * serialSendByte(self: pyaubo_sdk.Serial, arg0: str, arg1: str) -> int
246 *
247 * @par Lua函数原型
248 * serialSendByte(value: string, serial_name: string) -> nil
249 * \endchinese
250 * \english
251 * Sends a byte to the server
252 * Sends the byte <value> through the serial. Expects no response. Can
253 * be used to send special ASCII characters; 10 is newline, 2 is start of
254 * text, 3 is end of text.
255 *
256 * @param value
257 * @param serial_name
258 * @return
259 *
260 * @par Python function prototype
261 * serialSendByte(self: pyaubo_sdk.Serial, arg0: str, arg1: str) -> int
262 *
263 * @par Lua function prototype
264 * serialSendByte(value: string, serial_name: string) -> nil
265 * \endenglish
266 */
267 int serialSendByte(char value, const std::string &serial_name = "serial_0");
268
269 /**
270 * @ingroup Serial
271 * \chinese
272 * 发送一个整数(int32_t)到服务器
273 * 通过串口发送整数 <value>。以网络字节序发送。不期望有响应。
274 *
275 * @param value 整数值
276 * @param serial_name 串口名称
277 * @return 返回值
278 *
279 * @par Python函数原型
280 * serialSendInt(self: pyaubo_sdk.Serial, arg0: int, arg1: str) -> int
281 *
282 * @par Lua函数原型
283 * serialSendInt(value: number, serial_name: string) -> nil
284 * \endchinese
285 * \english
286 * Sends an int (int32_t) to the server
287 * Sends the int <value> through the serial. Send in network byte order.
288 * Expects no response.
289 *
290 * @param value
291 * @param serial_name
292 * @return
293 *
294 * @par Python function prototype
295 * serialSendInt(self: pyaubo_sdk.Serial, arg0: int, arg1: str) -> int
296 *
297 * @par Lua function prototype
298 * serialSendInt(value: number, serial_name: string) -> nil
299 * \endenglish
300 */
301 int serialSendInt(int value, const std::string &serial_name = "serial_0");
302
303 /**
304 * @ingroup Serial
305 * \chinese
306 * 发送带有换行符的字符串到服务器
307 * 以ASCII编码通过串口发送字符串<str>,并在末尾添加换行符。不期望有响应。
308 *
309 * @param str 字符串
310 * @param serial_name 串口名称
311 * @return 返回值
312 *
313 * @par Python函数原型
314 * serialSendLine(self: pyaubo_sdk.Serial, arg0: str, arg1: str) -> int
315 *
316 * @par Lua函数原型
317 * serialSendLine(str: string, serial_name: string) -> nil
318 * \endchinese
319 * \english
320 * Sends a string with a newline character to the server
321 * Sends the string <str> through the serial in ASCII coding, appending a newline at the end. Expects no response.
322 *
323 * @param str
324 * @param serial_name
325 * @return
326 *
327 * @par Python function prototype
328 * serialSendLine(self: pyaubo_sdk.Serial, arg0: str, arg1: str) -> int
329 *
330 * @par Lua function prototype
331 * serialSendLine(str: string, serial_name: string) -> nil
332 * \endenglish
333 */
334 int serialSendLine(const std::string &str,
335 const std::string &serial_name = "serial_0");
336
337 /**
338 * @ingroup Serial
339 * \chinese
340 * 发送字符串到服务器
341 * 以ASCII编码通过串口发送字符串<str>。不期望有响应。
342 *
343 * @param str 字符串
344 * @param serial_name 串口名称
345 * @return 返回值
346 *
347 * @par Python函数原型
348 * serialSendString(self: pyaubo_sdk.Serial, arg0: str, arg1: str) -> int
349 *
350 * @par Lua函数原型
351 * serialSendString(str: string, serial_name: string) -> nil
352 * \endchinese
353 * \english
354 * Sends a string to the server
355 * Sends the string <str> through the serial in ASCII coding. Expects no
356 * response.
357 *
358 * @param str
359 * @param serial_name
360 * @return
361 *
362 * @par Python function prototype
363 * serialSendString(self: pyaubo_sdk.Serial, arg0: str, arg1: str) -> int
364 *
365 * @par Lua function prototype
366 * serialSendString(str: string, serial_name: string) -> nil
367 * \endenglish
368 */
369 int serialSendString(const std::string &str,
370 const std::string &serial_name = "serial_0");
371
372 /**
373 * @ingroup Serial
374 * \chinese
375 *
376 * @param is_check 是否校验
377 * @param str 字符串数组
378 * @param serial_name 串口名称
379 * @return 返回值
380 *
381 * @par Python函数原型
382 * serialSendAllString(self: pyaubo_sdk.Serial, arg0: bool, arg1: List[str], arg2: str) -> int
383 *
384 * @par Lua函数原型
385 * serialSendAllString(is_check: boolean, str: table, serial_name: string) -> nil
386 * \endchinese
387 * \english
388 *
389 * @param is_check Whether to check
390 * @param str Array of strings
391 * @param serial_name Serial port name
392 * @return Return value
393 *
394 * @par Python function prototype
395 * serialSendAllString(self: pyaubo_sdk.Serial, arg0: bool, arg1: List[str], arg2: str) -> int
396 *
397 * @par Lua function prototype
398 * serialSendAllString(is_check: boolean, str: table, serial_name: string) -> nil
399 * \endenglish
400 */
401 int serialSendAllString(bool is_check, const std::vector<char> &str,
402 const std::string &serial_name = "serial_0");
403
404protected:
405 void *d_;
406};
407using SerialPtr = std::shared_ptr<Serial>;
408
409} // namespace common_interface
410} // namespace arcs
411#endif
int serialSendAllString(bool is_check, const std::vector< char > &str, const std::string &serial_name="serial_0")
int serialSendLine(const std::string &str, const std::string &serial_name="serial_0")
Sends a string with a newline character to the server Sends the string <str> through the serial in AS...
int serialSendString(const std::string &str, const std::string &serial_name="serial_0")
Sends a string to the server Sends the string <str> through the serial in ASCII coding.
int serialClose(const std::string &serial_name="serial_0")
Close TCP/IP serial communication Close down the serial connection to the server.
int serialReadByteList(int number, const std::string &variable, const std::string &serial_name="serial_0")
Reads a number of bytes from the serial.
int serialSendInt(int value, const std::string &serial_name="serial_0")
Sends an int (int32_t) to the server Sends the int through the serial.
int serialReadByte(const std::string &variable, const std::string &serial_name="serial_0")
Reads a number of bytes from the serial.
int serialSendByte(char value, const std::string &serial_name="serial_0")
Sends a byte to the server Sends the byte through the serial.
int serialOpen(const std::string &device, int baud, float stop_bits, int even, const std::string &serial_name="serial_0")
Open TCP/IP ethernet communication serial
int serialReadString(const std::string &variable, const std::string &serial_name="serial_0", const std::string &prefix="", const std::string &suffix="", bool interpret_escape=false)
Reads all data from the serial and returns the data as a string.
std::shared_ptr< Serial > SerialPtr
Definition serial.h:407
enum type definitions