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

Last change on this file since 415 was 133, checked in by Sanahuja Guillaume, 7 years ago

modif sterror

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