从C#发送OSC消息包并在Processing中接收获得了奇怪的地址模式和错误

时间:2013-06-16 20:23:32

标签: c# processing osc

我对编程比较陌生,所以如果问题很愚蠢,请原谅。我现在正在开展一个涉及Kinect的项目。我正在使用C#来提取实时联合信息(例如位置和方向),然后使用OSC消息-Udp protocal将数据发送到Processing。我从C#发送了OSC消息包,问题是我不知道如何在处理中将消息发送到我想要的内容。或者,我可能在C#中以错误的格式发送数据。如果有人能告诉我代码中可能出错的地方并导致错误,我将非常感激。

我使用以下代码从C#发送联合位置:

    if (joint0.JointType == JointType.ElbowRight)
    {
        // distance in meter
        String temp = "ElbowRight   " + joint0.Position.X * 1000 + "    " + joint0.Position.Y * 1000 + "    " + joint0.Position.Z * 1000;
        Console.WriteLine(temp);
        OscElement message = new OscElement("/joint/" + joint0.JointType.ToString(), joint0.Position.X * 1000, joint0.Position.Y * 1000, joint0.Position.Z, joint0.TrackingState.ToString());
        bundle.AddElement(message);
    }
    OscSender.Send(bundle);   // send message bundle

部分“/ joint /”是消息的地址模式。以下数据是消息的参数。根据{{​​3}},应在地址模式之后添加OSC类型标记字符串,该地址模式是以字符“,”(逗号)开头的OSC字符串,后跟与OSC序列完全对应的字符序列给定消息中的参数。但是,当我尝试此操作时,会导致格式异常并将错误报告为:无效字符(\ 44)。我所做的只是在OSC消息中加上“,s”:

OscElement message = new OscElement("/joint/" + ",s" + joint0.JointType.ToString(), joint0.Position.X * 1000, joint0.Position.Y * 1000, joint0.Position.Z, joint0.TrackingState.ToString());

我如何添加类型标签?这可能是造成以下错误的原因吗?

在我的处理代码中,我尝试使用以下代码获取关节位置值:

  if(theOscMessage.checkAddrPattern("/joint")==true) {
      String firstValue = theOscMessage.get(0).stringValue(); 
      float xosc = theOscMessage.get(1).floatValue(); // get the second osc argument
      float yosc = theOscMessage.get(2).floatValue(); // get the second osc argument
      float zosc = theOscMessage.get(3).floatValue(); // get the second osc argument
      String thirdValue = theOscMessage.get(4).stringValue(); // get the third osc argument
      println("### values: "+xosc+", "+xosc+", "+zosc);
      return;
  }

但是我收到了这个错误: [2013/6/16 20:20:53] ERROR @ UdpServer.run()ArrayIndexOutOfBoundsException:java.lang.ArrayIndexOutOfBoundsException

我使用Processing中给出的示例绑定接收消息,该示例显示按摩的地址模式和类型标记:

println("addrpattern\t"+theOscMessage.addrPattern());
println("typetag\t"+theOscMessage.typetag());

打印出来:

  

addrpattern pundle   typetag u ??? N?N?$ xlt ???

我不明白代码有什么问题。地址模式难道不是“联合”吗?或至少“捆绑”?什么是pundle ......

P.S。我在Win7 os计算机上使用Visual C#2010 Express和Processing 2.0b9 64bit。

非常感谢您的帮助!

更新 虽然我仍然无法弄清楚如何解决这个问题,但我找到了一种在Processing中接收消息的方法。我没有使用OSC包,而是发送具有不同地址模式的Osc消息。然后在draw方法中使用消息插件(例如oscP5.plug(this,”leftFoot”,”/joint/AnkleLeft”);)。然后创建一个名为leftFoot

的方法
public void leftFoot(float fx, float fy, float fz, String state) {
  println("Received: "+fx+", "+fy+", "+fz+", "+state);  
}

然后您可以看到正在打印的数据。附:在C#中,使用以下命令发送OSC消息:

OscElement message = new OscElement("/joint" + "/" + joint0.JointType.ToString(), joint0.Position.X * 1000, joint0.Position.Y * 1000, joint0.Position.Z, joint0.TrackingState.ToString());
                OscSender.Send(message);

2 个答案:

答案 0 :(得分:0)

嗯......确切地说不确定,但你可以使用OSCeleton。它带有一个处理示例 - 之前我已经使用过它,它运行正常。

(该示例还可以帮助您了解如何正确使用OSC地址模式...)

https://github.com/Sensebloom/OSCeleton

https://github.com/Sensebloom/OSCeleton-examples

答案 1 :(得分:0)

OSC要求填充到下一个32位边界,这意味着您需要添加零来填充消息,直到其长度为四个字节的偶数倍。

您需要对typetag执行相同的操作。即使您只发送一个typetag,也请使用零填充,因此,s00而非,s