I’m working on a project where I am using TCP to communicate between a Visual Studio C#.NET program running on a computer and a Sketch/C++ program running on an microcontroller. I’ve set up a protocol using structs. These only contain normal variable types that both environments recognize. At the moment, it works totally fine where I can say… fill out a struct on the desktop and it comes out into the Sketch. Here is an example of one message on both.
#pragma once
// CommonComms.h on the ESP8266.
struct VelocityChange
{
const static char ID = 'V';
float LeftVelocity;
float RightVelocity;
};
using System;
// CommonComms.cs on the Desktop
namespace Inq.Utils
{
public struct VelocityChange
{
const char ID = 'V';
float LeftVelocity;
float RightVelocity;
};
}
I would like to make one file that both projects can use. Just so that as messages are added, I only put them in one place, and re-compile both sides. I can get around issues of syntax… like hiding the using System; when used on the ESP8266. What I’m having troubles with is… I can’t include a *.h file in the C#.NET program and I can’t include a *.cs file in the Arduino IDE. I’ve done some searches on the Internet, but haven’t found anything useful. Every hit I get is about passing managed to/from un-managed, marshalling, etc… while on the same computer. My problem is simply being able to include the same file in both projects.
Does anyone have any ideas how I might do this?
Thanks.
Visual Studio seems to require a *.cs extension to even recognize the code. The Arduino IDE compiler (gcc in this case) will not recognize #include "CommonComss.cs". I simply get compile time errors. On Arduino IDE, it simply doesn’t see the named file in the same folder. On VS, it doesn’t acknowledge the file and syntax unless it has a cs file extension.
2
Answers
Actually the solution was given to me from another forum. The main problem is I assumed when the Arduino IDE could not find the CommonComms.cs file, it was because of the cs extension. It turns out, if the absolute path is used, it can find and use the included CS file. Here is the same example from above with the modifications that permit its use in both the Arduino C++ project and C#.NET project. It also shows the macros needed to use the C# syntax in a C++ project.
Here is the CommonComms.cs file
MyArdinoSketch.ino modifications
The two files you posted have different contents (c++ has different syntax than c# for the similar concepts) so there’s no way to reuse one of them for both compilers.
If you want to reuse the definitions then I think you need find a way to automate generating the source files (e.g., by reading a json file and writing the .h and .cs files)