Recent

Author Topic: CAN-BUS SockectCAN  (Read 4636 times)

MarkMLl

  • Hero Member
  • *****
  • Posts: 2093
Re: CAN-BUS SockectCAN
« Reply #15 on: October 22, 2020, 01:29:16 pm »
Code: Pascal  [Select][+][-]
  1.     if fpconnect(socket_result, @SAddr, 0) = -1 then begin
  2.  

Why the third parameter of fpconnect is zero?

Well spotted ;-)

For that matter, I have a lot of trouble tying https://www.freepascal.org/docs-html/rtl/sockets/fpconnect.html to anything remotely relevant.

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.

jcdammeyer

  • Full Member
  • ***
  • Posts: 119
  • Embedded System Developer
    • Automation Artisans Inc.
Re: CAN-BUS SockectCAN
« Reply #16 on: October 22, 2020, 07:06:19 pm »
So in general I get the feeling that to add socketCAN support to FPC will require a fairly extensive modification of the base sockets library.
PF_CAN is just a number. In linux sources you can find it defined as AF_CAN, which is defined as 29. So why don't you try that first?

EDIT: I see now that you are looking at sources of candump. It will be much easier if you use simpler example from here:
https://www.beyondlogic.org/example-c-socketcan-code/

True.  And in my example I assigned 29 to PF_CAN.  The beyondlogic example in C translated to pascal still suffers from the same issue that it's not handled in the socket library.  Thank you for that link.  It does simplify the code.  The candump.c has become more complicated with CAN_FD support which I'm not interested in looking at right now.  Especially since it's not supported by the MCP2515 and the BeagleBone internal CAN device nor many of the other existing CANopen modules out there.

I don't really want to add what appears to be an extra layer with TCP if I don't have to.  I'll download and compile socketcand to see if it fits in with what I want to do.

BTW, under winsock2.pp 29 is allocated to
Code: Pascal  [Select][+][-]
  1.   AF_TCNPROCESS   = 29;
  2.   AF_TCNMESSAGE   = 30;
  3.   AF_ICLFXBM      = 31;
  4.  
  5.   AF_MAX          = 32;
  6.  

My goal is to make something that runs on Windows, Pi and BeagleBones and other small modules running Linux.  Even LinuxCNC.  Not that concerned about Linux Desktop PCs.  The PC doesn't have the SPI or embedded CAN device so the CANUSB from Lawicel (or equivalent module with identical protocol) is what I'm currently targeting.


jcdammeyer

  • Full Member
  • ***
  • Posts: 119
  • Embedded System Developer
    • Automation Artisans Inc.
Re: CAN-BUS SockectCAN
« Reply #17 on: October 22, 2020, 07:08:55 pm »
Code: Pascal  [Select][+][-]
  1.     if fpconnect(socket_result, @SAddr, 0) = -1 then begin
  2.  

Why the third parameter of fpconnect is zero?

Well spotted ;-)

For that matter, I have a lot of trouble tying https://www.freepascal.org/docs-html/rtl/sockets/fpconnect.html to anything remotely relevant.

MarkMLl
Because I never got past the socket call and had not used bind which is what should be used.  Wasn't even worried about fpconnect or the Saddr once I dove down into the socket library and saw that AF_CAN  or PF_CAN equal to 29 was not supported which essentially means the socketCAN library working in C or Python also won't work.

jcdammeyer

  • Full Member
  • ***
  • Posts: 119
  • Embedded System Developer
    • Automation Artisans Inc.
Re: CAN-BUS SockectCAN
« Reply #18 on: October 22, 2020, 07:25:05 pm »
Code: Pascal  [Select][+][-]
  1.     if fpconnect(socket_result, @SAddr, 0) = -1 then begin
  2.  

Why the third parameter of fpconnect is zero?
Awe Crap!  That's what I get for leaving a project for 2 months and then jumping back in not remembering where I stopped.  In fact both you and Mark are correct that I'm using fpconnect incorrectly.
My mistake is that I though I didn't make it past the socket call which I do and returns 11 and not a value < 0. 

OK.  Some steps backwards and then I'll report what I've discovered.  Sheesh. Newbie type mistake...

avra

  • Hero Member
  • *****
  • Posts: 2112
    • Additional info
Re: CAN-BUS SockectCAN
« Reply #19 on: February 22, 2021, 03:41:43 pm »
For a start, I would like to update fpc's linux kernel header translations with missing AF_CAN, PF_CAN and some other constants.

Needed kernel header is at
Quote
/usr/src/linux-headers-5.10.0-0.bpo.3-common/include/linux/socket.h
and I want to update file
Quote
fpcsrc/packages/rtl-extra/src/linux/unxsockh.inc

Should I also update
Quote
fpcsrc/packages/rtl-extra/src/android/unxsockh.inc
as well?

I only intend to test linux...
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

MarkMLl

  • Hero Member
  • *****
  • Posts: 2093
Re: CAN-BUS SockectCAN
« Reply #20 on: February 22, 2021, 03:56:43 pm »
For a start, I would like to update fpc's linux kernel header translations with missing AF_CAN, PF_CAN and some other constants.
...
I only intend to test linux...

Noting that the AF_ and PF_ constants are used by user and/or kernel code to express an interest in a particular protocol facet, and that there aren't helpers to extend enumerations and subranges, I think that as a matter of policy they're worth getting into header files etc. as soon as they are standardised by the IETF or whoever's authoritative.

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.

jcdammeyer

  • Full Member
  • ***
  • Posts: 119
  • Embedded System Developer
    • Automation Artisans Inc.
Re: CAN-BUS SockectCAN
« Reply #21 on: February 23, 2021, 03:44:16 am »
For now this project is on hold for while I catch up on a few others.  I'm working on a project that uses the TI TMS320F28379D and the CAN bus and Quadrature encoder interface.  Also not quite finished a CNC mill conversion.  https://youtu.be/I6lvnYhlez4
When I get a few of these other projects out of the way I'll come back to Lazarus and see about the sockets and socketCAN.

avra

  • Hero Member
  • *****
  • Posts: 2112
    • Additional info
Re: CAN-BUS SockectCAN
« Reply #22 on: February 23, 2021, 08:19:58 am »
For a start, I would like to update fpc's linux kernel header translations with missing AF_CAN, PF_CAN and some other constants.
...
I only intend to test linux...

Noting that the AF_ and PF_ constants are used by user and/or kernel code to express an interest in a particular protocol facet, and that there aren't helpers to extend enumerations and subranges, I think that as a matter of policy they're worth getting into header files etc. as soon as they are standardised by the IETF or whoever's authoritative.
Those constants are only a start. I searched FPC files for them, and found where they are and what should be updated. I need a little guiding since it is not simple 1:1 header translation and there is a split into several files - probably because of cross platforming. Only when this work is finished, can and j1939 can build on them.

As for the standardization... well, I don't know what to say. Try to search in sources for AF_BLUETOOTH and you will see that in some operating systems it is defined as 31 and in some as 36. It seams to be OS dependent and not tied to some standard...
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

MarkMLl

  • Hero Member
  • *****
  • Posts: 2093
Re: CAN-BUS SockectCAN
« Reply #23 on: February 23, 2021, 08:33:04 am »
As for the standardization... well, I don't know what to say. Try to search in sources for AF_BLUETOOTH and you will see that in some operating systems it is defined as 31 and in some as 36. It seams to be OS dependent and not tied to some standard...

Hmmm. Another of those things like the data returned by a stat() call, where OSes varied and Linux tried to follow the dominant OS on each platform.

I obviously knew that the numerical value of AF_ and PF_ couldn't be trusted to be the same, but I'd assumed that ultimately they were traceable to IETF or similar... I was obviously wrong.

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.

jcdammeyer

  • Full Member
  • ***
  • Posts: 119
  • Embedded System Developer
    • Automation Artisans Inc.
Re: CAN-BUS SockectCAN
« Reply #24 on: February 23, 2021, 08:41:44 am »
The issue isn't only the AF_CAN and PF_CAN.  The lower levels need to deal with the socketCAN infrastructure.  The best way to do this is to research what happens with AF_CAN and PF_CAN in sockets.c.  The socketCAN interface has been usable in Linux now for almost 10 years.  In fact the newest code I was looking at now also supports CANFD which is an enhancement to the original CAN protocol that supports larger messages at higher data rates for the data part.

Even if CANFD was not supported initially I suspect the best thing to do is to port the socketCAN section from the C code to the pascal side.  Then rewrite, again without the CANFD support, the command line applications that are written in C like candump.

The candump application is clever enough to know the difference between a Lawicel CANUSB dongle and say the SPI based Pi MCP2515 or the Beagle embledded CAN device.  In both cases those devices show up as files that can be opened.

To do most of this one doesn't have to actually know much about CAN, although it's useful, but mostly about how Linux handles devices that are files and how the socket communications deals with them.  It's on my do list.
John



avra

  • Hero Member
  • *****
  • Posts: 2112
    • Additional info
Re: CAN-BUS SockectCAN
« Reply #25 on: February 23, 2021, 02:48:05 pm »
The issue isn't only the AF_CAN and PF_CAN...
That's why I said that updating socket wrapper with new constants is just a first step. Next step would be CAN through SocketCAN (not via socketcand daemon which already allows us to connect to CAN using ethernet), and after that SAE J1939 which is a layer on top of CAN. That's what I'm after. CAN FD is not in my focus, but I will probably add it, too.

« Last Edit: February 23, 2021, 03:06:05 pm by avra »
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

jcdammeyer

  • Full Member
  • ***
  • Posts: 119
  • Embedded System Developer
    • Automation Artisans Inc.
Re: CAN-BUS SockectCAN
« Reply #26 on: February 23, 2021, 06:23:51 pm »
The issue isn't only the AF_CAN and PF_CAN...
That's why I said that updating socket wrapper with new constants is just a first step. Next step would be CAN through SocketCAN (not via socketcand daemon which already allows us to connect to CAN using ethernet), and after that SAE J1939 which is a layer on top of CAN. That's what I'm after. CAN FD is not in my focus, but I will probably add it, too.
My focus will be CANopen with a monitor program that runs equally well on PCs with Windows, LinuxCNC, Pi and Beagles.  But I've worked with all sorts of CAN protocols..
http://www.autoartisans.com/rings/9S12-Controller.jpg  This M9S12 has 5 CAN channels and I've used it on one project that had a proprietary protocol on one channel, CANopen on two more actiing as bridge and two J1939, one to servo motors and one to the vehicle for engine RPM and transmission status.   Software on the PC to interface to this was done with Delphi to the USB port
This module here http://www.autoartisans.com/CAN/STU-2.jpg runs a PiZeroW and includes a PIC32 for the high speed CAN buffering and then SPI.  C software with pipes etc. to log CAN messages from an electric vehicle into Pi Linux.  I had to use C for that.  Would have been nice to be able to use Lazarus.
Both those projects are for clients so I can't disclose details.  But there are lots of other projects on the go that will be published for playing with CAN and Pi or BBB.


avra

  • Hero Member
  • *****
  • Posts: 2112
    • Additional info
Re: CAN-BUS SockectCAN
« Reply #27 on: March 02, 2021, 01:55:42 pm »
CAN constants have been added to FPC 48867:
https://svn.freepascal.org/svn/fpc/trunk/packages/rtl-extra/src/linux/unxsockh.inc

Also other stuff not related to CAN has been added from .../include/linux/socket.h in Linux 5.10 headers.

Proceeding further steps...
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

jcdammeyer

  • Full Member
  • ***
  • Posts: 119
  • Embedded System Developer
    • Automation Artisans Inc.
Re: CAN-BUS SockectCAN
« Reply #28 on: March 02, 2021, 06:25:31 pm »
Awesome.  You've probably already seen this but for convenience here's the link:
https://www.beyondlogic.org/example-c-socketcan-code

I guess for example one would take the C code and create a Lazarus command line version of candump called pcandump to avoid mixing things up.  But it needs that sockets.pp (I think that was the file) modified to understand the AF_CAN and PF_CAN constants.

https://elinux.org/Can-utils

The above link is to an example which has a link to the source which has a link to the doc.
« Last Edit: March 02, 2021, 06:30:10 pm by jcdammeyer »

avra

  • Hero Member
  • *****
  • Posts: 2112
    • Additional info
Re: CAN-BUS SockectCAN
« Reply #29 on: March 03, 2021, 09:54:10 am »
Awesome.  You've probably already seen this but for convenience here's the link:
https://www.beyondlogic.org/example-c-socketcan-code
That is the link I gave in my first reply to this topic.  ;)

I guess for example one would take the C code and create a Lazarus command line version of candump called pcandump to avoid mixing things up.
My initial goal would be to make pascal versions of cantransmit, canreceive and canfilter from https://github.com/craigpeacock/CAN-Examples. Then I would probably move to J1939.

But it needs that sockets.pp (I think that was the file) modified to understand the AF_CAN and PF_CAN constants.
Already done. With latest trunk you simply include sockets and those constants are available.

https://elinux.org/Can-utils
The above link is to an example which has a link to the source which has a link to the doc.
I am familiar with that, too. I am the author of https://github.com/linux-can/can-utils/blob/master/can-j1939-install-kernel-module.md  :D
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

 

TinyPortal © 2005-2018