1 | // %flair:license{
|
---|
2 | // This file is part of the Flair framework distributed under the
|
---|
3 | // CECILL-C License, Version 1.0.
|
---|
4 | // %flair:license}
|
---|
5 | // created: 2014/04/25
|
---|
6 | // filename: RTDM_SerialPort.cpp
|
---|
7 | //
|
---|
8 | // author: Guillaume Sanahuja
|
---|
9 | // Copyright Heudiasyc UMR UTC/CNRS 7253
|
---|
10 | //
|
---|
11 | // version: $Id: $
|
---|
12 | //
|
---|
13 | // purpose: port serie temps reel
|
---|
14 | //
|
---|
15 | //
|
---|
16 | /*********************************************************************/
|
---|
17 |
|
---|
18 | #include "RTDM_SerialPort.h"
|
---|
19 |
|
---|
20 | #ifdef __XENO__
|
---|
21 |
|
---|
22 | #include <rtdm/rtserial.h>
|
---|
23 | #include <errno.h>
|
---|
24 | #include <cstring>
|
---|
25 |
|
---|
26 | using std::string;
|
---|
27 |
|
---|
28 | namespace flair {
|
---|
29 | namespace core {
|
---|
30 |
|
---|
31 | RTDM_SerialPort::RTDM_SerialPort(const Object *parent, string name,
|
---|
32 | string device)
|
---|
33 | : SerialPort(parent, name) {
|
---|
34 | struct rtser_config write_config;
|
---|
35 |
|
---|
36 | write_config.config_mask = RTSER_SET_BAUD | RTSER_SET_TIMESTAMP_HISTORY,
|
---|
37 | write_config.baud_rate = 115200,
|
---|
38 | write_config.timestamp_history = RTSER_DEF_TIMESTAMP_HISTORY,
|
---|
39 | // the rest implicitely remains default
|
---|
40 |
|
---|
41 | fd = rt_dev_open(device.c_str(), 0);
|
---|
42 | if (fd < 0) {
|
---|
43 | Err("erreur rt_dev_open (%s)\n", strerror(-fd));
|
---|
44 | }
|
---|
45 |
|
---|
46 | // config
|
---|
47 | int err = rt_dev_ioctl(fd, RTSER_RTIOC_SET_CONFIG, &write_config);
|
---|
48 | if (err) {
|
---|
49 | Err("erreur rt_dev_ioctl RTSER_RTIOC_SET_CONFIG (%s)\n", strerror(-err));
|
---|
50 | }
|
---|
51 | }
|
---|
52 |
|
---|
53 | RTDM_SerialPort::~RTDM_SerialPort() { rt_dev_close(fd); }
|
---|
54 |
|
---|
55 | void RTDM_SerialPort::SetBaudrate(int baudrate) {
|
---|
56 | struct rtser_config write_config;
|
---|
57 |
|
---|
58 | write_config.config_mask = RTSER_SET_BAUD;
|
---|
59 | write_config.baud_rate = baudrate;
|
---|
60 | // the rest implicitely remains default
|
---|
61 |
|
---|
62 | // config
|
---|
63 | int err = rt_dev_ioctl(fd, RTSER_RTIOC_SET_CONFIG, &write_config);
|
---|
64 | if (err) {
|
---|
65 | Err("erreur rt_dev_ioctl RTSER_RTIOC_SET_CONFIG (%s)\n", strerror(-err));
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | void RTDM_SerialPort::SetRxTimeout(core::Time timeout_ns) {
|
---|
70 | struct rtser_config write_config;
|
---|
71 |
|
---|
72 | write_config.config_mask = RTSER_SET_TIMEOUT_RX;
|
---|
73 | write_config.rx_timeout = timeout_ns;
|
---|
74 | // the rest implicitely remains default
|
---|
75 |
|
---|
76 | // config
|
---|
77 | int err = rt_dev_ioctl(fd, RTSER_RTIOC_SET_CONFIG, &write_config);
|
---|
78 | if (err) {
|
---|
79 | Err("erreur rt_dev_ioctl RTSER_RTIOC_SET_CONFIG (%s)\n", strerror(-err));
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | void RTDM_SerialPort::FlushInput(void) {
|
---|
84 | char tmp;
|
---|
85 |
|
---|
86 | SetRxTimeout(1000000);
|
---|
87 | while (rt_dev_read(fd, &tmp, 1) == 1)
|
---|
88 | ;
|
---|
89 | SetRxTimeout(TIME_INFINITE);
|
---|
90 | }
|
---|
91 |
|
---|
92 | ssize_t RTDM_SerialPort::Write(const void *buf, size_t nbyte) {
|
---|
93 | return rt_dev_write(fd, buf, nbyte);
|
---|
94 | }
|
---|
95 |
|
---|
96 | ssize_t RTDM_SerialPort::Read(void *buf, size_t nbyte) {
|
---|
97 | return rt_dev_read(fd, buf, nbyte);
|
---|
98 | }
|
---|
99 |
|
---|
100 | } // end namespace core
|
---|
101 | } // end namespace flair
|
---|
102 |
|
---|
103 | #else //__XENO__
|
---|
104 |
|
---|
105 | using std::string;
|
---|
106 | using namespace flair::core;
|
---|
107 |
|
---|
108 | namespace flair {
|
---|
109 | namespace core {
|
---|
110 |
|
---|
111 | RTDM_SerialPort::RTDM_SerialPort(const Object *parent, string name,
|
---|
112 | string device)
|
---|
113 | : SerialPort(parent, name) {
|
---|
114 | Err("not available in non real time\n");
|
---|
115 | }
|
---|
116 |
|
---|
117 | RTDM_SerialPort::~RTDM_SerialPort() {}
|
---|
118 |
|
---|
119 | ssize_t RTDM_SerialPort::Write(const void *buf, size_t nbyte) { return -1; }
|
---|
120 |
|
---|
121 | ssize_t RTDM_SerialPort::Read(void *buf, size_t nbyte) { return -1; }
|
---|
122 |
|
---|
123 | void RTDM_SerialPort::SetBaudrate(int baudrate) {}
|
---|
124 |
|
---|
125 | void RTDM_SerialPort::SetRxTimeout(core::Time timeout_ns) {}
|
---|
126 |
|
---|
127 | void RTDM_SerialPort::FlushInput(void) {}
|
---|
128 |
|
---|
129 | } // end namespace core
|
---|
130 | } // end namespace flair
|
---|
131 |
|
---|
132 | #endif
|
---|