Unable to read float value from a PLC address












2















#!/usr/bin/env python

from pymodbus.constants import Endian
from pymodbus.payload import BinaryPayloadDecoder
from pymodbus.payload import BinaryPayloadBuilder
from pymodbus.client.sync import ModbusTcpClient
import struct
import logging

logging.basicConfig()
log = logging.getLogger()
log.setLevel(logging.INFO)

ip_address = "192.168.1.55"

client = ModbusTcpClient(ip_address)
if client.connect(): # connection is OK
# write float
#builder = BinaryPayloadBuilder(endian=Endian.Little)
#builder.add_32bit_float(77.77)
#payload = builder.build()
#result = client.write_registers(1, payload, skip_encode=True)
# read floats
result = client.read_holding_registers(28, 2)

decoder = BinaryPayloadDecoder.fromRegisters(result.registers,
endian=Endian.Big)


print str(decoder.decode_32bit_float())
client.close()
var1 = str(decoder.decode_32bit_float())


I m using PyModbus library to read float values from a PLC . I m getting right value from str(decoder.decode_32bit_float()) but failed to put it into a variable var1. It shows error (in decode 32 bit float return unpack(string, handle)[0] struct.error: unpack requires a string argument of length 4).
Thanks in advance










share|improve this question




















  • 2





    ever worked with streams? when you read a stream - f.e. by printing its str(). it is empty afterward because you are past the data in it ... unless more data comes in its empty and can not deliver more. Try var1 = decoder.decode_32bit_float() first , then print(var1) - dont use str unless you need a string.

    – Patrick Artner
    Nov 15 '18 at 11:41













  • Thanks Patrick it worked i really appreciate it Thanks

    – R-T
    Nov 15 '18 at 11:44











  • Thought so :) maybe delete it before it gets too many downvotes. Its the same for filestreams etc - once you read from it you are past the data you just read. Or selfanswer

    – Patrick Artner
    Nov 15 '18 at 11:51













  • @R-T Check this POST, I hope would help you up

    – Benyamin Jafari
    Nov 15 '18 at 22:03
















2















#!/usr/bin/env python

from pymodbus.constants import Endian
from pymodbus.payload import BinaryPayloadDecoder
from pymodbus.payload import BinaryPayloadBuilder
from pymodbus.client.sync import ModbusTcpClient
import struct
import logging

logging.basicConfig()
log = logging.getLogger()
log.setLevel(logging.INFO)

ip_address = "192.168.1.55"

client = ModbusTcpClient(ip_address)
if client.connect(): # connection is OK
# write float
#builder = BinaryPayloadBuilder(endian=Endian.Little)
#builder.add_32bit_float(77.77)
#payload = builder.build()
#result = client.write_registers(1, payload, skip_encode=True)
# read floats
result = client.read_holding_registers(28, 2)

decoder = BinaryPayloadDecoder.fromRegisters(result.registers,
endian=Endian.Big)


print str(decoder.decode_32bit_float())
client.close()
var1 = str(decoder.decode_32bit_float())


I m using PyModbus library to read float values from a PLC . I m getting right value from str(decoder.decode_32bit_float()) but failed to put it into a variable var1. It shows error (in decode 32 bit float return unpack(string, handle)[0] struct.error: unpack requires a string argument of length 4).
Thanks in advance










share|improve this question




















  • 2





    ever worked with streams? when you read a stream - f.e. by printing its str(). it is empty afterward because you are past the data in it ... unless more data comes in its empty and can not deliver more. Try var1 = decoder.decode_32bit_float() first , then print(var1) - dont use str unless you need a string.

    – Patrick Artner
    Nov 15 '18 at 11:41













  • Thanks Patrick it worked i really appreciate it Thanks

    – R-T
    Nov 15 '18 at 11:44











  • Thought so :) maybe delete it before it gets too many downvotes. Its the same for filestreams etc - once you read from it you are past the data you just read. Or selfanswer

    – Patrick Artner
    Nov 15 '18 at 11:51













  • @R-T Check this POST, I hope would help you up

    – Benyamin Jafari
    Nov 15 '18 at 22:03














2












2








2


1






#!/usr/bin/env python

from pymodbus.constants import Endian
from pymodbus.payload import BinaryPayloadDecoder
from pymodbus.payload import BinaryPayloadBuilder
from pymodbus.client.sync import ModbusTcpClient
import struct
import logging

logging.basicConfig()
log = logging.getLogger()
log.setLevel(logging.INFO)

ip_address = "192.168.1.55"

client = ModbusTcpClient(ip_address)
if client.connect(): # connection is OK
# write float
#builder = BinaryPayloadBuilder(endian=Endian.Little)
#builder.add_32bit_float(77.77)
#payload = builder.build()
#result = client.write_registers(1, payload, skip_encode=True)
# read floats
result = client.read_holding_registers(28, 2)

decoder = BinaryPayloadDecoder.fromRegisters(result.registers,
endian=Endian.Big)


print str(decoder.decode_32bit_float())
client.close()
var1 = str(decoder.decode_32bit_float())


I m using PyModbus library to read float values from a PLC . I m getting right value from str(decoder.decode_32bit_float()) but failed to put it into a variable var1. It shows error (in decode 32 bit float return unpack(string, handle)[0] struct.error: unpack requires a string argument of length 4).
Thanks in advance










share|improve this question
















#!/usr/bin/env python

from pymodbus.constants import Endian
from pymodbus.payload import BinaryPayloadDecoder
from pymodbus.payload import BinaryPayloadBuilder
from pymodbus.client.sync import ModbusTcpClient
import struct
import logging

logging.basicConfig()
log = logging.getLogger()
log.setLevel(logging.INFO)

ip_address = "192.168.1.55"

client = ModbusTcpClient(ip_address)
if client.connect(): # connection is OK
# write float
#builder = BinaryPayloadBuilder(endian=Endian.Little)
#builder.add_32bit_float(77.77)
#payload = builder.build()
#result = client.write_registers(1, payload, skip_encode=True)
# read floats
result = client.read_holding_registers(28, 2)

decoder = BinaryPayloadDecoder.fromRegisters(result.registers,
endian=Endian.Big)


print str(decoder.decode_32bit_float())
client.close()
var1 = str(decoder.decode_32bit_float())


I m using PyModbus library to read float values from a PLC . I m getting right value from str(decoder.decode_32bit_float()) but failed to put it into a variable var1. It shows error (in decode 32 bit float return unpack(string, handle)[0] struct.error: unpack requires a string argument of length 4).
Thanks in advance







python plc pymodbus






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 15:12









Cheche

816218




816218










asked Nov 15 '18 at 11:38









R-TR-T

456




456








  • 2





    ever worked with streams? when you read a stream - f.e. by printing its str(). it is empty afterward because you are past the data in it ... unless more data comes in its empty and can not deliver more. Try var1 = decoder.decode_32bit_float() first , then print(var1) - dont use str unless you need a string.

    – Patrick Artner
    Nov 15 '18 at 11:41













  • Thanks Patrick it worked i really appreciate it Thanks

    – R-T
    Nov 15 '18 at 11:44











  • Thought so :) maybe delete it before it gets too many downvotes. Its the same for filestreams etc - once you read from it you are past the data you just read. Or selfanswer

    – Patrick Artner
    Nov 15 '18 at 11:51













  • @R-T Check this POST, I hope would help you up

    – Benyamin Jafari
    Nov 15 '18 at 22:03














  • 2





    ever worked with streams? when you read a stream - f.e. by printing its str(). it is empty afterward because you are past the data in it ... unless more data comes in its empty and can not deliver more. Try var1 = decoder.decode_32bit_float() first , then print(var1) - dont use str unless you need a string.

    – Patrick Artner
    Nov 15 '18 at 11:41













  • Thanks Patrick it worked i really appreciate it Thanks

    – R-T
    Nov 15 '18 at 11:44











  • Thought so :) maybe delete it before it gets too many downvotes. Its the same for filestreams etc - once you read from it you are past the data you just read. Or selfanswer

    – Patrick Artner
    Nov 15 '18 at 11:51













  • @R-T Check this POST, I hope would help you up

    – Benyamin Jafari
    Nov 15 '18 at 22:03








2




2





ever worked with streams? when you read a stream - f.e. by printing its str(). it is empty afterward because you are past the data in it ... unless more data comes in its empty and can not deliver more. Try var1 = decoder.decode_32bit_float() first , then print(var1) - dont use str unless you need a string.

– Patrick Artner
Nov 15 '18 at 11:41







ever worked with streams? when you read a stream - f.e. by printing its str(). it is empty afterward because you are past the data in it ... unless more data comes in its empty and can not deliver more. Try var1 = decoder.decode_32bit_float() first , then print(var1) - dont use str unless you need a string.

– Patrick Artner
Nov 15 '18 at 11:41















Thanks Patrick it worked i really appreciate it Thanks

– R-T
Nov 15 '18 at 11:44





Thanks Patrick it worked i really appreciate it Thanks

– R-T
Nov 15 '18 at 11:44













Thought so :) maybe delete it before it gets too many downvotes. Its the same for filestreams etc - once you read from it you are past the data you just read. Or selfanswer

– Patrick Artner
Nov 15 '18 at 11:51







Thought so :) maybe delete it before it gets too many downvotes. Its the same for filestreams etc - once you read from it you are past the data you just read. Or selfanswer

– Patrick Artner
Nov 15 '18 at 11:51















@R-T Check this POST, I hope would help you up

– Benyamin Jafari
Nov 15 '18 at 22:03





@R-T Check this POST, I hope would help you up

– Benyamin Jafari
Nov 15 '18 at 22:03












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53318657%2funable-to-read-float-value-from-a-plc-address%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53318657%2funable-to-read-float-value-from-a-plc-address%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Hercules Kyvelos

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud