ArduinoTcpHardware.h
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2011, Willow Garage, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Willow Garage, Inc. nor the names of its
18  * contributors may be used to endorse or promote prducts derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #ifndef ROS_ARDUINO_TCP_HARDWARE_H_
36 #define ROS_ARDUINO_TCP_HARDWARE_H_
37 
38 #include <Arduino.h>
39 #if defined(ESP8266)
40  #include <ESP8266WiFi.h>
41 #elif defined(ESP32)
42  #include <WiFi.h> // Using Espressif's WiFi.h
43 #else
44  #include <SPI.h>
45  #include <Ethernet.h>
46 #endif
47 
48 class ArduinoHardware {
49 public:
51  {
52  }
53 
54  void setConnection(IPAddress &server, int port = 11411)
55  {
56  server_ = server;
57  serverPort_ = port;
58  }
59 
60  IPAddress getLocalIP()
61  {
62 #if defined(ESP8266) or defined(ESP32)
63  return tcp_.localIP();
64 #else
65  return Ethernet.localIP();
66 #endif
67  }
68 
69  void init()
70  {
71  if(tcp_.connected())
72  {
73  tcp_.stop();
74  }
75  tcp_.connect(server_, serverPort_);
76  }
77 
78  int read(){
79  if (tcp_.connected())
80  {
81  return tcp_.read();
82  }
83  else
84  {
85  tcp_.stop();
86  tcp_.connect(server_, serverPort_);
87  }
88  return -1;
89  }
90 
91  void write(const uint8_t* data, int length)
92  {
93  tcp_.write(data, length);
94  }
95 
96  unsigned long time()
97  {
98  return millis();
99  }
100 
101  bool connected()
102  {
103  return tcp_.connected();
104  }
105 
106 protected:
107 #if defined(ESP8266) or defined(ESP32)
108  WiFiClient tcp_;
109 #else
110  EthernetClient tcp_;
111 #endif
112  IPAddress server_;
113  uint16_t serverPort_ = 11411;
114 };
115 
116 #endif
EthernetClient tcp_
void write(const uint8_t *data, int length)
IPAddress getLocalIP()
void setConnection(IPAddress &server, int port=11411)
unsigned long time()


rosserial_arduino
Author(s): Michael Ferguson, Adam Stambler
autogenerated on Fri Jun 7 2019 22:02:56