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

Specify encoding when decoding registers to string #2555

Open
Breina opened this issue Jan 28, 2025 · 9 comments
Open

Specify encoding when decoding registers to string #2555

Breina opened this issue Jan 28, 2025 · 9 comments

Comments

@Breina
Copy link

Breina commented Jan 28, 2025

When calling convert_from_registers with data_type=DATATYPE.STRING, the encoding utf-8 is used.

Please parameterize this encoding so that it works with other text encoding formats (such as latin-1 in my case).

@janiversen
Copy link
Collaborator

janiversen commented Jan 30, 2025

Pull requests are welcome, we use utf-8 because it is the most generic one, and please be aware that it is actually not really used, since the byte value is used directly.

What is your problem exactly ?

@janiversen
Copy link
Collaborator

Please be aware you are not forced to use the convert functions, you can easily do it in the app.

Btw saw the app is actually a custom component of HA, which means you are using a pretty old version of pymodbus (due to the component modbus) where among others the convert functions work quite differently.

@Breina
Copy link
Author

Breina commented Jan 30, 2025

Thanks for the quick response!

I used BinaryPayloadDecoder.decode_string before and parsed that with python's bytes.decode. The warning message on BinaryPayloadDecoder advised me to use convert_from_registers, which did feel pretty clean.

My user has a starting byte of 0x80, which isn't ASCII nor UTF-8, so I need latin-1 extended ASCII specifically. I can create a PR for this.

Btw saw the app is actually a custom component of HA, which means you are using a pretty old version of pymodbus

Not exactly sure how it works, but I can specify a required version of pymodbus and it will get that for me. I'm running on the latest version currently.

Image

@janiversen
Copy link
Collaborator

Interesting, if you can use multiple versions of the same lib. Did you actually check either pip what is installed ?

The python builtin decode function needs a character type like utf-8, if there is a "raw" or similar I quest that would be the best change.....we do our best to avoid extra parameters.

@janiversen
Copy link
Collaborator

Just checked someone updated ha pymodbus version to 3.8.3, which is why you do not have problems.

@Breina
Copy link
Author

Breina commented Jan 30, 2025

The python builtin decode function needs a character type like utf-8, if there is a "raw" or similar I quest that would be the best change.....we do our best to avoid extra parameters.

I basically need to have a latin-1 instead of utf-8, would an optional parameter that defaults to utf-8 be alright? That's still perfectly backwards compatible then.

@janiversen
Copy link
Collaborator

A better solution would be to add "errors='ignore'" to the decode call, which should solve all your problems.

@Breina
Copy link
Author

Breina commented Jan 30, 2025

I don't think this would solve my problem. I need the 0x80 byte to be decoded to Ç.

If you don't like the parameter, I can parse the bytes outside of your lib as you said, but I felt it would be cleaner to provide a way to configure it on your end.

@janiversen
Copy link
Collaborator

A parameter with a correct default, would be accepted, please remember to add test cases (e.g. what happens if parameter="raw").

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