CANhack.de CAN-Interface RKS+CAN
CAN Hardware, CAN Software, CAN Protocols - CAN Bus Forum for Your CAN Bus Project.

C# Example: A Simple Tutorial

 
New Topic Reply 🔗 🖨 CANhack.de - Index » CANhack.de CAN-USB System: RKS+CAN

Wie wichtig ist euch ein C# API (Programmierschnittstelle) für das CAN-Interface?
Unwichtig, ich programmiere in C oder C++
0%
 0%  [ 0 ]
Ich finde C# super und will so etwas!
100%
 100%  [ 2 ]
Unwichtig, ich nutze nur KCANMonitor.
0%
 0%  [ 0 ]
Total votes : 2

Author Message
CAN-Diagnose
Administrator
Administrator
Avatar-CAN-Diagnose

Joined: 06/07/2011
Posts: 573
Karma: +29 / -0   Thank you, like it!
Location: Ländle



Post05-04-2017, 17:55    Subject: C# Example: A Simple Tutorial Quote

Not fully finished yet, but the basic idea is to use a C++ DLL to interface with the RKS+ CAN interface.
Of course, the code could be written more elegantly, and it only displays the serial number and version information from the interface.

If anyone is a C# expert and can provide feedback on my work, please let me know. It would be beneficial to create a separate C# file for including the code, which encapsulates the DLL API. This isn't overly complicated.

Best regards, Rainer.



Example-Net.rar
 Description:
 Beispiel, verbesserte Versionen siehe im Laufe des Themas.
Beispiel, verbesserte Versionen siehe im Laufe des Themas.
Download
 File name:  Example-Net.rar
 File size:  500.96 KB
 Downloaded:  1136 times
Dipl.-Ing. (FH) Rainer Kaufmann - Embedded Software Freelancer
System RKS+CAN: CANHack.de CAN-Bus Interface


Translated on 07-08-2026, 1:41.
Back to top Profile PM WWW
dunt
Hacker
Hacker


Joined: 11/06/2015
Posts: 11
Karma: +0 / -0   Thank you, like it!


Premium Support

Post11-06-2017, 12:29    Subject: C# Example: A Simple Tutorial Quote

Has anyone had any progress on this yet? I expanded the project to include Ub, and that seems to be working so far.

I'm now stuck with receiving the CAN messages.

Code:
if(RKSCANRx(&sRx))
- exactly here, how do I need to import this from the DLL?

The following error is displayed: "Additional information: "parameter #1" cannot be marshaled: Pointers are not allowed to point to marshaled structures. Use ByRef instead."


possibly, someone can help me icon_wink.gif



2017-06-06_RKS CAN USB.rar
 Description:
 C# Example: A Simple Tutorial
C# Example: A Simple Tutorial
Download
 File name:  2017-06-06_RKS CAN USB.rar
 File size:  988.74 KB
 Downloaded:  1408 times


Translated on 07-08-2026, 1:43.
Back to top Profile PM
CAN-Diagnose
Administrator
Administrator
Avatar-CAN-Diagnose

Joined: 06/07/2011
Posts: 573
Karma: +29 / -0   Thank you, like it!
Location: Ländle



Post16-06-2017, 13:37    Subject: C# Example: A Simple Tutorial Quote

Ugh... reluctantly becoming a C# hacker. Life could be so simple, I'm working on it, and soon I'll be ready for another vacation.

Here's how it works:
http://andyrushton.co.uk/csharp-calling-c-functions-marshalling-structs/
https://docs.microsoft.com/en-us/dotnet/framework/interop/passing-structures

Instead of wrapping each individual function call of the C/C++ DLL in a complex way, it would probably be more efficient to extend the C# RKS-USB class with wrapper functions that can be called instead. This way, the effort is only required once.

Best regards, Rainer.
Dipl.-Ing. (FH) Rainer Kaufmann - Embedded Software Freelancer
System RKS+CAN: CANHack.de CAN-Bus Interface


Translated on 07-08-2026, 1:44.
Back to top Profile PM WWW
CAN-Diagnose
Administrator
Administrator
Avatar-CAN-Diagnose

Joined: 06/07/2011
Posts: 573
Karma: +29 / -0   Thank you, like it!
Location: Ländle



Post10-01-2018, 20:58    Subject: C# Example: A Simple Tutorial Translating...

[Translating...]

Code:
using System.Runtime.InteropServices;

// CAN frame types

// CAN data structure
public class can_data_t
{
  public uint dwID;
  public byte byDLC;
  public byte[] abyData = new byte[8]; // max 8 data bytes
}

// CAN error information structure
public class can_err_t
{
  public byte byError;
}

// CAN information structure
//C++ TO C# CONVERTER TODO TASK: Unions are not supported in C#, but the following union can be replicated with the StructLayout and FieldOffset attributes:
//ORIGINAL LINE: union can_union_t
[StructLayout(LayoutKind.Explicit)]
public struct can_union_t
{
  [FieldOffset(0)]
  public can_data_t sData;
  [FieldOffset(0)]
  public can_err_t sErr;
}

// The final CAN frame structure used for send/receive
public class can_msg_t
{
  public byte byType;
  public uint dwTimeStamp;
  public can_union_t uFrm = new can_union_t();
}


internal static class DefineConstants
{
   public const int FRAME_TYPE_NORMAL = 0x1;
   public const int FRAME_TYPE_RTR = 0x2;
   public const int FRAME_TYPE_NORMAL_EXT = 0x3;
   public const int FRAME_TYPE_RTR_EXT = 0x4;
   public const int FRAME_TYPE_ERR = 0x5;
}
Dipl.-Ing. (FH) Rainer Kaufmann - Embedded Software Freelancer
System RKS+CAN: CANHack.de CAN-Bus Interface
Back to top Profile PM WWW
CAN-Diagnose
Administrator
Administrator
Avatar-CAN-Diagnose

Joined: 06/07/2011
Posts: 573
Karma: +29 / -0   Thank you, like it!
Location: Ländle



Post27-04-2018, 13:15    Subject: C# Example: A Simple Tutorial Quote

C# wrapper by Alexander Schmidt on GitHub:
https://www.canhack.de/en/viewtopic.php?t=2400
Dipl.-Ing. (FH) Rainer Kaufmann - Embedded Software Freelancer
System RKS+CAN: CANHack.de CAN-Bus Interface


Translated on 07-08-2026, 1:45.
Back to top Profile PM WWW

Ratings - C# Example: A Simple Tutorial

Average rating: 0.00 - worst rating: 0 - best rating: 0 - number of ratings: 0 - View ratings

You are not authorized to rate this topic. Danke sagen
New Topic Reply 🔗 🖨 CANhack.de - Index » CANhack.de CAN-USB System: RKS+CAN
Jump to:  
You cannot post new topics in this forum.