source: flair-src/branches/sanscv/lib/FlairCore/src/RTDM_SerialPort.cpp

Last change on this file was 324, checked in by Sanahuja Guillaume, 2 years ago

removing opencv dependency

File size: 3.4 KB
Line 
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 <cstring>
24
25using std::string;
26
27namespace flair {
28namespace core {
29
30RTDM_SerialPort::RTDM_SerialPort(const Object *parent, string name,
31 string device)
32 : SerialPort(parent, name) {
33 struct rtser_config write_config;
34
35 write_config.config_mask = RTSER_SET_BAUD | RTSER_SET_TIMESTAMP_HISTORY,
36 write_config.baud_rate = 115200,
37 write_config.timestamp_history = RTSER_DEF_TIMESTAMP_HISTORY,
38 // the rest implicitely remains default
39
40 fd = rt_dev_open(device.c_str(), 0);
41 if (fd < 0) {
42 char errorMsg[256];
43 Err("error rt_dev_open (%s)\n", strerror_r(-fd, errorMsg, sizeof(errorMsg)));
44 }
45
46 // config
47 int err = rt_dev_ioctl(fd, RTSER_RTIOC_SET_CONFIG, &write_config);
48 if (err) {
49 char errorMsg[256];
50 Err("error rt_dev_ioctl RTSER_RTIOC_SET_CONFIG (%s)\n", strerror_r(-err, errorMsg, sizeof(errorMsg)));
51 }
52}
53
54RTDM_SerialPort::~RTDM_SerialPort() { rt_dev_close(fd); }
55
56void RTDM_SerialPort::SetBaudrate(int baudrate) {
57 struct rtser_config write_config;
58
59 write_config.config_mask = RTSER_SET_BAUD;
60 write_config.baud_rate = baudrate;
61 // the rest implicitely remains default
62
63 // config
64 int err = rt_dev_ioctl(fd, RTSER_RTIOC_SET_CONFIG, &write_config);
65 if (err) {
66 char errorMsg[256];
67 Err("error rt_dev_ioctl RTSER_RTIOC_SET_CONFIG (%s)\n", strerror_r(-err, errorMsg, sizeof(errorMsg)));
68 }
69}
70
71void RTDM_SerialPort::SetRxTimeout(core::Time timeout_ns) {
72 struct rtser_config write_config;
73
74 write_config.config_mask = RTSER_SET_TIMEOUT_RX;
75 write_config.rx_timeout = timeout_ns;
76 // the rest implicitely remains default
77
78 // config
79 int err = rt_dev_ioctl(fd, RTSER_RTIOC_SET_CONFIG, &write_config);
80 if (err) {
81 char errorMsg[256];
82 Err("error rt_dev_ioctl RTSER_RTIOC_SET_CONFIG (%s)\n", strerror_r(-err, errorMsg, sizeof(errorMsg)));
83 }
84}
85
86void RTDM_SerialPort::FlushInput(void) {
87 char tmp;
88
89 SetRxTimeout(1000000);
90 while (rt_dev_read(fd, &tmp, 1) == 1)
91 ;
92 SetRxTimeout(TIME_INFINITE);
93}
94
95ssize_t RTDM_SerialPort::Write(const void *buf, size_t nbyte) {
96 return rt_dev_write(fd, buf, nbyte);
97}
98
99ssize_t RTDM_SerialPort::Read(void *buf, size_t nbyte) {
100 return rt_dev_read(fd, buf, nbyte);
101}
102
103} // end namespace core
104} // end namespace flair
105
106#else //__XENO__
107
108using std::string;
109using namespace flair::core;
110
111namespace flair {
112namespace core {
113
114RTDM_SerialPort::RTDM_SerialPort(const Object *parent, string name,
115 string device)
116 : SerialPort(parent, name) {
117 Err("not available in non real time\n");
118}
119
120RTDM_SerialPort::~RTDM_SerialPort() {}
121
122ssize_t RTDM_SerialPort::Write(const void *buf, size_t nbyte) { return -1; }
123
124ssize_t RTDM_SerialPort::Read(void *buf, size_t nbyte) { return -1; }
125
126void RTDM_SerialPort::SetBaudrate(int baudrate) {}
127
128void RTDM_SerialPort::SetRxTimeout(core::Time timeout_ns) {}
129
130void RTDM_SerialPort::FlushInput(void) {}
131
132} // end namespace core
133} // end namespace flair
134
135#endif
Note: See TracBrowser for help on using the repository browser.