Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by kspiel · Aug 17, 2012 at 03:54 PM · c#udpunsafe

Trouble creating a C# UDP interface to a C++ app in Unity

Okay... I do C/C++ in my sleep... C#, not so much. Still wrapping my head around that.

I have a fairly basic problem: I have a very large and complex application written in C++ that I need to exchange data with via UDP. The UDP part is easy. Where I'm getting my pants in a wad is mapping the C++ message structures into C# structures. I cannot modify the C++ structures as I have to emulate a piece of physical hardware within the Unity3D space (transparently to the C++ app)

So... for example, here's a simple C++ structure that matches the ICD for one of the pieces of hardware...

 struct RequestMessageType
 {
     //MESSAGE TYPE 2
     unsigned char  msg_type;
     char           port_a_output[8];
     char           port_b_output[8];
     char           port_d_output[8];
     unsigned short serialLength;
     char           serialData[256];
     unsigned short checksum;
 };

As I understand it, I could represent this in a C# structure as follows...

 protected unsafe struct RequestMessageType
 {
         public byte       messageType;
         public fixed byte portAOutput[8];
         public fixed byte portBOutput[8];
         public fixed byte portCEOutput[8];
         public ushort     serialLength;
         public fixed byte serialData[256];
         public ushort     checksum;
 };

The catch being that I cannot use 'unsafe' with Unity.

So... any chance one of the Unity/C# gurus can lend an old C++ programmer a hand in understanding how to create a C# structure to receive the message and how to map the raw byte[] returned from the UdpClient.Receive() call into that structure?

Or... is it as I fear, and I'm going to have to use brute force to manually stuff the data from the byte array into a C# structure?

TIA for any assists on this.

EDIT: I am can good speak engleesh

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · Aug 17, 2012 at 05:41 PM

Yes, you probably have to "deserialize" it manually. You can use a BinaryReader for example.

 using System.IO;
 
 // [...]
 
 byte[] data;
 RequestMessageType request;
 
 // [...]
 
 BinaryReader reader = new BinaryReader(new MemoryStream(data));
 request.messageType = reader.ReadByte();
 for (int i = 0; i < 8; i++)
     request.port_a_output[i] = reader.ReadByte();
 for (int i = 0; i < 8; i++)
     request.portBOutput[i] = reader.ReadByte();
 for (int i = 0; i < 8; i++)
     request.portCEOutput[i] = reader.ReadByte();
 request.serialLength = reader.ReadUInt16();
 for (int i = 0; i < 256; i++)
     request.serialData[i] = reader.ReadByte();
 request.checksum = reader.ReadUInt16();

I would write a constructor for the RequestMessageType struct that takes a byte array ;)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image scottdevine · Mar 05, 2019 at 06:10 PM 0
Share

Note that $$anonymous$$emoryStream assumes that all the bytes in the array are unsigned. This answer did not work with doubles for me, however, BitConverter did.

avatar image
0

Answer by kspiel · Aug 17, 2012 at 08:26 PM

Thank you Bunny83 --

You confirmed what I thought and this did get the trick done. Appreciate you lending a hand to a C# noob.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

C# code run in App Console but not on Unity console 1 Answer

UDPSend not working with iPad over LAN 0 Answers

Why breaks my Ethernet over USB (USB NCM) connection between HoloLens and PC? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges