Recent

Author Topic: CAN-BUS SocketCAN  (Read 26277 times)

avra

  • Hero Member
  • *****
  • Posts: 2131
    • Additional info
Re: CAN-BUS SocketCAN
« Reply #60 on: May 16, 2021, 02:35:31 am »
is there already a way to perform the actions which the 'ip' command does in pascal?

like "sudo ip link set can0 type can bitrate 125000 sample-point 0.875"
and "sudo ip link set can0 up"
and "sudo ip link set can0 down"?
Calling ip command from your pascal application and parsing the output would be the simplest solution. If you are not satisfied with that, you would have to use NETLINK sockets, since that is what ip command uses. Alternatively, you could use libsocketcan (a wrapper around netlink sockets for CAN) or libnl, but you would need to make your own wrapper.

https://lalten.github.io/libsocketcan/Documentation/html/group__extern.html#gaa01e52bcb6f4b873da7ce32cab38332d
https://github.com/jmore-reachtech/can-utils/blob/master/src/canconfig.c
https://www.infradead.org/~tgr/libnl/doc/api/group__can.html#gab6de57cdb6c42d48df432636e266700b
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

ThomasK

  • New Member
  • *
  • Posts: 34
Re: CAN-BUS SocketCAN
« Reply #61 on: May 16, 2021, 10:45:20 am »
Thanks.

Well, it seems that due to the 'C' in CAN most code is written in C >:(
Started Pascal on a Siemens 4004/151 in 1977. TurboPascal 1.0 in 1984 on PC-Dos.

ThomasK

  • New Member
  • *
  • Posts: 34
Re: CAN-BUS SocketCAN wrapper for libsocketcan
« Reply #62 on: May 16, 2021, 03:03:41 pm »
Hi,

I installed the library and am trying to call it.
Code: Pascal  [Select][+][-]
  1. unit LibSockCAN;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. //lib/arm-linux-gnueabihf/libsocketcan.so
  8. //libsocketcan.h
  9.  
  10. {$linklib c}
  11. {$linklib libsocketcan}
  12.  
  13. //function can_do_restart                   : longint; cdecl; external;
  14. function can_do_stop(const pchar)           : longint; cdecl; external;
  15. function can_do_start(const pchar)          : longint; cdecl; external;
  16. //function can_set_bitrate                  : longint; cdecl; external;
  17. //function can_set_bitrate_samplepoint      : longint; cdecl; external;
  18.  
  19. //uses
  20.  
  21. implementation
  22.  
  23.  
  24. end.
  25.  

The call is done as reaction to a Button:

Code: Pascal  [Select][+][-]
  1.  
  2. procedure TForm1.B_StartCan0Click(Sender: TObject);
  3. var
  4.   ifr_name : PChar;
  5.   myresult : integer;
  6. begin
  7.   ifr_name:= 'can0';
  8.   myresult:=can_do_start(ifr_name);
  9.   Label_StartResult.caption:=InttoStr(myresult);
  10. end;

No errors during compilation, but return code is -1.
Where is my error?

btw. the CAN-UTILS and the ip commands work
« Last Edit: May 16, 2021, 03:18:44 pm by ThomasK »
Started Pascal on a Siemens 4004/151 in 1977. TurboPascal 1.0 in 1984 on PC-Dos.

avra

  • Hero Member
  • *****
  • Posts: 2131
    • Additional info
Re: CAN-BUS SocketCAN
« Reply #63 on: May 16, 2021, 03:32:39 pm »
Thanks.
You're welcome.  :)

it seems that due to the 'C' in CAN most code is written in C >:(
Well, Linux kernel 2.6.25 with first SocketCAN implementation was donated by Volkswagen in 2008, so besides much wider user base C also has about 13 years advantage - although we already have some unique and pretty useful error dump and error simulation tools. With FreePascal wrapper all SocketCAN code can be easily transferred with almost 1:1 translation. Just take a look at demos which do not start with hl prefix in their name. They are in Pascal but they look almost identical to their C roots. If you take a look at canconfig.c that I mentioned in previous post, you will see that besides SocketCAN (that is already part of the kernel) only libsocketcan is used additionally. So, if you create a libsocketcan wrapper then it should not be too hard to do an almost 1:1 translation, or extract just parts which you need for CAN setup in code. If that is too much, then you can always run ip command in a process and parse the output.

According to my free time, plan is to support J1939 (trucks, buses, heavy machinery, marine, military and agriculture), OBD-II (cars) and DBC file parsing (for automatic conversion of CAN messages to human readable form). If in the mean time I get some CANopen (plc, industrial automation, embedded) freelance work then I might consider supporting CANopen as well. For now, there is no plan to support libsocketcan.

UPDATE:

No errors during compilation, but return code is -1.
Where is my error?
I am not familiar with internals of libsocketcan so I can not tell. If I had to guess then I would first try to replace
Code: Pascal  [Select][+][-]
  1. ifr_name:= 'can0';
with
Code: Pascal  [Select][+][-]
  1. ifr_name:= 'can0' + #0;
If that doesn't work, then I would try to create the most simplest code in C which does the job, and then try to convert that to FreePascal with already mentioned 1:1 conversion method. That would eliminate a lot of unknowns of what works and what doesn't.

Ah, yes... You should also try to start your app as root, and check if can0 is not already started.
« Last Edit: May 16, 2021, 04:03:02 pm by avra »
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

ThomasK

  • New Member
  • *
  • Posts: 34
Re: CAN-BUS SocketCAN
« Reply #64 on: May 16, 2021, 04:25:03 pm »
Adding the #0 to the devince name changed nothing.

Started Pascal on a Siemens 4004/151 in 1977. TurboPascal 1.0 in 1984 on PC-Dos.

dseligo

  • Sr. Member
  • ****
  • Posts: 277
Re: CAN-BUS SocketCAN
« Reply #65 on: May 16, 2021, 11:40:48 pm »
According to my free time, plan is to support J1939 (trucks, buses, heavy machinery, marine, military and agriculture), OBD-II (cars) and DBC file parsing (for automatic conversion of CAN messages to human readable form). If in the mean time I get some CANopen (plc, industrial automation, embedded) freelance work then I might consider supporting CANopen as well. For now, there is no plan to support libsocketcan.

What device(s) do you use to interface with CAN bus?

avra

  • Hero Member
  • *****
  • Posts: 2131
    • Additional info
Re: CAN-BUS SocketCAN
« Reply #66 on: May 17, 2021, 12:44:51 am »
What device(s) do you use to interface with CAN bus?
Pi, PocketBeagle and Arduino (AVR, STM32, ESP32)
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

dseligo

  • Sr. Member
  • ****
  • Posts: 277
Re: CAN-BUS SocketCAN
« Reply #67 on: May 17, 2021, 02:54:14 pm »
What device(s) do you use to interface with CAN bus?
Pi, PocketBeagle and Arduino (AVR, STM32, ESP32)

Does Arduino (AVR) has to be some specific type (e.g. AT90CAN128) or any will do?
What do you use between 'controller' and CAN bus?

Blade

  • Full Member
  • ***
  • Posts: 105
Re: CAN-BUS SocketCAN
« Reply #68 on: May 17, 2021, 03:23:15 pm »
This thread mentions C to Pascal translation several times.  I'm wondering if this is not also an opportunity to test out some of the automatic C to Pascal or C to Delphi translators.  Probably wouldn't be as good as being done manually by someone very fluent in both, but perhaps the automatic software could be useful in various cases.

avra

  • Hero Member
  • *****
  • Posts: 2131
    • Additional info
Re: CAN-BUS SocketCAN
« Reply #69 on: May 18, 2021, 12:58:09 am »
What device(s) do you use to interface with CAN bus?
Pi, PocketBeagle and Arduino (AVR, STM32, ESP32)

Does Arduino (AVR) has to be some specific type (e.g. AT90CAN128) or any will do?
What do you use between 'controller' and CAN bus?

You can use some CAN based AVR as AT90CAN128 with just a TJA1050 or similar CAN transceiver, but it is more common to use simpler AVR (like ATMEGA328P) with MCP2515 and some CAN transceiver chip as MCP is much better supported. Using MCP2515 also means that besides using CAN directly from AVR, you can also use it from Linux as Lawicel/SLCAN based CAN gateway for SocketCAN with something like this: https://github.com/latonita/arduino-canbus-monitor, https://github.com/autowp/can-usb or https://github.com/mintynet/nano-can. You are limited with serial to USB chip speed on your Arduino so with speeds of 500kbps and higher expect dropped frames (and even with 250kbps if your CAN bus load is high). Although MCP2515 is the most supported CAN 2.0B chip on the planet, if you need CAN FD then it will not be enough and you will have to use MCP2518 and similar with a proper lib.

Although ESP32 is much more capable then standard AVRs, there is also a serial to USB chip so there is the same speed limit. It has internal CAN so there are libs which do not need MCP2515 to implement Lawicel/SLCAN which can be used from Linux SocketCAN. I have seen versions which use Bluetooth instead of serial to USB, but I have never tested them (https://github.com/michey/esp32_can and https://github.com/mintynet/esp32-slcan). There are also versions which support CAN FD and create a WiFi UDP tunnel to Linux SocketCAN via cannelloni protocol (https://github.com/PhilippFux/ESP32_CAN_Interface and https://github.com/epozzobon/lasagne), but I haven't tested them either.

STM32 has many microcontrollers (like STM32F103C8T6 BluePill for example) which support CAN out of the box and do not need MCP2515. They also have USB but unfortunately USB and CAN can not work at the same time, so some serial to USB must also be used. Look at https://github.com/kolyandex/CAN-to-USB-lawicel-for-stm32, https://github.com/GBert/misc/tree/master/stm32-slcan, https://github.com/geier99/STM32-CAN_CubeMX_Tests, https://github.com/darauble/lawicel-slcan, http://randomport.com/pages/usb-to-can.html and https://ucandevices.github.io.

Most of the mentioned devices can be used as very cheap Lawicel/SLCAN compatible protocol CAN gateways for Linux SocketCAN, but I personally do not use them for that. I use them as embedded devices when Linux SBC is an overkill or when long booting can not be tolerated. What is good about such Lawicel/SLCAN protocol compatible devices is that there is a lot of software for Windows that can use them, so that will be your CAN path if you are not a Linux fan. However, Linux SocketCAN path is much more powerful and standardized so unlike with Windows various software can work together using data simultaneously from the same CAN adapter.

Raspberry Pi needs a HAT with MCP2515/2517/2518. PocketBeagle and BeagleBone can also use MCP, but if you do not need CAN FD then built in CAN with some transceiver will be enough. There is no speed limit or dropped frames problem with Linux boards, so you can use SocketCAN at it's full glory.

SocketCAN also supports virtual CAN channels so you can play and learn even if you have no CAN adapter at all.

For a simple introduction to CAN, CAN FD, OBD-II, CANopen and J1939 take a look at these links:
https://www.csselectronics.com/screen/page/simple-intro-to-can-bus
https://www.csselectronics.com/screen/page/can-fd-flexible-data-rate-intro
https://www.csselectronics.com/screen/page/simple-intro-obd2-explained
https://www.csselectronics.com/screen/page/simple-intro-j1939-explained
https://www.csselectronics.com/screen/page/canopen-tutorial-simple-intro

Here is also a little old but still relevant Car Hacker's Handbook:
http://www.opengarages.org/handbook/

This thread mentions C to Pascal translation several times.  I'm wondering if this is not also an opportunity to test out some of the automatic C to Pascal or C to Delphi translators.  Probably wouldn't be as good as being done manually by someone very fluent in both, but perhaps the automatic software could be useful in various cases.
I use these all the time. They are good, but not perfect and I usually have to combine them. They can save you a lot of time, but can not save you from manual work completelly.
https://forum.lazarus.freepascal.org/index.php/topic,12763.msg66330.html#msg66330
https://forum.lazarus.freepascal.org/index.php/topic,53389.msg395085.html#msg395085
https://wiki.freepascal.org/C_to_Pascal
« Last Edit: May 18, 2021, 01:40:03 am by avra »
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

dseligo

  • Sr. Member
  • ****
  • Posts: 277
Re: CAN-BUS SocketCAN
« Reply #70 on: May 18, 2021, 04:38:36 am »
Thanks  :D

I will need some time to process this  :)

ThomasK

  • New Member
  • *
  • Posts: 34
Re: CAN-BUS SocketCAN
« Reply #71 on: May 19, 2021, 01:24:21 pm »
For now, there is no plan to support libsocketcan.

I see.

Anyway for my point of view it is better if an application takes care itself of all relevant initialisation and parameterisation tasks.
At least I am used to that working with an automation system for automotive test beds. Customers always complained to if they need more than one program to get an interface working.

I also found a fork of can-utils containing canconfig.c with a more recent date than 2009. 8). I will try to get short snippets to work, as you proposed and migrate then.

I tested my short app by starting it with admin rights, it failed too. Didn't find the file, it said in the console.
Started Pascal on a Siemens 4004/151 in 1977. TurboPascal 1.0 in 1984 on PC-Dos.

ThomasK

  • New Member
  • *
  • Posts: 34
Re: CAN-BUS SocketCAN
« Reply #72 on: May 30, 2021, 10:07:21 pm »
Have you intentions to implement this:

Code: C  [Select][+][-]
  1. num_events = epoll_wait(fd_epoll, events_pending, currmax, timeout_ms);
  2.                 if (num_events == -1) {
  3.                         if (errno != EINTR)
  4.                                 running = 0;
  5.                         continue;
  6.                 }
  7.  

This would allow a read process not to wait until data is available.

I found that in the recent can-utils.
Started Pascal on a Siemens 4004/151 in 1977. TurboPascal 1.0 in 1984 on PC-Dos.

avra

  • Hero Member
  • *****
  • Posts: 2131
    • Additional info
Re: CAN-BUS SocketCAN
« Reply #73 on: May 31, 2021, 01:30:08 am »
I found that in the recent can-utils.
Not everything is related to CAN. If you take a look at candump.c from can-utils you will see that it includes sys/epoll.h which is a standard linux header for which there is a man page https://linux.die.net/man/2/epoll_wait. As such, you could also search your fpc directory and find that translation for epoll_wait can already be found in your existing \fpcsrc\rtl\linux\linux.pp, so you already have everything needed.

You need to improve your source search fu a little, if you want the source to be with you.  :D ;) :D
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

ThomasK

  • New Member
  • *
  • Posts: 34
Re: CAN-BUS SocketCAN
« Reply #74 on: May 31, 2021, 02:46:54 pm »
I know that this is not only CAN related.
But this code snippet is from the latest https://github.com/linux-can/can-utils candump.c.

I try to get this to work.
Started Pascal on a Siemens 4004/151 in 1977. TurboPascal 1.0 in 1984 on PC-Dos.

 

TinyPortal © 2005-2018