1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
/*
Copyright (C) 2014 BARRATERO Laurent
AeroUp is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
Aeroup is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/*
* Filename: seriallinux.cpp
*
* Description: Serial lib linux (termios version)
* used if haven't boost
*
* Version: 0.2
* Created: 17/01/2014 03:31:32
* Revision: none
* Compiler: gcc
*
* Author: BARATTERO Laurent, laurentba<at>larueluberlu.net
* Organization: La rue Luberlu
*/
#include <stdlib.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <iostream>
#include <pthread.h>
#include "seriallinux.hpp"
using namespace std;
/*
* Class: SerialLinux
* Description: open/write to serial port
*/
SerialLinux::SerialLinux(std::string dev)
{
memset(&tio,0,sizeof(tio));
tio.c_iflag= IGNPAR ;
tio.c_oflag=0;
tio.c_cflag= CS8|CREAD|CLOCAL ;
tio.c_lflag=0;
tio.c_cc[VMIN]=1;
tio.c_cc[VTIME]=0;
fd = open( dev.c_str(), O_RDWR | O_NOCTTY);
if(fd < 0)
{
perror("open");
printf("file => %s\n", dev.c_str());
exit(EXIT_FAILURE);
}
tcgetattr(fd,&old_tio);
tcflush(fd, TCIFLUSH);
cfsetospeed(&tio, B19200);
cfsetispeed(&tio, B19200);
tcsetattr(fd,TCSANOW,&tio);
}
SerialLinux::~SerialLinux()
{
tcsetattr(fd,TCSANOW,&old_tio);
fclose();
}
void
SerialLinux::sendChar(char c)
{
write(fd,&c,1);
}
void
SerialLinux::sendString(uint8_t *c, int n)
{
write(fd, c, n);
}
string
SerialLinux::readLine()
{
char c[] = {0,0};
string result;
for(;;)
{
nb_read = read(fd, c, 1);
switch(c[0])
{
case '\r':
break;
case '\n':
return result;
default:
result+=c[0];
}
}
}
void
SerialLinux::fclose()
{
if (close(fd)<0)
{
perror("close");
exit(EXIT_FAILURE);
}
}
void
SerialLinux::setspeed(speed_t vitesse)
{
cfsetospeed(&tio, vitesse);
cfsetispeed(&tio, vitesse);
tcsetattr(fd,TCSANOW,&tio);
}
|