pyhys / minimalmodbus

Easy-to-use Modbus RTU and Modbus ASCII implementation for Python.
Apache License 2.0
308 stars 145 forks source link

How to add caracter NULL in minimalmodbus.write_string() ? #76

Closed patfrench closed 3 years ago

patfrench commented 3 years ago

Hi,

I try to place a null caracter terminator in

write_string(0x1402,"SIMTRO698\0",6)

is it possible ?

Thank you

j123b567 commented 3 years ago

Did you try it already? Do you have any issues?

You are writing 6 registers but your string is 5 registers log, so it will be padded by spaces as stated in documentation. If you need it to be padded by zeros, you should use "SIMTRO698\0\0\0"

patfrench commented 3 years ago

Hi,

I have no problem with write_string(0x1402,"SIMTRO698",5)

my problem is that the terminal character must be 0 (NULL in ASCII) I want to write the terminal character in the 6th register I will try your solution.

why three \0 ?

"SIMTRO698\0\0\0"

Thank you for answering so quickly

j123b567 commented 3 years ago

Please, count number of characters first. SIMTRO698 is 9 characters but you are writing 5 registers, which mean 10 characters. As the documentation of write_string states. Your string will be padded with spaces. So in your case, just use write_string(0x1402,"SIMTRO698\0",5) If you need to set also the 6th register, then you should define also its content, if spaces are not good for you, so this is the meaning of additional two \0. So the alternative is write_string(0x1402,"SIMTRO698\0\0\0", 6).

patfrench commented 3 years ago

oups i'm sorry :( my str is "SIMTROF698" 0x1402 => 'SI' 0x1403 => 'MT' 0x1404 => 'RO' 0x1405 => 'F6' 0x1406 => '98' 0x1407 => caracter terminator = NUL

j123b567 commented 3 years ago

So then write_string(0x1402,"SIMTROF698\0", 6) should just work

Or if you need to fill entire the last register write_string(0x1402,"SIMTROF698\0\0", 6)

patfrench commented 3 years ago

hi, perfect !!!! it's good

thank you for your support

j123b567 commented 3 years ago

Your welcome. Please, close the issue if it is solved.