How can I use code between C and C # most effectively (both Mono and Silverlight)

Our desktop application consists of the back of Mono / .NET 3.5, which communicates via USB with various devices and the Silverlight interface, which communicates with the back through sockets. The firmware for the devices was developed internally by C. To speed up the development process and reduce the number of errors, we would like to share the code between our firmware and the desktop application. What tools and methods could you offer us to do this? Better yet, what have you successfully used in your software to solve a similar problem?

The two main things we would like to separate are the message structures that define our communication protocol and the data that are currently defined using the constants of the C / array structure. For protocol messages, we are currently manually rewriting our classes that implement messages according to C definitions, using the C code as a guide. For the data that we share, we created a C ++ managed application that references compiled C code, then extracts the contents of the arrays into an XML file.

Our methods work, but they are less than optimal. Firstly, we had a lot of errors related to our interpretation of C structures as C #, due to changes in C code in parallel and programmer errors; we would like to avoid this class of errors in future development. For data exchange, I don’t have a big problem with our current solution, but the developer of the extraction program says that this is a painful process that allows you to work correctly.

We are a bit limited by what we can change on the firmware for devices. Firstly, we have a large number of processor architectures and embedded platforms, so C code should remain portable. On the other hand, the firmware launches real-time software and is limited by the available MIPS and storage space, so we cannot add anything with unpredictable or slow runtimes.

+4
source share
2 answers

Try protocol buffers, which are a binary format for a programming language β€” an agnostic format that Google uses as a format for exchanging data between its services.

The idea is that you write a .proto file that describes the structure of your data and runs a protocol buffer compiler that generates serialization / deserialization code for your language. This would be more efficient than coding in XML, and save time writing manual serializers / deserializers and eliminate errors due to incorrect implementation (since they are automatically generated from a high-level description in the case of protocol buffers).

The Google implementation supports C ++, Java, and Python, and there is an independent implementation for other languages, for example. for C # there is this and this .

There are other technologies, for example. Facebook Thrift.

+2
source

I would consider using the XSLT transform to generate code. Have XML that defines protocol structures and has various XSLTs that generate C # and various platform C codes. Then use the generated code when creating applications.

XSLT transformation can be included in the project assembly, I used this method for several projects, as described in Using XSLT to generate performance counter code (although this message is not related to comm protocols, I actually used the same technique in communication protocols as as conductor, and between modules). Once you overcome the difficulties of speeding up writing XSL and XQuery, you will become very productive and you will realize how easily and quickly you can change the communication protocol.

+2
source

All Articles