'How to Send Messages over the Plaintext Protocol using Node.JS
I'm still a bit new to Sockets and NodeJS, I wanted to be able to communicate to a RoboMaster robot over the Plaintext protocol using NodeJS, not Python like what is explained in the documentation. I'm not sure how to do this using NodeJS and am a bit confused if my application socket is a client or server. I would preferably like to convert the example code in the docs to a NodeJS friendly version, but not sure how. I have looked into things like Socket.io, but I'm not sure if that is what I need to be using.
Any help would be appreciated.
Edit: I found this example, it looks quite similar to what I need but I am not sure.
Solution 1:[1]
It turns out that I can use the net module to communicate with the RoboMaster robot. Using the code from here this is what it looks like:
var net = require('net');
var HOST = '192.168.2.1';
var PORT = 40923;
var client = new net.Socket();
client.connect(PORT, HOST, function () {
console.log('CONNECTED TO: ' + HOST + ':' + PORT);
// Write a message to the socket as soon as the client is connected, the server will receive it as message from the client
client.write('command;');
});
// Add a 'data' event handler for the client socket
// data is what the server sent to this socket
client.on('data', function (data) {
console.log('DATA: ' + data);
// Close the client socket completely
client.destroy();
});
// Add a 'close' event handler for the client socket
client.on('close', function () {
console.log('Connection closed');
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | KeyMeerkat |
