GithubHelp home page GithubHelp logo

Comments (6)

pylessard avatar pylessard commented on July 20, 2024

Hi, you are not so far from it. In your case, you need to define a codec with a decode method not encode and you must specify the codec object in your config, not a function. The payload comes in as a binary string. Something like that should do (untested).

class MyCustomCodec(DidCodec):
    def decode(self, payload):
        return (int(payload[0]) << 8 + int(payload[1])) * 0.01544

    def __len__(self):
        return 2

config['data_identifiers'] = {0x019E: MyCustomCodec}

from python-udsoncan.

CSG2019 avatar CSG2019 commented on July 20, 2024

Hello pylessard,
Thank you for your guidance.
It seems that I have to define a Class first, which contains the decode(formula) and the response length, then I can call this through the 'config'.
But I don't understand why it return (int(payload[0]) << 8 + int(payload(1))) * 0.01544, actually, I want to do something for the byte4 and byte5 of the response(0x05 0x62 0x01 0x9E 0x03 0x1F). Do the payload[0] and payload[1] represent byte4 and byte5?
Could you please help to explain again?
I tried to run below codes, but the error is TypeError: 'bytes' object is not callable

class MyCustomCodec(udsoncan.DidCodec):
    def decode(self, payload):
        return (int(payload[0]) << 8 + int(payload(1))) * 0.01544

    def __len__(self):
        return 2

config['data_identifiers'] = {0x019E: MyCustomCodec}

from python-udsoncan.

pylessard avatar pylessard commented on July 20, 2024

Hi,
The problem is because of the paranthesis used for byte 1. Use square bracket instead. My apologies, I wrote paranthesis in my answer (I work a lot with Matlab, and that software use paranthesis instead of square brackets). I updated my answer.

As for the byte offset, the library strip off the rest of the response. It only feeds the Did data to the Codec.

Regards

from python-udsoncan.

CSG2019 avatar CSG2019 commented on July 20, 2024

Hello pylessard,

Thanks for your help. Now it can work as I hope. :) Really appreciated.
I am new to Python and this library. I also didn't notice the parentheses issue previously.
By the way, I can find the encode method in the example of ReadDataByIdentifier, I want to ask, in which case need to define this method?

from python-udsoncan.

pylessard avatar pylessard commented on July 20, 2024

Good,
Encode is when you use WriteDataByIdentifier

from python-udsoncan.

CSG2019 avatar CSG2019 commented on July 20, 2024

Understand, thanks :)

from python-udsoncan.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.