source: flair-src/branches/sanscv/lib/FlairCore/src/Unix_SerialPort.cpp@ 338

Last change on this file since 338 was 324, checked in by Sanahuja Guillaume, 5 years ago

removing opencv dependency

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 tcsetattr(fd, TCSANOW, &options);
53 FlushInput();
54}
55
56Unix_SerialPort::~Unix_SerialPort() { close(fd); }
57
58void Unix_SerialPort::SetBaudrate(int baudrate) {
59 struct termios options;
60 // Get the current options for the port
61 tcgetattr(fd, &options);
62
63 switch (baudrate) {
64 case 1200:
65 cfsetispeed(&options, B1200);
66 cfsetospeed(&options, B1200);
67 break;
68 case 2400:
69 cfsetispeed(&options, B2400);
70 cfsetospeed(&options, B2400);
71 break;
72 case 4800:
73 cfsetispeed(&options, B4800);
74 cfsetospeed(&options, B4800);
75 break;
76 case 9600:
77 cfsetispeed(&options, B9600);
78 cfsetospeed(&options, B9600);
79 break;
80 case 19200:
81 cfsetispeed(&options, B19200);
82 cfsetospeed(&options, B19200);
83 break;
84 case 38400:
85 cfsetispeed(&options, B38400);
86 cfsetospeed(&options, B38400);
87 break;
88 case 115200:
89 cfsetispeed(&options, B115200);
90 cfsetospeed(&options, B115200);
91 break;
92 case 230400:
93 cfsetispeed(&options, B230400);
94 cfsetospeed(&options, B230400);
95 break;
96 case 460800:
97 cfsetispeed(&options, B460800);
98 cfsetospeed(&options, B460800);
99 break;
100 case 921600:
101 cfsetispeed(&options, B921600);
102 cfsetospeed(&options, B921600);
103 break;
104 default:
105 Err("unsupported baudrate\n");
106 }
107 tcsetattr(fd, TCSANOW, &options);
108 FlushInput();
109}
110
111void Unix_SerialPort::SetRxTimeout(core::Time timeout_ns) {}
112
113void Unix_SerialPort::FlushInput(void) { tcflush(fd, TCIFLUSH); }
114
115ssize_t Unix_SerialPort::Write(const void *buf, size_t nbyte) {
116 return write(fd, buf, nbyte);
117}
118
119ssize_t Unix_SerialPort::Read(void *buf, size_t nbyte) {
120 if (options.c_cc[VMIN] != nbyte) {
121 tcgetattr(fd, &options); // bug if not called?
122 options.c_cc[VTIME] = 0;
123 options.c_cc[VMIN] = nbyte;
124 tcsetattr(fd, TCSANOW, &options);
125 }
126
127 return read(fd, buf, nbyte);
128}
129
130} // end namespace core
131} // end namespace flair
Note: See TracBrowser for help on using the repository browser.