source: flair-src/trunk/lib/FlairCore/src/Unix_SerialPort.cpp @ 16

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

sources reformatted with flair-format-dir script

File size: 3.3 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:   Unix_SerialPort.cpp
7//
8//  author:     Guillaume Sanahuja
9//              Copyright Heudiasyc UMR UTC/CNRS 7253
10//
11//  version:    $Id: $
12//
13//  purpose:    Class for unix serial port
14//
15//
16/*********************************************************************/
17
18#include "Unix_SerialPort.h"
19#include <fcntl.h> /* File control definitions */
20#include <unistd.h>
21
22using std::string;
23
24namespace flair {
25namespace core {
26
27Unix_SerialPort::Unix_SerialPort(const Object *parent, string name,
28                                 string device)
29    : SerialPort(parent, name) {
30  // open port
31  fd = open(device.c_str(), O_RDWR | O_NOCTTY); // |O_NDELAY|O_NONBLOCK);
32  if (fd == -1) {
33    Err("open_port: Unable to open %s\n", device.c_str());
34  }
35  // fcntl(fd, F_SETFL, 0); //read calls are non blocking
36
37  // Get the current options for the port
38
39  tcgetattr(fd, &options);
40  // Set the baud rates to 115200
41  cfsetispeed(&options, B115200);
42  cfsetospeed(&options, B115200);
43
44  options.c_cflag |= (CLOCAL | CREAD); // Enable the receiver and set local mode
45  options.c_iflag = 0;                 // clear input options
46  options.c_lflag = 0;                 // clear local options
47  options.c_oflag &= ~OPOST;           // clear output options (raw output)
48
49  // Set the new options for the port
50  options.c_cc[VTIME] = 0;
51  options.c_cc[VMIN] = 1;
52  FlushInput();
53  tcsetattr(fd, TCSANOW, &options);
54}
55
56Unix_SerialPort::~Unix_SerialPort() { close(fd); }
57
58void Unix_SerialPort::SetBaudrate(int baudrate) {
59  // set port options
60  struct termios options;
61  // Get the current options for the port
62  tcgetattr(fd, &options);
63  // Set the baud rates to 115200
64
65  switch (baudrate) {
66  case 1200:
67    cfsetispeed(&options, B1200);
68    cfsetospeed(&options, B1200);
69    break;
70  case 2400:
71    cfsetispeed(&options, B2400);
72    cfsetospeed(&options, B2400);
73    break;
74  case 4800:
75    cfsetispeed(&options, B4800);
76    cfsetospeed(&options, B4800);
77    break;
78  case 9600:
79    cfsetispeed(&options, B9600);
80    cfsetospeed(&options, B9600);
81    break;
82  case 19200:
83    cfsetispeed(&options, B19200);
84    cfsetospeed(&options, B19200);
85    break;
86  case 38400:
87    cfsetispeed(&options, B38400);
88    cfsetospeed(&options, B38400);
89    break;
90  case 115200:
91    cfsetispeed(&options, B115200);
92    cfsetospeed(&options, B115200);
93    break;
94  case 460800:
95    cfsetispeed(&options, B460800);
96    cfsetospeed(&options, B460800);
97    break;
98  case 921600:
99    cfsetispeed(&options, B921600);
100    cfsetospeed(&options, B921600);
101    break;
102  default:
103    Err("unsupported baudrate\n");
104  }
105  tcsetattr(fd, TCSANOW, &options);
106  FlushInput();
107}
108
109void Unix_SerialPort::SetRxTimeout(core::Time timeout_ns) {}
110
111void Unix_SerialPort::FlushInput(void) { tcflush(fd, TCIFLUSH); }
112
113ssize_t Unix_SerialPort::Write(const void *buf, size_t nbyte) {
114  return write(fd, buf, nbyte);
115}
116
117ssize_t Unix_SerialPort::Read(void *buf, size_t nbyte) {
118  if (options.c_cc[VMIN] != nbyte) {
119    tcgetattr(fd, &options); // bug if not called?
120    options.c_cc[VTIME] = 0;
121    options.c_cc[VMIN] = nbyte;
122    tcsetattr(fd, TCSANOW, &options);
123  }
124
125  return read(fd, buf, nbyte);
126}
127
128} // end namespace core
129} // end namespace flair
Note: See TracBrowser for help on using the repository browser.