Created
October 30, 2018 01:06
-
-
Save ncorgan/b443bc177b1100fd16c34f0058dc75a4 to your computer and use it in GitHub Desktop.
Minimal example code for a Jetson to publish messages with zmqpp to be received by a RoboRIO with JeroMQ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <zmqpp/zmqpp.hpp> | |
| #include <chrono> | |
| #include <iostream> | |
| #include <thread> | |
| int main(int argc, char* argv[]) | |
| { | |
| const std::string publish_endpoint = "tcp://*:5038"; | |
| zmqpp::context context; | |
| zmqpp::socket_type type = zmqpp::socket_type::publish; | |
| zmqpp::socket socket(context, type); | |
| socket.bind(publish_endpoint); | |
| std::cout << "Bound to " << publish_endpoint << std::endl; | |
| // Sleep to allow time for the socket to bind. | |
| std::this_thread::sleep_for(std::chrono::milliseconds(1000)); | |
| while(true) | |
| { | |
| zmqpp::message message("Jetson message: Hey look, a target!"); | |
| socket.send(message); | |
| std::cout << "Sent message to RoboRIO" << std::endl; | |
| std::this_thread::sleep_for(std::chrono::milliseconds(1000)); | |
| } | |
| socket.disconnect(publish_endpoint); | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import org.zeromq.*; | |
| public class JavaSubscriber | |
| { | |
| public static void main(String[] args) | |
| { | |
| // | |
| // Belongs in initialization function | |
| // | |
| String endpoint = "tcp://*:5038"; | |
| String filterString = "Jetson message"; // Ignore any message not starting with this | |
| ZContext context = new ZContext(); | |
| ZMQ.Socket socket = context.createSocket(ZMQ.SUB); | |
| socket.subscribe(filterString.getBytes()); | |
| socket.connect(endpoint); | |
| System.out.println("Connected to " + endpoint); | |
| // | |
| // Belongs in periodic function | |
| // | |
| while(true) | |
| { | |
| // Block until a reply is received. | |
| String message = socket.recvStr(0); | |
| // Print the message. | |
| System.out.println("Received: \"" + message + "\""); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment