Recent

Author Topic: TCanSocket – SocketCAN communication on Linux  (Read 866 times)

pragmalin

  • New member
  • *
  • Posts: 7
TCanSocket – SocketCAN communication on Linux
« on: April 06, 2022, 01:53:54 pm »
A little while ago, I was working on a Linux application with which I wanted to interact with messages on a Controller Area Network (CAN) bus. I built a Lazarus component for this, called TCanSocket.

It’s a small niche and probably not a lot of users would need this. Nevertheless, I figured I might as well release it to the public and let it be useful for someone else.

You can find the component in this GitHub repository:
The online user manual is available here:
Have fun with it!

Frank

sstvmaster

  • Sr. Member
  • ****
  • Posts: 299
Re: TCanSocket – SocketCAN communication on Linux
« Reply #1 on: April 06, 2022, 03:16:12 pm »
Hi Frank,

thanks for your lib. User avra made also something for CAN, see: https://forum.lazarus.freepascal.org/index.php/topic,39858.msg420245.html#msg420245
and look in the attachment.
greetings Maik

Windows 10,
- Lazarus 2.2.6 (stable) + fpc 3.2.2 (stable)
- Lazarus 2.2.7 (fixes) + fpc 3.3.1 (main/trunk)

mas steindorff

  • Hero Member
  • *****
  • Posts: 532
Re: TCanSocket – SocketCAN communication on Linux
« Reply #2 on: April 06, 2022, 08:26:33 pm »
hi sstvmaster and Frank,
which CAN protocol does your code follow?  CanOpen, Can Kingdom, Can DeviceNet, ... ?
With the CAN bus being as old as it is, I suspect there are several objects around.  I do not work with it anymore but someone who does should add a wiki page pointing to these libraries. 
Mas
windows 10 &11, Ubuntu 21+ - fpc 3.0.4, IDE 2.0 general releases

pragmalin

  • New member
  • *
  • Posts: 7
Re: TCanSocket – SocketCAN communication on Linux
« Reply #3 on: April 06, 2022, 11:01:16 pm »
Hi Frank,

thanks for your lib. User avra made also something for CAN, see: https://forum.lazarus.freepascal.org/index.php/topic,39858.msg420245.html#msg420245
and look in the attachment.

Yeah, avra did a great job bringing the entire SocketCAN API into Pascal. It enables you to do everything you can do in C/C++ with SocketCAN in Pascal.

I took a slightly different approach. The SocketCAN functionality offered by the Linux kernel can be a bit overwhelming, if you've not worked with it before. And in most cases all you really want to do is just (1) connect to the CAN bus and (2) transmit and receive CAN messages. With this in mind, I put together a shared library that enables you to do exactly that:
As a long term fan of Delphi and Lazarus, I wanted to use this cancomm shared library in Pascal. So I created a wrapper for it and built the TCanSocket component on top of it. This makes accessing the CAN bus from Linux a piece of cake. Just drop a TCanSocket on your form, set the name of your CAN adapter and call the Connect method. Once connected, you simply call the Transmit method for send a CAN message and the OnReceived event tells you when a new CAN message was received.

pragmalin

  • New member
  • *
  • Posts: 7
Re: TCanSocket – SocketCAN communication on Linux
« Reply #4 on: April 06, 2022, 11:11:07 pm »
hi sstvmaster and Frank,
which CAN protocol does your code follow?  CanOpen, Can Kingdom, Can DeviceNet, ... ?
With the CAN bus being as old as it is, I suspect there are several objects around.  I do not work with it anymore but someone who does should add a wiki page pointing to these libraries. 
Mas

Hi Mas,

The TCanSocket component handles the basic CAN message transmission and reception and does not assume any protocol. You can use it to build another protocol on top of it. For example J1939, CANopen, OSEK-TP, etc. That's actually a pretty nice idea for follow-up components. Something along the lines of TCanOpen, TCanTp, TCanJ1939.

I agree that a dedicated SocketCAN page on the Wiki would be great to collect the information about accessing a CAN bus from Lazarus. Especially with the ever growing popularity of embedded Linux devices such as the Raspberry PI. I'll even volunteer to do so, when I have some spare time.

 

TinyPortal © 2005-2018