Sfoglia il codice sorgente

working communication

develop
Robin Thoni 8 anni fa
parent
commit
e662d0299c
5 ha cambiato i file con 164 aggiunte e 14 eliminazioni
  1. 1
    1
      src/DBO/Result.h
  2. 82
    9
      src/DataAccess/ArduinoSerial.cpp
  3. 11
    1
      src/DataAccess/ArduinoSerial.h
  4. 22
    0
      src/config.h
  5. 48
    3
      src/main.cpp

+ 1
- 1
src/DBO/Result.h Vedi File

47
 
47
 
48
 typedef Result<bool> BResult;
48
 typedef Result<bool> BResult;
49
 
49
 
50
-# include "DBO/Result.hxx"
50
+# include "Result.hxx"
51
 
51
 
52
 #endif //PDNS_SLAVE_RESULT_H
52
 #endif //PDNS_SLAVE_RESULT_H

+ 82
- 9
src/DataAccess/ArduinoSerial.cpp Vedi File

25
     close();
25
     close();
26
 }
26
 }
27
 
27
 
28
-int ArduinoSerial::open()
28
+BResult ArduinoSerial::open()
29
 {
29
 {
30
     struct termios toptions;
30
     struct termios toptions;
31
 
31
 
33
     fd_ = ::open(port_.c_str(), O_RDWR | O_NONBLOCK);
33
     fd_ = ::open(port_.c_str(), O_RDWR | O_NONBLOCK);
34
 
34
 
35
     if (fd_ == -1)  {
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
     //int iflags = TIOCM_DTR;
39
     //int iflags = TIOCM_DTR;
42
     //ioctl(fd, TIOCMBIC, &iflags);    // turn off DTR
41
     //ioctl(fd, TIOCMBIC, &iflags);    // turn off DTR
43
 
42
 
44
     if (tcgetattr(fd_, &toptions) < 0) {
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
     speed_t brate;
46
     speed_t brate;
49
     switch(bauderate_) {
47
     switch(bauderate_) {
104
 
102
 
105
     tcsetattr(fd_, TCSANOW, &toptions);
103
     tcsetattr(fd_, TCSANOW, &toptions);
106
     if (tcsetattr(fd_, TCSAFLUSH, &toptions) < 0) {
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
 void ArduinoSerial::close()
111
 void ArduinoSerial::close()
117
         ::close(fd_);
114
         ::close(fd_);
118
         fd_ = 0;
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
+}

+ 11
- 1
src/DataAccess/ArduinoSerial.h Vedi File

7
 
7
 
8
 
8
 
9
 #include <string>
9
 #include <string>
10
+#include "../DBO/Result.h"
11
+#include "../config.h"
10
 
12
 
11
 class ArduinoSerial
13
 class ArduinoSerial
12
 {
14
 {
14
     ArduinoSerial(std::string port, int baudrate);
16
     ArduinoSerial(std::string port, int baudrate);
15
     virtual ~ArduinoSerial();
17
     virtual ~ArduinoSerial();
16
 
18
 
17
-    int open();
19
+    BResult open();
18
     void close();
20
     void close();
19
 
21
 
22
+    Result<std::pair<SERIAL_PACKET_TYPE, std::string>> read();
23
+
24
+    BResult write(const char* data, SERIAL_PACKET_LENGTH_TYPE length);
25
+
20
 protected:
26
 protected:
27
+    BResult readBytes(char* buffer, size_t length);
28
+
29
+    BResult writeBytes(const char* data, size_t length);
30
+
21
     std::string port_;
31
     std::string port_;
22
     int bauderate_;
32
     int bauderate_;
23
 
33
 

+ 22
- 0
src/config.h Vedi File

1
+//
2
+// Created by robin on 5/6/16.
3
+//
4
+
5
+#ifndef DIGICODE_HOST_CONFIG_H
6
+#define DIGICODE_HOST_CONFIG_H
7
+
8
+#define SERIAL_BAUDRATE 9600
9
+#define SERIAL_PACKET_TYPE unsigned char
10
+#define SERIAL_PACKET_LENGTH_TYPE unsigned char
11
+#define SERIAL_PACKET_TYPE_INT unsigned char
12
+
13
+
14
+#define ERROR_NONE 0
15
+#define ERROR_HOST_PROTOCOL 1
16
+#define ERROR_HOST_TIMEOUT 2
17
+
18
+#define PACKET_DEBUG 1
19
+#define PACKET_SELF_TEST 2
20
+#define PACKET_LOGIN 3
21
+
22
+#endif //DIGICODE_HOST_CONFIG_H

+ 48
- 3
src/main.cpp Vedi File

1
 #include <iostream>
1
 #include <iostream>
2
-
2
+#include<signal.h>
3
 #include "DataAccess/ArduinoSerial.h"
3
 #include "DataAccess/ArduinoSerial.h"
4
 
4
 
5
+
6
+constexpr char hexmap[] = {'0', '1', '2', '3', '4', '5', '6', '7',
7
+                           '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
8
+
9
+std::string hexStr(std::string str)
10
+{
11
+    std::string s(str.size() * 2, ' ');
12
+    for (int i = 0; i < str.size(); ++i) {
13
+        s[2 * i]     = hexmap[(str[i] & 0xF0) >> 4];
14
+        s[2 * i + 1] = hexmap[str[i] & 0x0F];
15
+    }
16
+    return s;
17
+}
18
+
19
+int lastSignal = 0;
20
+
21
+void sig_handler(int signo)
22
+{
23
+    std::cout << "Signal catched " << signo << std::endl;
24
+    lastSignal = signo;
25
+}
26
+
5
 int main()
27
 int main()
6
 {
28
 {
7
-    ArduinoSerial arduinoSerial("/dev/ttyUSB0", 9600);
8
-    std::cout << arduinoSerial.open() << std::endl;
29
+    signal(SIGINT, sig_handler);
30
+    signal(SIGTERM, sig_handler);
31
+    signal(SIGKILL, sig_handler);
32
+
33
+    ArduinoSerial arduinoSerial("/dev/ttyUSB0", SERIAL_BAUDRATE);
34
+    auto res = arduinoSerial.open();
35
+    if (!res) {
36
+        res.print();
37
+        return 1;
38
+    }
39
+    while (lastSignal == 0)
40
+    {
41
+        auto data = arduinoSerial.read();
42
+        if (!data) {
43
+            data.print();
44
+            return 2;
45
+        }
46
+        std::cout << (int) data.getData().first << " " << hexStr(data.getData().second)
47
+        << " " << data.getData().second << std::endl;
48
+        if (data.getData().first == PACKET_SELF_TEST) {
49
+            SERIAL_PACKET_TYPE_INT status = 42;
50
+            arduinoSerial.write((const char*)&status, sizeof(status));
51
+        }
52
+    }
9
     return 0;
53
     return 0;
54
+
10
 }
55
 }

Loading…
Annulla
Salva