|
@@ -25,7 +25,7 @@ ArduinoSerial::~ArduinoSerial()
|
25
|
25
|
close();
|
26
|
26
|
}
|
27
|
27
|
|
28
|
|
-int ArduinoSerial::open()
|
|
28
|
+BResult ArduinoSerial::open()
|
29
|
29
|
{
|
30
|
30
|
struct termios toptions;
|
31
|
31
|
|
|
@@ -33,8 +33,7 @@ int ArduinoSerial::open()
|
33
|
33
|
fd_ = ::open(port_.c_str(), O_RDWR | O_NONBLOCK);
|
34
|
34
|
|
35
|
35
|
if (fd_ == -1) {
|
36
|
|
- perror("serialport_init: Unable to open port ");
|
37
|
|
- return -1;
|
|
36
|
+ return BResult().error(strerror(errno));
|
38
|
37
|
}
|
39
|
38
|
|
40
|
39
|
//int iflags = TIOCM_DTR;
|
|
@@ -42,8 +41,7 @@ int ArduinoSerial::open()
|
42
|
41
|
//ioctl(fd, TIOCMBIC, &iflags); // turn off DTR
|
43
|
42
|
|
44
|
43
|
if (tcgetattr(fd_, &toptions) < 0) {
|
45
|
|
- perror("serialport_init: Couldn't get term attributes");
|
46
|
|
- return -1;
|
|
44
|
+ return BResult().error(strerror(errno));
|
47
|
45
|
}
|
48
|
46
|
speed_t brate;
|
49
|
47
|
switch(bauderate_) {
|
|
@@ -104,11 +102,10 @@ int ArduinoSerial::open()
|
104
|
102
|
|
105
|
103
|
tcsetattr(fd_, TCSANOW, &toptions);
|
106
|
104
|
if (tcsetattr(fd_, TCSAFLUSH, &toptions) < 0) {
|
107
|
|
- perror("init_serialport: Couldn't set term attributes");
|
108
|
|
- return -1;
|
|
105
|
+ return BResult().error(strerror(errno));
|
109
|
106
|
}
|
110
|
107
|
|
111
|
|
- return 0;
|
|
108
|
+ return BResult().ok(true);
|
112
|
109
|
}
|
113
|
110
|
|
114
|
111
|
void ArduinoSerial::close()
|
|
@@ -117,4 +114,80 @@ void ArduinoSerial::close()
|
117
|
114
|
::close(fd_);
|
118
|
115
|
fd_ = 0;
|
119
|
116
|
}
|
120
|
|
-}
|
|
117
|
+}
|
|
118
|
+
|
|
119
|
+Result<std::pair<SERIAL_PACKET_TYPE, std::string>> ArduinoSerial::read()
|
|
120
|
+{
|
|
121
|
+ SERIAL_PACKET_TYPE packetType = 0;
|
|
122
|
+ auto res = readBytes((char*)&packetType, sizeof(packetType));
|
|
123
|
+ if (!res) {
|
|
124
|
+ return Result<std::pair<SERIAL_PACKET_TYPE, std::string>>().error(res.getError());
|
|
125
|
+ }
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+ SERIAL_PACKET_LENGTH_TYPE length = 0;
|
|
129
|
+ res = readBytes((char*)&length, sizeof(length));
|
|
130
|
+ if (!res) {
|
|
131
|
+ return Result<std::pair<SERIAL_PACKET_TYPE, std::string>>().error(res.getError());
|
|
132
|
+ }
|
|
133
|
+
|
|
134
|
+ if (length > 0) {
|
|
135
|
+ char buff[length];
|
|
136
|
+
|
|
137
|
+ res = readBytes(buff, length);
|
|
138
|
+ if (!res) {
|
|
139
|
+ return Result<std::pair<SERIAL_PACKET_TYPE, std::string>>().error(res.getError());
|
|
140
|
+ }
|
|
141
|
+
|
|
142
|
+ std::pair<SERIAL_PACKET_TYPE, std::string> pair(packetType, std::string(buff, length));
|
|
143
|
+
|
|
144
|
+ return Result<std::pair<SERIAL_PACKET_TYPE, std::string>>().ok(pair);
|
|
145
|
+ }
|
|
146
|
+
|
|
147
|
+ std::pair<SERIAL_PACKET_TYPE, std::string> pair(packetType, "");
|
|
148
|
+
|
|
149
|
+ return Result<std::pair<SERIAL_PACKET_TYPE, std::string>>().ok(pair);
|
|
150
|
+}
|
|
151
|
+
|
|
152
|
+BResult ArduinoSerial::readBytes(char* buffer, size_t length)
|
|
153
|
+{
|
|
154
|
+ ssize_t res = 0;
|
|
155
|
+ bool done = false;
|
|
156
|
+ size_t pos = 0;
|
|
157
|
+ while (!done)
|
|
158
|
+ {
|
|
159
|
+ res = ::read(fd_, buffer + pos, (size_t) length);
|
|
160
|
+ if (res < 0)
|
|
161
|
+ {
|
|
162
|
+ return BResult().error(strerror(errno));
|
|
163
|
+ }
|
|
164
|
+ if (res > 0) {
|
|
165
|
+ pos += res;
|
|
166
|
+ if (pos >= length) {
|
|
167
|
+ done = true;
|
|
168
|
+ }
|
|
169
|
+ }
|
|
170
|
+ else {
|
|
171
|
+ usleep(1);
|
|
172
|
+ }
|
|
173
|
+ }
|
|
174
|
+ return BResult().ok(true);
|
|
175
|
+}
|
|
176
|
+
|
|
177
|
+BResult ArduinoSerial::write(const char *data, SERIAL_PACKET_LENGTH_TYPE length)
|
|
178
|
+{
|
|
179
|
+ auto res = writeBytes((const char*)&length, sizeof(length));
|
|
180
|
+ if (!res) {
|
|
181
|
+ return res;
|
|
182
|
+ }
|
|
183
|
+ return writeBytes((const char*)&data, length);
|
|
184
|
+}
|
|
185
|
+
|
|
186
|
+BResult ArduinoSerial::writeBytes(const char *data, size_t length)
|
|
187
|
+{
|
|
188
|
+ auto res = ::write(fd_, data, length);
|
|
189
|
+ if (res < 0) {
|
|
190
|
+ return BResult().error(strerror(errno));
|
|
191
|
+ }
|
|
192
|
+ return BResult().ok(true);
|
|
193
|
+}
|