Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bind() must be called before using the socket #156

Closed
Schuchmann opened this issue Apr 12, 2023 · 6 comments
Closed

bind() must be called before using the socket #156

Schuchmann opened this issue Apr 12, 2023 · 6 comments

Comments

@Schuchmann
Copy link

Hi,
I am trying a very simple example:

#!/usr/bin/env python3
import udsoncan
from udsoncan.connections import IsoTPSocketConnection
from udsoncan.client import Client
from udsoncan.exceptions import *
from udsoncan.services import *
udsoncan.setup_logging()
my_connection = IsoTPSocketConnection('canfd0', rxid=0x12, txid=0x23)
my_connection.send(b'\x11\x01\x77\x88\x99')

but I get the following error:

2023-04-12 17:20:20 [DEBUG] Connection: Sending 5 bytes : [b'1101778899']
Traceback (most recent call last):
  File "./test.py", line 12, in <module>
    my_connection.send(b'\x11\x01\x77\x88\x99')
  File "/home/pi/.local/lib/python3.7/site-packages/udsoncan/connections.py", line 65, in send
    self.specific_send(payload)
  File "/home/pi/.local/lib/python3.7/site-packages/udsoncan/connections.py", line 321, in specific_send
    self.tpsock.send(payload)
  File "/home/pi/.local/lib/python3.7/site-packages/isotp/tpsock/__init__.py", line 83, in send
    raise RuntimeError("bind() must be called before using the socket")
RuntimeError: bind() must be called before using the socket

Can someone help. What am I doing wrong?

@pylessard
Copy link
Owner

my_connection.open()

you can also do

with IsoTPSocketConnection('canfd0', rxid=0x12, txid=0x23) as conn:
    conn.send(bytes([1,2,3]))

@Schuchmann
Copy link
Author

@pylessard Thanks for the answer, but getting nearly the same error with your exampe:

2023-04-12 18:06:19 [DEBUG] Connection: Sending 3 bytes : [b'010203']
Traceback (most recent call last):
  File "./test.py", line 14, in <module>
    conn.send(bytes([1,2,3]))
  File "/home/pi/.local/lib/python3.7/site-packages/udsoncan/connections.py", line 65, in send
    self.specific_send(payload)
  File "/home/pi/.local/lib/python3.7/site-packages/udsoncan/connections.py", line 321, in specific_send
    self.tpsock.send(payload)
  File "/home/pi/.local/lib/python3.7/site-packages/isotp/tpsock/__init__.py", line 83, in send
    raise RuntimeError("bind() must be called before using the socket")
RuntimeError: bind() must be called before using the socket

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "./test.py", line 14, in <module>
    conn.send(bytes([1,2,3]))
  File "/home/pi/.local/lib/python3.7/site-packages/udsoncan/connections.py", line 297, in __exit__
    self.close()
  File "/home/pi/.local/lib/python3.7/site-packages/udsoncan/connections.py", line 315, in close
    self.rxthread.join()
AttributeError: 'IsoTPSocketConnection' object has no attribute 'rxthread'

@pylessard
Copy link
Owner

You are right. The connection can'T be used in a with statement. My bad.
Just call open.

@Schuchmann
Copy link
Author

Juhuuuu... calling open() works!
Maybe something to put into the example: https://udsoncan.readthedocs.io/en/latest/udsoncan/examples.html#raw-connection

One more question:
Is it possible to use extended IDs also?
If I am trying
my_connection = IsoTPSocketConnection(interface='canfd0', rxid=0x1ac9fef8, txid=0x1ac8f8fe)
I get

ValueError: txid must be smaller than 0x7FF for 11 bits identifier

How can I switch to 29Bit Identifiers?

@pylessard
Copy link
Owner

It's possible. There's a small caveat. IsotpSocketConnection is the first conenction I wrote and the codebase has evolved and now I don't want to break previous interfaces.. The actual constructor won't let you do this cleanly.

import isotp
address = isotp.Address(isotp.AddressingMode.Normal_29bits, rxid=0x123456, txid=0x789ABC)
IsoTPSocketConnection(interface='canfd0', rxid=address.rxid, txid=address.txid, address=address)   # Duplication of information here.... bad interface, but will work.

For something cleaner, you can create the socket yourself and use a SocketConnection

form udsoncan.connections import SocketConnection
import isotp
address = isotp.Address(isotp.AddressingMode.Normal_29bits, rxid=0x123456, txid=0x789ABC)
sock = isotp.socket()
sock.bind(address)
conn = SocketConnection(sock)

@Schuchmann
Copy link
Author

Thank you very much. Your example works with Ext-IDs!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants