From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

Automotive and Embedded Networks

cancel
Showing results for 
Search instead for 
Did you mean: 

.Net wrapper for XNET API.

Dear Sir,

 

I try to program   XNET (CAN & LIN) with C#.

 

I found the example for the ANSI-C.

I try to conver this example to C#, but it's very difficult to me.

Some body already have .Net Wrapper for XNET API? and can share ?

 

Thanks,

 

Hiawath

0 Kudos
Message 1 of 6
(6,792 Views)

Hi Hiawath,

 

There's currently no support for our customers willing to use XNET with a .NET programming language. As per the example in C and the use of P/Invoke, you can create a C# wrapper for your needs, yet we cannot really assist you in doing this.

 

A useful document to get started:

- http://www.codeproject.com/Articles/403285/P-Invoke-Tutorial-Basics-Part

 

You can also share you experience with your local sales representative in case he can figure out a better way out for you.

--Eric

Eric M. - Senior Software Engineer
Certified LabVIEW Architect - Certified LabVIEW Embedded Systems Developer - Certified LabWindows™/CVI Developer
Neosoft Technologies inc.

0 Kudos
Message 2 of 6
(6,751 Views)

Dear Sir,

 

 

I try to wrapper for the "nixnet.dll" and worked very well for the "nxCreateSession" and "nxWriteSignalSinglePoint".

But I can't get the data from " nxReadSignalSinglePoint" , even though compile OK,

Can you see  "nxReadSignalSinglePoint" and advice ?

 

Thanks,

 

Andrew.

 

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Threading;

namespace ConsoleApplication2
{
    class Program
    {
        public const string DLL_NAME = "nixnet.dll";

        static IntPtr g_SessionRef;
        static IntPtr g_SessionRef2;

        [DllImport(DLL_NAME, EntryPoint = "nxCreateSession", CallingConvention = CallingConvention.Cdecl)]
        public static extern Int32 CreateSession(string DatabaseName, string ClusterName, string List, string Interface,
            UInt32 Mode, out IntPtr SessionRef);
        //nxStatus_t _NXFUNC nxCreateSession(
        //                   const char* DatabaseName,
        //                   const char* ClusterName,
        //                   const char* List,
        //                   const char* Interface,
        //                   u32 Mode,
        //                   nxSessionRef_t* SessionRef);

        [DllImport(DLL_NAME, EntryPoint = "nxReadSignalSinglePoint", CallingConvention = CallingConvention.Cdecl)]
        public static extern Int32 ReadSignalSinglePoint(IntPtr SessionRef, out IntPtr ReadValueBuffer, 
            UInt32 SizeOfValueBuffer, out IntPtr TimestampBuffer, UInt32 SizeOfTimestampBuffer);


        //nxStatus_t _NXFUNC nxReadSignalSinglePoint (
        //                     nxSessionRef_t SessionRef,
        //                     f64* ValueBuffer,
        //                     u32 SizeOfValueBuffer,
        //                     nxTimestamp_t* TimestampBuffer,
        //                     u32 SizeOfTimestampBuffer);

        [DllImport(DLL_NAME, EntryPoint = "nxWriteSignalSinglePoint", CallingConvention = CallingConvention.Cdecl)]
        public static extern Int32 WriteSignalSinglePoint(IntPtr SessionRef, double[] ValueBuffer, UInt32 SizeOfValueBuffer);

        //nxStatus_t _NXFUNC nxWriteSignalSinglePoint(
        //                           nxSessionRef_t SessionRef,
        //                           f64* ValueBuffer,
        //                           u32 SizeOfValueBuffer);


        static void Main(string[] args)
        {
            string l_pSelectedInterface = "LIN2";
            string l_pSignalArray = "";
            string l_pSignalArray2 = "";
            string l_pSelectedDatabase = "IG";
            string l_pSelectedCluster = "Cluster";
            string l_pSelectedSignal1 = "LH_HeaterSWLEDHigh";
            string l_pSelectedSignal2 = "LH_HeaterSWLEDMid";
            string l_pSelectedSignal5 = "LH_HeaterSWLEDLow";

            string l_pSelectedSignal3 = "LH_HeaterSW";

            int g_Status = 0;

            double[] ValueBuffer = new double[2];

            IntPtr ReadValueBuffer = IntPtr.Zero;
            IntPtr TimestampBuffer = IntPtr.Zero;

            /// for read from LIN
            l_pSignalArray = l_pSelectedSignal1;
            l_pSignalArray += ",";
            l_pSignalArray += l_pSelectedSignal2;
            l_pSignalArray += ",";
            l_pSignalArray += l_pSelectedSignal5;
            
            // for writing to LIN
            l_pSignalArray2 = l_pSelectedSignal3;
            l_pSignalArray2 += ",";
            l_pSignalArray2 += l_pSelectedSignal5;

            /// for read from LIN
            g_Status = CreateSession(l_pSelectedDatabase, l_pSelectedCluster, l_pSignalArray, 
                l_pSelectedInterface, 0, out g_SessionRef);
            // for writing to LIN
            g_Status = CreateSession(l_pSelectedDatabase, l_pSelectedCluster, l_pSignalArray2,
                l_pSelectedInterface, 3, out g_SessionRef2);

            //
            ValueBuffer[0] = 0x01;
            ValueBuffer[1] = 0x00;

            // write 
            g_Status = WriteSignalSinglePoint(g_SessionRef2, ValueBuffer, 16);


            /////////////////////  read
            g_Status = ReadSignalSinglePoint(g_SessionRef, out ReadValueBuffer, 24, out TimestampBuffer, 24);

            return;
        }
    }
}

 

Message 3 of 6
(6,568 Views)

I don't know .NET, but looking at your code, you are marking "ReadValueBuffer" as an 'IntPtr'. This is actually a double-precision floating point value.

 

[DllImport(DLL_NAME, EntryPoint = "nxReadSignalSinglePoint", CallingConvention = CallingConvention.Cdecl)]
        public static extern Int32 ReadSignalSinglePoint(IntPtr SessionRef, out IntPtr ReadValueBuffer, 
            UInt32 SizeOfValueBuffer, out IntPtr TimestampBuffer, UInt32 SizeOfTimestampBuffer);

 I don't know if this helps.

Message 4 of 6
(6,556 Views)

hummm,, its 2016 and no support for .NET framework? I wonder if NI is pushing customers to competitors for in-vehicle CAN (ValueCAN, Vector CANTech, etc) for not being able to interface to new development environment. Afterall, Measurment Sudio uses WPF.  If I have the hardware; why do I need to shling another $1k for development environement that I can get for free from Microsoft. Its not rocket science; only CAN messages.

Message 5 of 6
(5,530 Views)

Any luck? I am also looking for C# code for LIN2.0.

0 Kudos
Message 6 of 6
(4,617 Views)