[2] | 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 | {
|
---|
| 30 | namespace core
|
---|
| 31 | {
|
---|
| 32 |
|
---|
| 33 | RTDM_SerialPort::RTDM_SerialPort(const Object* parent,string name,string device) : SerialPort(parent,name)
|
---|
| 34 | {
|
---|
| 35 | struct rtser_config write_config;
|
---|
| 36 |
|
---|
| 37 | write_config.config_mask = RTSER_SET_BAUD | RTSER_SET_TIMESTAMP_HISTORY,
|
---|
| 38 | write_config.baud_rate = 115200,
|
---|
| 39 | write_config.timestamp_history = RTSER_DEF_TIMESTAMP_HISTORY,
|
---|
| 40 | // the rest implicitely remains default
|
---|
| 41 |
|
---|
| 42 | fd = rt_dev_open(device.c_str(), 0);
|
---|
| 43 | if (fd < 0)
|
---|
| 44 | {
|
---|
| 45 | Err("erreur rt_dev_open (%s)\n",strerror(-fd));
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | // config
|
---|
| 49 | int err = rt_dev_ioctl(fd, RTSER_RTIOC_SET_CONFIG, &write_config );
|
---|
| 50 | if (err)
|
---|
| 51 | {
|
---|
| 52 | Err("erreur rt_dev_ioctl RTSER_RTIOC_SET_CONFIG (%s)\n",strerror(-err));
|
---|
| 53 | }
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | RTDM_SerialPort::~RTDM_SerialPort()
|
---|
| 57 | {
|
---|
| 58 | rt_dev_close(fd);
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | void RTDM_SerialPort::SetBaudrate(int baudrate)
|
---|
| 62 | {
|
---|
| 63 | struct rtser_config write_config;
|
---|
| 64 |
|
---|
| 65 | write_config.config_mask = RTSER_SET_BAUD;
|
---|
| 66 | write_config.baud_rate = baudrate;
|
---|
| 67 | // the rest implicitely remains default
|
---|
| 68 |
|
---|
| 69 | // config
|
---|
| 70 | int err = rt_dev_ioctl(fd, RTSER_RTIOC_SET_CONFIG, &write_config );
|
---|
| 71 | if (err)
|
---|
| 72 | {
|
---|
| 73 | Err("erreur rt_dev_ioctl RTSER_RTIOC_SET_CONFIG (%s)\n",strerror(-err));
|
---|
| 74 | }
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | void RTDM_SerialPort::SetRxTimeout(core::Time timeout_ns)
|
---|
| 78 | {
|
---|
| 79 | struct rtser_config write_config;
|
---|
| 80 |
|
---|
| 81 | write_config.config_mask = RTSER_SET_TIMEOUT_RX;
|
---|
| 82 | write_config.rx_timeout = timeout_ns;
|
---|
| 83 | // the rest implicitely remains default
|
---|
| 84 |
|
---|
| 85 | // config
|
---|
| 86 | int err = rt_dev_ioctl(fd, RTSER_RTIOC_SET_CONFIG, &write_config );
|
---|
| 87 | if (err)
|
---|
| 88 | {
|
---|
| 89 | Err("erreur rt_dev_ioctl RTSER_RTIOC_SET_CONFIG (%s)\n",strerror(-err));
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | void RTDM_SerialPort::FlushInput(void)
|
---|
| 94 | {
|
---|
| 95 | char tmp;
|
---|
| 96 |
|
---|
| 97 | SetRxTimeout(1000000);
|
---|
| 98 | while(rt_dev_read(fd,&tmp,1)==1);
|
---|
| 99 | SetRxTimeout(TIME_INFINITE);
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | ssize_t RTDM_SerialPort::Write(const void *buf,size_t nbyte)
|
---|
| 103 | {
|
---|
| 104 | return rt_dev_write(fd, buf, nbyte);
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | ssize_t RTDM_SerialPort::Read(void *buf,size_t nbyte)
|
---|
| 108 | {
|
---|
| 109 | return rt_dev_read(fd, buf, nbyte);
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | } // end namespace core
|
---|
| 113 | } // end namespace flair
|
---|
| 114 |
|
---|
| 115 | #else //__XENO__
|
---|
| 116 |
|
---|
| 117 | using std::string;
|
---|
| 118 | using namespace flair::core;
|
---|
| 119 |
|
---|
| 120 |
|
---|
| 121 | namespace flair
|
---|
| 122 | {
|
---|
| 123 | namespace core
|
---|
| 124 | {
|
---|
| 125 |
|
---|
| 126 | RTDM_SerialPort::RTDM_SerialPort(const Object* parent,string name,string device) : SerialPort(parent,name)
|
---|
| 127 | {
|
---|
| 128 | Err("not available in non real time\n");
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | RTDM_SerialPort::~RTDM_SerialPort()
|
---|
| 132 | {
|
---|
| 133 |
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | ssize_t RTDM_SerialPort::Write(const void *buf,size_t nbyte)
|
---|
| 137 | {
|
---|
| 138 | return -1;
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | ssize_t RTDM_SerialPort::Read(void *buf,size_t nbyte)
|
---|
| 142 | {
|
---|
| 143 | return -1;
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | void RTDM_SerialPort::SetBaudrate(int baudrate)
|
---|
| 147 | {
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | void RTDM_SerialPort::SetRxTimeout(core::Time timeout_ns)
|
---|
| 151 | {
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | void RTDM_SerialPort::FlushInput(void)
|
---|
| 155 | {
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | } // end namespace core
|
---|
| 159 | } // end namespace flair
|
---|
| 160 |
|
---|
| 161 | #endif
|
---|