Re: J1939 Questions on Intended usage

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Am 15.05.21 um 16:01 schrieb Patrick Menschel:
> Am 15.05.21 um 14:26 schrieb Kurt Van Dijck:
>> On Fri, 14 May 2021 12:04:47 +0000, Patrick Menschel wrote:
>>> Do I need to open one socket per PGN I'm sending?
>>> e.g.
>>>
>>> s1 = socket.socket(socket.AF_CAN, socket.SOCK_DGRAM, socket.CAN_J1939)
>>> s1.bind(interface_name, MY_NAME, PGN_OF_TSC1, MY_SA)
>>> s1.write(bytes(8))
>>>
>>> s2 = socket.socket(socket.AF_CAN, socket.SOCK_DGRAM, socket.CAN_J1939)
>>> s2.bind(interface_name, MY_NAME, PGN_OF_EBC1, MY_SA)
>>> s2.write(bytes(8))
>>
>> No, you don't _need_ to. You can.
>>
>> If you need quite some different PGN's, it may be more interesting to:
>> s = socket.socket(socket.AF_CAN, socket.SOCK_DGRAM, socket.CAN_J1939)
>> s.bind(interface_name, MY_NAME, ANY_PGN, MY_SA)
>> s.sendto(bytes(8), DST_1, PGN_1)
>> s.sendto(bytes(8), DST_2, PGN_2)
>> ...
>>
>> I'm not a python expert, I just assume something like that is possible.
> 
> Yes, the method exists
> 
> sendto()
> https://docs.python.org/3/library/socket.html#socket.socket.sendto
> https://github.com/python/cpython/blob/main/Modules/socketmodule.c#L4279
> 
> but apparently sockaddr_can is not yet expanded to individual parameters
> as it was done with the
> 
> bind()
> https://github.com/python/cpython/blob/main/Modules/socketmodule.c#L2207
> 
> Then I'll start by passing in the sockaddr_can struct as a first test
> and make a PR to that repo in the long run.

Guess I have to amend that impression, everything works fine,
except for broadcast which gives me a PermissionError ?!

python -i
Python 3.9.5 (default, May 13 2021, 13:29:45)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
>>> s = socket.socket(socket.AF_CAN, socket.SOCK_DGRAM, socket.CAN_J1939)
>>> s.bind(("mcp0", 0, 0x40000, 0x20))
>>> s.sendto(bytes(range(8)), ("", 0, 0x12300, 0x30))
8
>>> s.sendto(bytes(range(0,0x88,0x11)), ("", 0, 0x12300, 0x30))
8
>>>

Took me some try and error to get rid of

OSError: [Errno 77] File descriptor in bad state

but at least something comes out.

candump mcp0
  mcp0  19233020   [8]  00 01 02 03 04 05 06 07
  mcp0  19233020   [8]  00 11 22 33 44 55 66 77

Maybe I didn't get the concept at all.



The transport protocol also does something.

s.sendto(bytes(range(64)), ("mcp0", 0, 0xFECA, 0x20))


TP.CM.RTS
  mcp0  18EC2020   [8]  10 40 00 0A 0A CA FE 00
TP.CM.CTS
  mcp0  18EC2020   [8]  11 0A 01 FF FF CA FE 00

TP.DT
  mcp0  18EB2020   [8]  01 00 01 02 03 04 05 06
  mcp0  18EB2020   [8]  02 07 08 09 0A 0B 0C 0D
  mcp0  18EB2020   [8]  03 0E 0F 10 11 12 13 14
  mcp0  18EB2020   [8]  04 15 16 17 18 19 1A 1B
  mcp0  18EB2020   [8]  05 1C 1D 1E 1F 20 21 22
  mcp0  18EB2020   [8]  06 23 24 25 26 27 28 29
  mcp0  18EB2020   [8]  07 2A 2B 2C 2D 2E 2F 30
  mcp0  18EB2020   [8]  08 31 32 33 34 35 36 37
  mcp0  18EB2020   [8]  09 38 39 3A 3B 3C 3D 3E
  mcp0  18EB2020   [8]  0A 3F FF FF FF FF FF FF

TP.CM.EndMsgAck
  mcp0  18EC2020   [8]  13 40 00 0A FF CA FE 00

Those with a little knowledge of J1939 will now roll on the floor ;-)


The only thing that I didn't get to work is send to broadcast.
That PermissionError is somewhat strange.

>>> s.sendto(bytes(range(8)), ("", 0, 0xFECA, 0xFF))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
PermissionError: [Errno 13] Permission denied

>>> s.sendto(bytes(range(64)), ("mcp0", 0, 0xFECA, 0xFF))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
PermissionError: [Errno 13] Permission denied


Best Regards,
Patrick



[Index of Archives]     [Automotive Discussions]     [Linux ARM Kernel]     [Linux ARM]     [Linux Omap]     [Fedora ARM]     [IETF Annouce]     [Security]     [Bugtraq]     [Linux]     [Linux OMAP]     [Linux MIPS]     [eCos]     [Asterisk Internet PBX]     [Linux API]     [CAN Bus]

  Powered by Linux