How to read a 16 bit register if I'm getting float value in it using NModbus4












0














I'm using NModbus4 to read a 16-bit register which already has a float value in it. I'm getting this float value directly from respectively relevant software with registers configured for those float values.
This register configuration helps me to get recorded value in my console program using NModbus4.



Question is quite simple. Nmodbus returns ushort for Holding registers and I want to get a float value while reading from response.



Yes, I tried reading two 16-bit registers and fusing them to form float value(32-bit) and NO, I'm not getting desired results.



For example 2.3e-007 results to 0 and number larger than ushort datatype results into 32767. Maybe I need to change something in library(NModbus4). Just some hint would do the trick. Please can anybody help me?



Here is my code snippet:



TcpClient masterTcpClient = new TcpClient(address.ToString(), portNo);
ModbusIpMaster master = ModbusIpMaster.CreateIp(masterTcpClient);//Created master

ushort inputs = null;
inputs = master.ReadHoldingRegisters(slaveId, startAddress, numInputs);//Read Registers

GetRegistersByNModbus(inputs, startAddress);//Convert registers to float
private static void GetRegistersByNModbus(ushort inputs, ushort startAddress)
{
List<float> floatList = new List<float>();
for (int i = 0; i < inputs.Length;)
{
float temp = ModbusUtility.GetSingle(inputs[i++], inputs[i++]);
floatList.Add(temp);
}
float floatArray = floatList.ToArray();
int startReg = int.Parse(startAddress.ToString());
for (int i = 0, j = startReg; i < floatArray.Length; i++, startAddress += 2)
{
Console.WriteLine("Register {0}={1}", startAddress, floatArray[i]);
}
}









share|improve this question
























  • Without knowing more details, it is diffcult to guess, what's going wrong. When you have to read a float, something/somebody writes it before. Is there a documentation, how it is written? Did you test to exchange the loword and hiword? What is your source code to combine the two words into a float?
    – KBO
    Nov 23 '18 at 6:20












  • @KBO I'm using ReadHoldingRegisters() method to read holding registers from 0101 to 0131. After providing startAddress, Slave id and noofregisters to read, it reads 0101 to 0131 registers and provide its value in ushort as expected. But not desired. From 0101 to 0131 each register contains a float value thus ushort give 0 when 2.3e-007 is desired and 32767 when number is out of ushort range. 0101 to 0131 registers are getting their values(write) from automation software. We just need to access those values
    – Samrat Matte
    Nov 23 '18 at 6:25








  • 1




    Are the floats single-precision IEEE-754? Assuming so, you'd need to read in 2 ushorts, store them in 4 bytes, being careful to get the order correct, then use something like BitConverter.ToSingle() to translate them to a float. I assume you're already doing similar; can you add some actual non-working code you're using to your question?
    – sellotape
    Nov 23 '18 at 7:29










  • The comment by @sellotape should be the answer. You may have to try all possible combinations of arranging the two ushort values to the 4 byte array (swapping the words and event swapping the bytes within each word) as there is no universal agreement how to encode float in Modbus.
    – Klaus Gütter
    Nov 23 '18 at 7:32












  • @sellotape ALL my Code is working. And that's the problem. Because each register is 16-bit but assigned to 32-bit float values written by automation software. While reading each register at a time I'm getting those float values read as ushort. Hence, the above conversion problem. I've already implemented what you said about BitConvertor.ToSingle() but it will combine two successive registers which I don't want. I just want value of one register into float. Could reconfiguring registers in feed from Automation software to 32-bit from 16-bit get me anywhere?
    – Samrat Matte
    Nov 23 '18 at 7:56


















0














I'm using NModbus4 to read a 16-bit register which already has a float value in it. I'm getting this float value directly from respectively relevant software with registers configured for those float values.
This register configuration helps me to get recorded value in my console program using NModbus4.



Question is quite simple. Nmodbus returns ushort for Holding registers and I want to get a float value while reading from response.



Yes, I tried reading two 16-bit registers and fusing them to form float value(32-bit) and NO, I'm not getting desired results.



For example 2.3e-007 results to 0 and number larger than ushort datatype results into 32767. Maybe I need to change something in library(NModbus4). Just some hint would do the trick. Please can anybody help me?



Here is my code snippet:



TcpClient masterTcpClient = new TcpClient(address.ToString(), portNo);
ModbusIpMaster master = ModbusIpMaster.CreateIp(masterTcpClient);//Created master

ushort inputs = null;
inputs = master.ReadHoldingRegisters(slaveId, startAddress, numInputs);//Read Registers

GetRegistersByNModbus(inputs, startAddress);//Convert registers to float
private static void GetRegistersByNModbus(ushort inputs, ushort startAddress)
{
List<float> floatList = new List<float>();
for (int i = 0; i < inputs.Length;)
{
float temp = ModbusUtility.GetSingle(inputs[i++], inputs[i++]);
floatList.Add(temp);
}
float floatArray = floatList.ToArray();
int startReg = int.Parse(startAddress.ToString());
for (int i = 0, j = startReg; i < floatArray.Length; i++, startAddress += 2)
{
Console.WriteLine("Register {0}={1}", startAddress, floatArray[i]);
}
}









share|improve this question
























  • Without knowing more details, it is diffcult to guess, what's going wrong. When you have to read a float, something/somebody writes it before. Is there a documentation, how it is written? Did you test to exchange the loword and hiword? What is your source code to combine the two words into a float?
    – KBO
    Nov 23 '18 at 6:20












  • @KBO I'm using ReadHoldingRegisters() method to read holding registers from 0101 to 0131. After providing startAddress, Slave id and noofregisters to read, it reads 0101 to 0131 registers and provide its value in ushort as expected. But not desired. From 0101 to 0131 each register contains a float value thus ushort give 0 when 2.3e-007 is desired and 32767 when number is out of ushort range. 0101 to 0131 registers are getting their values(write) from automation software. We just need to access those values
    – Samrat Matte
    Nov 23 '18 at 6:25








  • 1




    Are the floats single-precision IEEE-754? Assuming so, you'd need to read in 2 ushorts, store them in 4 bytes, being careful to get the order correct, then use something like BitConverter.ToSingle() to translate them to a float. I assume you're already doing similar; can you add some actual non-working code you're using to your question?
    – sellotape
    Nov 23 '18 at 7:29










  • The comment by @sellotape should be the answer. You may have to try all possible combinations of arranging the two ushort values to the 4 byte array (swapping the words and event swapping the bytes within each word) as there is no universal agreement how to encode float in Modbus.
    – Klaus Gütter
    Nov 23 '18 at 7:32












  • @sellotape ALL my Code is working. And that's the problem. Because each register is 16-bit but assigned to 32-bit float values written by automation software. While reading each register at a time I'm getting those float values read as ushort. Hence, the above conversion problem. I've already implemented what you said about BitConvertor.ToSingle() but it will combine two successive registers which I don't want. I just want value of one register into float. Could reconfiguring registers in feed from Automation software to 32-bit from 16-bit get me anywhere?
    – Samrat Matte
    Nov 23 '18 at 7:56
















0












0








0







I'm using NModbus4 to read a 16-bit register which already has a float value in it. I'm getting this float value directly from respectively relevant software with registers configured for those float values.
This register configuration helps me to get recorded value in my console program using NModbus4.



Question is quite simple. Nmodbus returns ushort for Holding registers and I want to get a float value while reading from response.



Yes, I tried reading two 16-bit registers and fusing them to form float value(32-bit) and NO, I'm not getting desired results.



For example 2.3e-007 results to 0 and number larger than ushort datatype results into 32767. Maybe I need to change something in library(NModbus4). Just some hint would do the trick. Please can anybody help me?



Here is my code snippet:



TcpClient masterTcpClient = new TcpClient(address.ToString(), portNo);
ModbusIpMaster master = ModbusIpMaster.CreateIp(masterTcpClient);//Created master

ushort inputs = null;
inputs = master.ReadHoldingRegisters(slaveId, startAddress, numInputs);//Read Registers

GetRegistersByNModbus(inputs, startAddress);//Convert registers to float
private static void GetRegistersByNModbus(ushort inputs, ushort startAddress)
{
List<float> floatList = new List<float>();
for (int i = 0; i < inputs.Length;)
{
float temp = ModbusUtility.GetSingle(inputs[i++], inputs[i++]);
floatList.Add(temp);
}
float floatArray = floatList.ToArray();
int startReg = int.Parse(startAddress.ToString());
for (int i = 0, j = startReg; i < floatArray.Length; i++, startAddress += 2)
{
Console.WriteLine("Register {0}={1}", startAddress, floatArray[i]);
}
}









share|improve this question















I'm using NModbus4 to read a 16-bit register which already has a float value in it. I'm getting this float value directly from respectively relevant software with registers configured for those float values.
This register configuration helps me to get recorded value in my console program using NModbus4.



Question is quite simple. Nmodbus returns ushort for Holding registers and I want to get a float value while reading from response.



Yes, I tried reading two 16-bit registers and fusing them to form float value(32-bit) and NO, I'm not getting desired results.



For example 2.3e-007 results to 0 and number larger than ushort datatype results into 32767. Maybe I need to change something in library(NModbus4). Just some hint would do the trick. Please can anybody help me?



Here is my code snippet:



TcpClient masterTcpClient = new TcpClient(address.ToString(), portNo);
ModbusIpMaster master = ModbusIpMaster.CreateIp(masterTcpClient);//Created master

ushort inputs = null;
inputs = master.ReadHoldingRegisters(slaveId, startAddress, numInputs);//Read Registers

GetRegistersByNModbus(inputs, startAddress);//Convert registers to float
private static void GetRegistersByNModbus(ushort inputs, ushort startAddress)
{
List<float> floatList = new List<float>();
for (int i = 0; i < inputs.Length;)
{
float temp = ModbusUtility.GetSingle(inputs[i++], inputs[i++]);
floatList.Add(temp);
}
float floatArray = floatList.ToArray();
int startReg = int.Parse(startAddress.ToString());
for (int i = 0, j = startReg; i < floatArray.Length; i++, startAddress += 2)
{
Console.WriteLine("Register {0}={1}", startAddress, floatArray[i]);
}
}






c# modbus-tcp






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 9:56









piet.t

9,94263245




9,94263245










asked Nov 23 '18 at 6:04









Samrat Matte

284




284












  • Without knowing more details, it is diffcult to guess, what's going wrong. When you have to read a float, something/somebody writes it before. Is there a documentation, how it is written? Did you test to exchange the loword and hiword? What is your source code to combine the two words into a float?
    – KBO
    Nov 23 '18 at 6:20












  • @KBO I'm using ReadHoldingRegisters() method to read holding registers from 0101 to 0131. After providing startAddress, Slave id and noofregisters to read, it reads 0101 to 0131 registers and provide its value in ushort as expected. But not desired. From 0101 to 0131 each register contains a float value thus ushort give 0 when 2.3e-007 is desired and 32767 when number is out of ushort range. 0101 to 0131 registers are getting their values(write) from automation software. We just need to access those values
    – Samrat Matte
    Nov 23 '18 at 6:25








  • 1




    Are the floats single-precision IEEE-754? Assuming so, you'd need to read in 2 ushorts, store them in 4 bytes, being careful to get the order correct, then use something like BitConverter.ToSingle() to translate them to a float. I assume you're already doing similar; can you add some actual non-working code you're using to your question?
    – sellotape
    Nov 23 '18 at 7:29










  • The comment by @sellotape should be the answer. You may have to try all possible combinations of arranging the two ushort values to the 4 byte array (swapping the words and event swapping the bytes within each word) as there is no universal agreement how to encode float in Modbus.
    – Klaus Gütter
    Nov 23 '18 at 7:32












  • @sellotape ALL my Code is working. And that's the problem. Because each register is 16-bit but assigned to 32-bit float values written by automation software. While reading each register at a time I'm getting those float values read as ushort. Hence, the above conversion problem. I've already implemented what you said about BitConvertor.ToSingle() but it will combine two successive registers which I don't want. I just want value of one register into float. Could reconfiguring registers in feed from Automation software to 32-bit from 16-bit get me anywhere?
    – Samrat Matte
    Nov 23 '18 at 7:56




















  • Without knowing more details, it is diffcult to guess, what's going wrong. When you have to read a float, something/somebody writes it before. Is there a documentation, how it is written? Did you test to exchange the loword and hiword? What is your source code to combine the two words into a float?
    – KBO
    Nov 23 '18 at 6:20












  • @KBO I'm using ReadHoldingRegisters() method to read holding registers from 0101 to 0131. After providing startAddress, Slave id and noofregisters to read, it reads 0101 to 0131 registers and provide its value in ushort as expected. But not desired. From 0101 to 0131 each register contains a float value thus ushort give 0 when 2.3e-007 is desired and 32767 when number is out of ushort range. 0101 to 0131 registers are getting their values(write) from automation software. We just need to access those values
    – Samrat Matte
    Nov 23 '18 at 6:25








  • 1




    Are the floats single-precision IEEE-754? Assuming so, you'd need to read in 2 ushorts, store them in 4 bytes, being careful to get the order correct, then use something like BitConverter.ToSingle() to translate them to a float. I assume you're already doing similar; can you add some actual non-working code you're using to your question?
    – sellotape
    Nov 23 '18 at 7:29










  • The comment by @sellotape should be the answer. You may have to try all possible combinations of arranging the two ushort values to the 4 byte array (swapping the words and event swapping the bytes within each word) as there is no universal agreement how to encode float in Modbus.
    – Klaus Gütter
    Nov 23 '18 at 7:32












  • @sellotape ALL my Code is working. And that's the problem. Because each register is 16-bit but assigned to 32-bit float values written by automation software. While reading each register at a time I'm getting those float values read as ushort. Hence, the above conversion problem. I've already implemented what you said about BitConvertor.ToSingle() but it will combine two successive registers which I don't want. I just want value of one register into float. Could reconfiguring registers in feed from Automation software to 32-bit from 16-bit get me anywhere?
    – Samrat Matte
    Nov 23 '18 at 7:56


















Without knowing more details, it is diffcult to guess, what's going wrong. When you have to read a float, something/somebody writes it before. Is there a documentation, how it is written? Did you test to exchange the loword and hiword? What is your source code to combine the two words into a float?
– KBO
Nov 23 '18 at 6:20






Without knowing more details, it is diffcult to guess, what's going wrong. When you have to read a float, something/somebody writes it before. Is there a documentation, how it is written? Did you test to exchange the loword and hiword? What is your source code to combine the two words into a float?
– KBO
Nov 23 '18 at 6:20














@KBO I'm using ReadHoldingRegisters() method to read holding registers from 0101 to 0131. After providing startAddress, Slave id and noofregisters to read, it reads 0101 to 0131 registers and provide its value in ushort as expected. But not desired. From 0101 to 0131 each register contains a float value thus ushort give 0 when 2.3e-007 is desired and 32767 when number is out of ushort range. 0101 to 0131 registers are getting their values(write) from automation software. We just need to access those values
– Samrat Matte
Nov 23 '18 at 6:25






@KBO I'm using ReadHoldingRegisters() method to read holding registers from 0101 to 0131. After providing startAddress, Slave id and noofregisters to read, it reads 0101 to 0131 registers and provide its value in ushort as expected. But not desired. From 0101 to 0131 each register contains a float value thus ushort give 0 when 2.3e-007 is desired and 32767 when number is out of ushort range. 0101 to 0131 registers are getting their values(write) from automation software. We just need to access those values
– Samrat Matte
Nov 23 '18 at 6:25






1




1




Are the floats single-precision IEEE-754? Assuming so, you'd need to read in 2 ushorts, store them in 4 bytes, being careful to get the order correct, then use something like BitConverter.ToSingle() to translate them to a float. I assume you're already doing similar; can you add some actual non-working code you're using to your question?
– sellotape
Nov 23 '18 at 7:29




Are the floats single-precision IEEE-754? Assuming so, you'd need to read in 2 ushorts, store them in 4 bytes, being careful to get the order correct, then use something like BitConverter.ToSingle() to translate them to a float. I assume you're already doing similar; can you add some actual non-working code you're using to your question?
– sellotape
Nov 23 '18 at 7:29












The comment by @sellotape should be the answer. You may have to try all possible combinations of arranging the two ushort values to the 4 byte array (swapping the words and event swapping the bytes within each word) as there is no universal agreement how to encode float in Modbus.
– Klaus Gütter
Nov 23 '18 at 7:32






The comment by @sellotape should be the answer. You may have to try all possible combinations of arranging the two ushort values to the 4 byte array (swapping the words and event swapping the bytes within each word) as there is no universal agreement how to encode float in Modbus.
– Klaus Gütter
Nov 23 '18 at 7:32














@sellotape ALL my Code is working. And that's the problem. Because each register is 16-bit but assigned to 32-bit float values written by automation software. While reading each register at a time I'm getting those float values read as ushort. Hence, the above conversion problem. I've already implemented what you said about BitConvertor.ToSingle() but it will combine two successive registers which I don't want. I just want value of one register into float. Could reconfiguring registers in feed from Automation software to 32-bit from 16-bit get me anywhere?
– Samrat Matte
Nov 23 '18 at 7:56






@sellotape ALL my Code is working. And that's the problem. Because each register is 16-bit but assigned to 32-bit float values written by automation software. While reading each register at a time I'm getting those float values read as ushort. Hence, the above conversion problem. I've already implemented what you said about BitConvertor.ToSingle() but it will combine two successive registers which I don't want. I just want value of one register into float. Could reconfiguring registers in feed from Automation software to 32-bit from 16-bit get me anywhere?
– Samrat Matte
Nov 23 '18 at 7:56














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%2f53441372%2fhow-to-read-a-16-bit-register-if-im-getting-float-value-in-it-using-nmodbus4%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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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%2f53441372%2fhow-to-read-a-16-bit-register-if-im-getting-float-value-in-it-using-nmodbus4%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







Popular posts from this blog

What visual should I use to simply compare current year value vs last year in Power BI desktop

Alexandru Averescu

Trompette piccolo