skip to Main Content

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


  1. Chosen as BEST ANSWER

    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

    #if ARDUINO
      #pragma once
      #define public
    #endif
    
    namespace InqSim
    {
        struct VelocityChange
        {
            public char ID;
            public float LeftVelocity;
            public float RightVelocity;
        };
    }
    
    #if ARDUINO
      #undef public
    #endif
    

    MyArdinoSketch.ino modifications

    // The following include is shared in both the Arduino Sketch
    // and the VS C#.NET project.  The Arduino IDE must use 
    // an absolute path when the extension is "cs".   Also, must
    // include the using namespace statement for visibility.
    
    #include "X:InqEggCommonComms.cs"
    using namespace InqSim;
    

  2. 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)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search