serial_loopback.cpp
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD-3 License)
3  *
4  * Copyright (c) 2018 Daniel Koch.
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 are met:
9  *
10  * * Redistributions of source code must retain the above copyright notice, this
11  * list of conditions and the following disclaimer.
12  *
13  * * Redistributions in binary form must reproduce the above copyright notice,
14  * this list of conditions and the following disclaimer in the documentation
15  * and/or other materials provided with the distribution.
16  *
17  * * Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived from
19  * this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
41 #include <async_comm/serial.h>
42 
43 #include <cstdint>
44 #include <cstdio>
45 
46 #include <chrono>
47 #include <thread>
48 
49 #define NUM_BYTES 64
50 
51 
60 void callback(const uint8_t* buf, size_t len)
61 {
62  for (size_t i = 0; i < len; i++)
63  {
64  std::printf("Received byte: %d\n", buf[i]);
65  }
66 }
67 
68 
69 int main(int argc, char** argv)
70 {
71  // initialize
72  char* port;
73  if (argc < 2)
74  {
75  std::printf("USAGE: %s PORT\n", argv[0]);
76  return 1;
77  }
78  else
79  {
80  std::printf("Using port %s\n", argv[1]);
81  port = argv[1];
82  }
83 
84  // open serial port
85  async_comm::Serial serial(port, 115200);
87 
88  if (!serial.init())
89  {
90  std::printf("Failed to initialize serial port\n");
91  return 2;
92  }
93 
94  uint8_t buffer[NUM_BYTES];
95 
96  // test sending bytes one at a time
97  std::printf("Transmit individual bytes:\n");
98  for (uint8_t i = 0; i < NUM_BYTES; i++)
99  {
100  buffer[i] = i;
101  serial.send_byte(i);
102  }
103 
104  // wait for all bytes to be received
105  std::this_thread::sleep_for(std::chrono::milliseconds(500));
106 
107  // test sending all the bytes at once
108  std::printf("Bulk transmit:\n");
109  serial.send_bytes(buffer, NUM_BYTES);
110 
111  // wait for all bytes to be received
112  std::this_thread::sleep_for(std::chrono::milliseconds(500));
113 
114  // close serial port
115  serial.close();
116 
117  return 0;
118 }
Asynchronous communication class for a serial port.
Definition: serial.h:56
int main(int argc, char **argv)
void callback(const uint8_t *buf, size_t len)
Callback function for the async_comm library.
void send_bytes(const uint8_t *src, size_t len)
Send bytes from a buffer over the port.
Definition: comm.cpp:97
bool init()
Initializes and opens the port.
Definition: comm.cpp:61
void send_byte(uint8_t data)
Send a single byte over the port.
Definition: comm.h:113
void register_receive_callback(std::function< void(const uint8_t *, size_t)> fun)
Register a callback function for when bytes are received on the port.
Definition: comm.cpp:110
void close()
Closes the port.
Definition: comm.cpp:74
#define NUM_BYTES


async_comm
Author(s):
autogenerated on Fri May 14 2021 02:35:38