Marshaling C-Struct with doubles to C# - positive Values wrong

Viewed 207

First: I´m fresh to programming, so i´m sorry if this question is too dumb. But i´ve searched an solution for 5 days and now im lost and hopeless :/

I need to control a CNC-machine via a C#-program. The API for the CNC-machine is a *.dll written in C, so I need to marshal all the different datatypes. Nearly everything works well, but there is a strange problem. Only negative doubles work well. Positive ones are just wrong. If I define y=1, the X-Axis moves to it´s border and collides. The border is at y>500

The coordinates of the different axis are defined as a struct in the dll.

header:

typedef struct _CNC_CART_DOUBLE
{
    double x,y,z,a,b,c;
} CNC_CART_DOUBLE;

c-function:

/*
* Name   : CncMoveTo
* In     : pos[i] position to move to for axis i,
*          move[i], if 1 axis i will move, if false no move.
*          
* Out    : -
* Return : See CNC_RC values
* Descr  : Move joints/motors to given position
*/
 CNC_RC EXP2DF __stdcall CncMoveTo(CNC_CART_DOUBLE pos, CNC_CART_BOOL, double velocityFactor);

so I created a struct in my C#-Script for marschaling:

[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct CNC_CART_DOUBLE
{
    public double x;
    public double y;
    public double z;
    public double a;
    public double b;
    public double c;
}

the dll-Import:

[DllImport("cncapi.dll", CallingConvention = CallingConvention.StdCall)])
public static extern void CncMoveTo(CNC_Vars.CNC_CART_DOUBLE pos, CNC_Vars.CNC_CART_BOOL achse, double velocityFactor);

and the test:

private void btn_MoveTo100_Click(object sender, RoutedEventArgs e)
    {
        CNC_Vars.CNC_CART_BOOL _achsenAn = new CNC_Vars.CNC_CART_BOOL();
        _achsenAn.x = 1;
        _achsenAn.y = 1;
        _achsenAn.z = 1;
        _achsenAn.a = 0;
        _achsenAn.b = 0;
        _achsenAn.c = 0;

        CNC_Vars.CNC_CART_DOUBLE _koords = new CNC_Vars.CNC_CART_DOUBLE(); // Koordinatenachsen beachten--> X=negativ, Y=Positiv, Z=Negativ
        _koords.x = -100.0;
        _koords.y = 1.0;
        _koords.z = -100.0;
        _koords.a = 0;
        _koords.b = 0;
        _koords.c = 0;

        double velocityfactor = 0.5;

        CNC_Methods.CncMoveTo(_koords, _achsenAn, velocityfactor);
    }

I´ve tried to define the doubles as 64bit-floating number(through MarshalAs...something R8) but the result was the same. The different CallingConventions changed nothing too. The debug shows the right values in the c#-struct so the problem is somewhere in the marshaling process.

Can someone help me with this?

Thank you, Riggo

1 Answers
Related