-
Notifications
You must be signed in to change notification settings - Fork 0
/
uart_to_abmt.cpp
85 lines (76 loc) · 1.99 KB
/
uart_to_abmt.cpp
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
#include <iostream>
#include <filesystem>
#include <abmt/io/eio.h>
#include <abmt/util/bt_exception.h>
#include <unistd.h>
using namespace abmt;
using namespace abmt::io;
using namespace std;
void abmt::log(std::string s){
cout << s << endl;
}
void abmt::log_err(std::string s){
cout << s << endl;
}
void abmt::die(std::string s){
throw abmt::util::bt_exception(s);
}
void abmt::die_if(bool condition, std::string msg){
if(condition){
abmt::die(msg);
}
}
int main(int argc, char* argv[]){
bool quit = false;
if(argc != 3){
cout << "usage:uart_to_abmt <serial_port> <baudrate>" << endl;
return -1;
}
string port = argv[1];
int baud_rate = stol(argv[2]);
usleep(150*1000); // controller reset delay
for(int i = 0; i < 60; i++){
if(std::filesystem::exists(port)){
break;
}
usleep(100*1000);
}
try{
usleep(150*1000); // premissions might not been setup
cout << "Open serial port "<< port << " with baud rate: " << baud_rate << "..." << endl;
abmt::io::event_list e;
abmt::io::serial_ptr uart_con(new abmt::io::serial(e,port,baud_rate, true));
usleep(250*1000);
uint16_t port = 15101;
const char* port_str = std::getenv("ABMT_MODEL_PORT");
if(port_str != nullptr){
port = std::stoul(port_str);
}
abmt::io::tcp_ptr abmt_con = abmt::io::tcp::connect_to(e,{"127.0.0.1",port});
abmt_con->on_new_data = [&](abmt::blob& b)->size_t{
uart_con->send(b.data, b.size);
return b.size;
};
abmt_con->on_close = [&]{
cout << "Abmt closed connection..." << endl;
quit = true;
};
uart_con->on_new_data = [&](abmt::blob& b)->size_t{
abmt_con->send(b);
return b.size;
};
while(quit == false){
e.wait(100);
}
}catch(abmt::util::bt_exception& e){
cout << "An exception occured: " << e.what() << endl;
//e.print_backtrace();
}catch(std::exception& e){
cout << "An exception occured: " << e.what() << endl;
}catch(const char* c ){
cout << "An exception occured: " << c << endl;
}
catch(...){
cout << "An unknown exception occured... " << endl;
}
}