0

I am writing a robot simulation scenario. The robots a programmed in C and use calls to a special API.

My plan is to compile the robot programs to a DLL and at the start of the simulation I'm going to call that DLL's main function to get it started.

The issue I have is how the DLL is going to communicate back. I will give an example of this in pseudo-code

Say the robot program is simply "SetLeftWheelSpeed(100)" and then "Wait(1500)" to get it to move its left wheel and then wait for 1.5s.

When I call the the robot's program how would I get SetLeftWheelSpeed(int speed) to communicate back with my simulator? I think of DLL as libraries of tools that you call a method on and it gives you a return. I have to keep it running in a loop so I can't just use return values I don't think .

EDIT:

Some more thought has led me to a fundamental problem. In my given example I wouldn't be able to compile the robot code because it doesn't have definitions for things like moving the wheels. For this I would have to link it to the API which is in the simulator. So maybe I have to link the API to the robot code, link the robot code to the simulator, and somehow link simulator to the API so that it implements what it is supposed to do.

pseudoabdul
  • 616
  • 3
  • 12
  • @pseudoadbul: Lots of ways for a DLL to push data to C#, probably one of the most straightforward would be to write messages into a pipe that the C# code is reading using a stream. It all depends on whether the C# code is responsible for setting the values to return to the robot driver, or if it is just watching. – Ben Voigt Dec 12 '16 at 05:40
  • Thanks I will look into this. But yes, the communication will have to be both ways. – pseudoabdul Dec 12 '16 at 07:03
  • You know how to use c# delegates? When passed through p/invoke they become function pointers that allow the native code to make a callback into c# code. Be aware that the callback from native code is hidden from the garbage collector, so you'll have to keep the delegate from becoming unreachable. – Ben Voigt Dec 12 '16 at 07:12
  • I use delegates a lot but all this garbage collector stuff freaks me out. I might just look into having a data stream between the two. – pseudoabdul Dec 12 '16 at 09:21
  • OK new plan. You have 3 distinct parts: The Simulator (a windows executable), the robot code (A dll) and the API (also a dll) The robot code is linked to the API so it can call the API functions. The simulator also has a link to the API dll so it can add delegates to the API functions. Would this be a better strategy? – pseudoabdul Dec 12 '16 at 09:30

1 Answers1

0

Yes, It is possible.

Please go thru this tutorial by Code Project which helps you in this regards.

Also, I think it is a duplicate of How to call C++ DLL in C# or Is it possible to call a C function from C#.Net

Community
  • 1
  • 1
Prajwal
  • 3,930
  • 5
  • 24
  • 50
  • Thanks, but I have already read all of those links. The problem isn't calling a a function across a DLL; that much I have figured out. The problem is how to get the DLL to communicate back so it can call my API programs in the simulator. – pseudoabdul Dec 12 '16 at 05:27
  • You will have to do the same, but from C to C#. Check this question. http://stackoverflow.com/questions/4428267/calling-c-sharp-from-c – Prajwal Dec 12 '16 at 05:40