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

Last change on this file since 2 was 2, checked in by Sanahuja Guillaume, 8 years ago

flaircore

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