CNMAT / OSC

OSC: Arduino and Teensy implementation of OSC encoding
cnmat.berkeley.edu/oscuino
Other
727 stars 135 forks source link

It is hoped that a public function can be added to OSCMessage to obtain the binary array of OSCMessage, so that OSC messages can be sent in more ways. #142

Open ChinaBurn opened 8 months ago

ChinaBurn commented 8 months ago

It is hoped that a public function can be added to OSCMessage to obtain the binary array of OSCMessage, so that OSC messages can be sent in more ways. Especially on other MCUs, such as esp32, stm32. Below is a sample code snippet of mine on eps32, which can accurately receive osc messages, but the sent osc messages are subpackaged by UDP, resulting in incomplete data.

The hardware is esp32+Lan8720 sending and receiving through Ethernet, using "AsyncUDP":

void InitAsyncUDP() {
  if (AsyncUdp.listen(UDPLocalPort)) {
    Serial.printf("AsyncUDP Listening on Port: %d\n", UDPLocalPort);
    AsyncUdp.onPacket(UDPReceiver);
  }
}

void UDPReceiver(AsyncUDPPacket packet) {
  OSCBundle bundleIN;
  bundleIN.fill(packet.data(), packet.length());
  if (!bundleIN.hasError()) {
    bundleIN.route("/TableLight", routeTone);
  } else {
    Serial.printf("OSCReceiveError\n");
  }
  // Serial.printf("Received Udp Packet Lenght:%d\n", packet.length());
}

void Task_SendOSC() {
  int AccelerationOSC = round(Acceleration * 100.00);
  OSCMessage msg("/BianMaQi");
  msg.add(AccelerationOSC);
  AsyncUdp.connect(RemooteOSCIP, RemooteOSCPort);
  msg.send(AsyncUdp);
  AsyncUdp.close();
  msg.empty();
}

In the above code, receiving osc messages works fine, but the sent osc messages are divided into multiple udp data packets, but the data length is correct. Because of this, when another device receives the osc message, a parsing error will occur.

In esp32, AsyncUdp has methods for sending data, such as:

size_t writeTo(const uint8_t *data, size_t len, const IPAddress addr, uint16_t port, tcpip_adapter_if_t tcpip_if=TCPIP_ADAPTER_IF_MAX);

If I can obtain the binary data of this message after constructing the OSC message, I can send it out in this way.