Goby v2
serial2tcp_server.cpp
1 // Copyright 2009-2018 Toby Schneider (http://gobysoft.org/index.wt/people/toby)
2 // GobySoft, LLC (2013-)
3 // Massachusetts Institute of Technology (2007-2014)
4 //
5 //
6 // This file is part of the Goby Underwater Autonomy Project Binaries
7 // ("The Goby Binaries").
8 //
9 // The Goby Binaries are free software: you can redistribute them and/or modify
10 // them under the terms of the GNU General Public License as published by
11 // the Free Software Foundation, either version 2 of the License, or
12 // (at your option) any later version.
13 //
14 // The Goby Binaries are distributed in the hope that they will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with Goby. If not, see <http://www.gnu.org/licenses/>.
21 
22 #include <iostream>
23 
24 #include "goby/util/as.h"
25 #include "goby/util/linebasedcomms.h"
26 
27 using goby::util::as;
28 
29 int main(int argc, char* argv[])
30 {
31  int run_freq = 100;
32 
33  if (argc < 4)
34  {
35  std::cout
36  << "usage: serial2tcp_server server_port serial_port serial_baud [run-frequency=100]"
37  << std::endl;
38  return 1;
39  }
40 
41  std::string server_port = argv[1];
42  std::string serial_port = argv[2];
43  std::string serial_baud = argv[3];
44  if (argc == 5)
45  run_freq = goby::util::as<int>(argv[4]);
46 
47  goby::util::TCPServer tcp_server(as<unsigned>(server_port));
48  goby::util::SerialClient serial_client(serial_port, as<unsigned>(serial_baud));
49 
50  tcp_server.start();
51  serial_client.start();
52 
53  std::string s;
54  for (;;)
55  {
56  while (serial_client.readline(&s)) tcp_server.write(s);
57 
58  while (tcp_server.readline(&s)) serial_client.write(s);
59 
60  usleep(1000000 / run_freq);
61  }
62 }
provides a basic client for line by line text based communications over a 8N1 tty (such as an RS-232 ...
Definition: serial_client.h:35
provides a basic TCP server for line by line text based communications to a one or more remote TCP cl...
Definition: tcp_server.h:49