source: flair-src/trunk/lib/FlairCore/src/RTDM_SerialPort.cpp@ 67

Last change on this file since 67 was 15, checked in by Bayard Gildas, 6 years ago

sources reformatted with flair-format-dir script

File size: 3.2 KB
RevLine 
[2]1// %flair:license{
[15]2// This file is part of the Flair framework distributed under the
3// CECILL-C License, Version 1.0.
[2]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
26using std::string;
27
[15]28namespace flair {
29namespace core {
[2]30
[15]31RTDM_SerialPort::RTDM_SerialPort(const Object *parent, string name,
32 string device)
33 : SerialPort(parent, name) {
34 struct rtser_config write_config;
[2]35
[15]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
[2]40
[15]41 fd = rt_dev_open(device.c_str(), 0);
42 if (fd < 0) {
43 Err("erreur rt_dev_open (%s)\n", strerror(-fd));
44 }
[2]45
[15]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 }
[2]51}
52
[15]53RTDM_SerialPort::~RTDM_SerialPort() { rt_dev_close(fd); }
[2]54
[15]55void RTDM_SerialPort::SetBaudrate(int baudrate) {
56 struct rtser_config write_config;
[2]57
[15]58 write_config.config_mask = RTSER_SET_BAUD;
59 write_config.baud_rate = baudrate;
60 // the rest implicitely remains default
[2]61
[15]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 }
[2]67}
68
[15]69void RTDM_SerialPort::SetRxTimeout(core::Time timeout_ns) {
70 struct rtser_config write_config;
[2]71
[15]72 write_config.config_mask = RTSER_SET_TIMEOUT_RX;
73 write_config.rx_timeout = timeout_ns;
74 // the rest implicitely remains default
[2]75
[15]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 }
[2]81}
82
[15]83void RTDM_SerialPort::FlushInput(void) {
84 char tmp;
[2]85
[15]86 SetRxTimeout(1000000);
87 while (rt_dev_read(fd, &tmp, 1) == 1)
88 ;
89 SetRxTimeout(TIME_INFINITE);
[2]90}
91
[15]92ssize_t RTDM_SerialPort::Write(const void *buf, size_t nbyte) {
93 return rt_dev_write(fd, buf, nbyte);
[2]94}
95
[15]96ssize_t RTDM_SerialPort::Read(void *buf, size_t nbyte) {
97 return rt_dev_read(fd, buf, nbyte);
[2]98}
99
100} // end namespace core
101} // end namespace flair
102
103#else //__XENO__
104
105using std::string;
106using namespace flair::core;
107
[15]108namespace flair {
109namespace core {
[2]110
[15]111RTDM_SerialPort::RTDM_SerialPort(const Object *parent, string name,
112 string device)
113 : SerialPort(parent, name) {
114 Err("not available in non real time\n");
[2]115}
116
[15]117RTDM_SerialPort::~RTDM_SerialPort() {}
[2]118
[15]119ssize_t RTDM_SerialPort::Write(const void *buf, size_t nbyte) { return -1; }
[2]120
[15]121ssize_t RTDM_SerialPort::Read(void *buf, size_t nbyte) { return -1; }
[2]122
[15]123void RTDM_SerialPort::SetBaudrate(int baudrate) {}
[2]124
[15]125void RTDM_SerialPort::SetRxTimeout(core::Time timeout_ns) {}
[2]126
[15]127void RTDM_SerialPort::FlushInput(void) {}
[2]128
129} // end namespace core
130} // end namespace flair
131
132#endif
Note: See TracBrowser for help on using the repository browser.