CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

modbus rtu data calculation

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
raman00084@gmail.com



Joined: 28 Jul 2014
Posts: 38

View user's profile Send private message

modbus rtu data calculation
PostPosted: Wed Dec 30, 2020 5:56 am     Reply with quote

Dear friends,
I am new to modbus rtu. I am getting the following data. How to compute the same ?

01 03 04 (86 9F 00 01) 22 95 = 99999
(86 9F 00 01) this is the data. How can i compute to get 99999 ?

01 03 04 (79 61 FF FE )72 C1 = -99999
(79 61 FF FE ) this is the data. How can i compute to get -99999 ?

01 03 04 (40 00 44 0D) 1D 36 = 7894.567
(40 00 44 0D) this is the data. How can i compute to get 7894.567 ?

kindly help me.
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Wed Dec 30, 2020 8:09 am     Reply with quote

The first two are easy. They are just integer values. So all you would need
to do is write the bytes in the right order into a CCS signed int32 value,
and it'll contain the result:
Code:

union {
   unsigned int8 bytes[4];
   signed int32 word;
} combiner;

//combiner.bytes[0] = second byte from value
//combiner.bytes[1] = first byte from value
//combiner.bytes[2] = fourth byte from value
//combiner.bytes[3] = third byte from value

//so

combiner.bytes[0] = 0x9F;
combiner.bytes[1] = 0x86;
combiner.bytes[2] = 0x1;
combiner.bytes[3] = 0x00;

//Then
combiner.word //Will be 99999

The third is problematic. Modbus _does not define any way to send float
values
_. Float transmission is 'customer specific'. You need to know
how this value has been encoded. The normal way to do it would be
IEE 754. However the value you post is not in this format. Likely alternatives
are that in fact the value is being sent as perhaps an integer in some small
fraction. However I can't see an obvious multiple to give this.
You need data from whoever is generating this value as to the format
they are using. Otherwise it is going to be a difficult task (and you would
need a lot more values), to work out how they are coded.
raman00084@gmail.com



Joined: 28 Jul 2014
Posts: 38

View user's profile Send private message

PostPosted: Wed Dec 30, 2020 10:31 pm     Reply with quote

Thanks for your code it is working.
For floating point value i got some more values:
Rx:000001-01 03 04 (4F 80 47 C3) 9E AE = 99999.000
Rx:000003-01 03 04 (44 9C C2 6A) FF A2 = -58.567
Rx:000005-01 03 04 (33 33 41 43 )74 D9 = 12.200
Rx:000007-01 03 04 (E6 B6 46 40 )1F 0D = 12345.678

Can you guess what type of encoding they are using ?
I am using Weintek HMI. From that only i am getting the data.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Dec 31, 2020 2:01 am     Reply with quote

It's IEEE-754 32-bit single precision floating point format.
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Thu Dec 31, 2020 2:15 am     Reply with quote

OK. Well those are in standard IEEE format. So it suggests that the other
value may have been wrong. The bytes you gave decode to 565.0 using
standard IEEE encoding....

Question. What PIC are you on?. The answer changes if you are on a
PIC24/30/33.
For a PIC16/18:

Code:

include "IEEEfloat.c"

union {
   unsigned int8 bytes[4];
   signed int32 word;
   unsigned int32 uword;
} combiner;
float floatval;

//combiner.bytes[0] = second byte from value
//combiner.bytes[1] = first byte from value
//combiner.bytes[2] = fourth byte from value
//combiner.bytes[3] = third byte from value

//so

combiner.bytes[0] = 0x80;
combiner.bytes[1] = 0x4F;
combiner.bytes[2] = 0xC3;
combiner.bytes[3] = 0x47;
//for your first 99999.00 number

//Then
floatval=f_IEEEtoPIC(combiner.uword);

//will give the float value


On the PIC24/30/33, the float format is already IEEE, so all that is needed
is a fourth 'float' value in the combiner, and this will contain the float result.
raman00084@gmail.com



Joined: 28 Jul 2014
Posts: 38

View user's profile Send private message

PostPosted: Thu Dec 31, 2020 4:38 am     Reply with quote

I am using dspic 33ep512mu810.
Float format is IEEE-754.
Quote:

union {
unsigned int8 bytes[4];
signed int32 word;
unsigned int32 uword;
} combiner;
float floatval;

//combiner.bytes[0] = second byte from value
//combiner.bytes[1] = first byte from value
//combiner.bytes[2] = fourth byte from value
//combiner.bytes[3] = third byte from value
so

while(true)
{
combiner.bytes[0] = 0x80;
combiner.bytes[1] = 0x4F;
combiner.bytes[2] = 0xC3;
combiner.bytes[3] = 0x47;
//for your first 99999.00 number

//Then
floatval=f_IEEEtoPIC(combiner.uword);
lcd_gotoxy(1, 1);
printf(lcd_putc1, "%f",floatval);
//will give the float value
}

The result I am getting in lcd is = 2676363008.00.
This value is wrong!
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Thu Dec 31, 2020 4:44 am     Reply with quote

It would be. Read what I said for the PIC24/33.
All you need for this is:
Code:

//include "IEEEfloat.c" Not needed for this on DsPIC

union {
   unsigned int8 bytes[4];
   signed int32 word;
   unsigned int32 uword;
   float32 floatval;
} combiner;

//combiner.bytes[0] = second byte from value
//combiner.bytes[1] = first byte from value
//combiner.bytes[2] = fourth byte from value
//combiner.bytes[3] = third byte from value

//so

combiner.bytes[0] = 0x80;
combiner.bytes[1] = 0x4F;
combiner.bytes[2] = 0xC3;
combiner.bytes[3] = 0x47;
//for your first 99999.00 number

//Then
combiner.floatval //is then the result.

//will give the float value


You are taking an already IEEE value and converting it to the CCS/
Microchip format, when your chip uses IEEE....
raman00084@gmail.com



Joined: 28 Jul 2014
Posts: 38

View user's profile Send private message

PostPosted: Thu Dec 31, 2020 6:18 am     Reply with quote

Sorry for the confusion bro.
Now it is working correctly.
Thank you.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group