export marker data through UDP ?
Quote from Oli on 23/11/2023, 11:47Hi all,
We are trying to read marker data (succeeded) and to send them through UDP to a Python server (not working).
Do you have script examples to send data through UDP to Python ? We saw your Jessica paper, but it doesn't give examples for UDP.
Our goal is to find the quickest solution, to give the highest flow for our data, for about 10-20 markers.
I thank you in advance...
Oli
Hi all,
We are trying to read marker data (succeeded) and to send them through UDP to a Python server (not working).
Do you have script examples to send data through UDP to Python ? We saw your Jessica paper, but it doesn't give examples for UDP.
Our goal is to find the quickest solution, to give the highest flow for our data, for about 10-20 markers.
I thank you in advance...
Oli
Quote from mm_dev on 23/11/2023, 16:14Well, the UDP interface isn't much different than the TCP interface:
async function sendDataToUdpServer(data) {
// create a UDP socket object
let socket = await UDP.bind("127.0.0.1", CLIENT_PORT);
await socket.connect(SERVER_IP, SERVER_PORT);
await socket.send(data);
await socket.shutdown();
}
On your python receiving side it is almost the same:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(("127.0.0.1", SERVER_PORT))
# not strictly necessary to set a timeout value, but without the receive call might block indefinitely when no data is received
s.settimeout(10)
data = s.recv(10000)
But keep in mind that the send() functions expect an ArrayBuffer object (TCP and UDP deal with bytes, not objects), so you first need to encode the return value from Mission.getMarkers() in some way, and then decode the received binary data accordingly in python. One option would be to use a DataView to write the number of markers and the frequency and value of each marker as binary data into the arraybuffer:
function encodeMarkers(marker)
{
// assuming "marker" is an array of objects with "frequency" and "value" properties
let buf = new ArrayBuffer(marker.length * 12 + 1)
let view = new DataView(buf);
// write number of markers as byte at the start of the packet
view.setUint8(0, marker.length);
for (let i = 0; i < marker.length; i++) {
// write frequency as 64 bit integer into the proper offset
view.setInt64(i * 12 + 1, marker[i].frequency);
// write value as 32 bit float into the proper offset
view.setFloat32(i * 12 + 9, marker[i].value);
}
return buf;
}
Decoding in python can then be done using the "struct" module. Or alternatively use JSON.stringify() and UTF8.encode() if you don't mind the overhead.
Please note that these code samples are just for demonstration, you need to adjust them to your specific use case.
Well, the UDP interface isn't much different than the TCP interface:
async function sendDataToUdpServer(data) {
// create a UDP socket object
let socket = await UDP.bind("127.0.0.1", CLIENT_PORT);
await socket.connect(SERVER_IP, SERVER_PORT);
await socket.send(data);
await socket.shutdown();
}
On your python receiving side it is almost the same:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(("127.0.0.1", SERVER_PORT))
# not strictly necessary to set a timeout value, but without the receive call might block indefinitely when no data is received
s.settimeout(10)
data = s.recv(10000)
But keep in mind that the send() functions expect an ArrayBuffer object (TCP and UDP deal with bytes, not objects), so you first need to encode the return value from Mission.getMarkers() in some way, and then decode the received binary data accordingly in python. One option would be to use a DataView to write the number of markers and the frequency and value of each marker as binary data into the arraybuffer:
function encodeMarkers(marker)
{
// assuming "marker" is an array of objects with "frequency" and "value" properties
let buf = new ArrayBuffer(marker.length * 12 + 1)
let view = new DataView(buf);
// write number of markers as byte at the start of the packet
view.setUint8(0, marker.length);
for (let i = 0; i < marker.length; i++) {
// write frequency as 64 bit integer into the proper offset
view.setInt64(i * 12 + 1, marker[i].frequency);
// write value as 32 bit float into the proper offset
view.setFloat32(i * 12 + 9, marker[i].value);
}
return buf;
}
Decoding in python can then be done using the "struct" module. Or alternatively use JSON.stringify() and UTF8.encode() if you don't mind the overhead.
Please note that these code samples are just for demonstration, you need to adjust them to your specific use case.
Quote from Oli on 23/11/2023, 17:48Hi ,
Thank you for the prompt response, we really appreciate it.
We tested your code, while adapting it to our use case, but we're still getting trouble receiving data from the script.. Here's how it boils down, from the js RTSA suite pro:
import { Mission } from "mission.js"
import { UDP } from "udp.js"const port = 5020;
const host = "127.0.0.1"console.log("Connecting to UDP socket");
async function sendDataToUdpServer(data) {
let socket = await UDP.bind("127.0.0.1", port);
await socket.connect(host, port);
await socket.send(data);
await socket.shutdown();
}[more code not relevant right now as we're just importing the markers but not handling them]
sendDataToUdpServer(UTF8.encode("test12345654654321564"));
On python side (same computer, with firewall disabled just in case), we made it very simple for now:
import socket
bufferSize = 1024
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(("127.0.0.1", 5020)data = s.recv(bufferSize)
print(data)It is to be noted the python script does receive data if we send it from another python instance... So we might be doing things wrong on the RTSA Suite side.
Hi ,
Thank you for the prompt response, we really appreciate it.
We tested your code, while adapting it to our use case, but we're still getting trouble receiving data from the script.. Here's how it boils down, from the js RTSA suite pro:
import { Mission } from "mission.js"
import { UDP } from "udp.js"const port = 5020;
const host = "127.0.0.1"console.log("Connecting to UDP socket");
async function sendDataToUdpServer(data) {
let socket = await UDP.bind("127.0.0.1", port);
await socket.connect(host, port);
await socket.send(data);
await socket.shutdown();
}[more code not relevant right now as we're just importing the markers but not handling them]
sendDataToUdpServer(UTF8.encode("test12345654654321564"));
On python side (same computer, with firewall disabled just in case), we made it very simple for now:
import socket
bufferSize = 1024
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(("127.0.0.1", 5020)data = s.recv(bufferSize)
print(data)
It is to be noted the python script does receive data if we send it from another python instance... So we might be doing things wrong on the RTSA Suite side.
Quote from mm_dev on 24/11/2023, 19:18Looks like you're trying to use the same port on the same system for sending and receiving, that can't work. When you're binding that port in your script block, you can't also use it in your python code as well at the same time.
So just make sure to use different numbers for CLIENT_PORT and SERVER_PORT and pay attention when to use which.
Looks like you're trying to use the same port on the same system for sending and receiving, that can't work. When you're binding that port in your script block, you can't also use it in your python code as well at the same time.
So just make sure to use different numbers for CLIENT_PORT and SERVER_PORT and pay attention when to use which.
Quote from ManoDaSilva on 12/12/2023, 19:40Hi!
I can confirm the code snippet works, thanks! Just had indeed to specify a different port as a client, and it all works fine now.
Hi!
I can confirm the code snippet works, thanks! Just had indeed to specify a different port as a client, and it all works fine now.