Beijer Electronics (formerly QSI Corporation)

Manufacturer of Mobile Data and Human Machine Interface Terminals.
It is currently Fri Nov 24, 2017 6:12 am

All times are UTC - 7 hours




Post new topic Reply to topic  [ 11 posts ] 
Author Message
 Post subject: Modbus 32 bit integers
PostPosted: Thu Jan 21, 2010 11:17 am 
Offline

Joined: Thu Jan 21, 2010 11:04 am
Posts: 9
Quick question about the use of reading a 32 bit integer from a modbus slave, it seems to show the wrong value. Almost like the value is being rounded off on the low byte.


Top
 Profile  
 
PostPosted: Thu Jan 21, 2010 11:29 am 
Offline
User avatar

Joined: Thu Mar 02, 2006 2:12 pm
Posts: 487
Location: Salt Lake City, Utah
I'm going to assume your using an MBRegister object or similar to read the value. Here are some properties you can check or try changing.
1. On the MBRegister object verify the vartype is set to mb_int32.
2. You may want to try changing the register property in case your address if off by 1.
You'll also want to look at some properties on the ModbusComm object.
3. Make sure your wordsize is set to mb_16bit. It is very rare for the wordsize to be different that 16-bit.
4. Try toggling the swapword property to see if it makes any difference.

_________________
Ron L.

http://www.beijerelectronicsinc.com/


Top
 Profile  
 
PostPosted: Thu Jan 21, 2010 11:39 am 
Offline

Joined: Thu Jan 21, 2010 11:04 am
Posts: 9
ok, so to confirm, the ModbusComm object is set to 16bit, the VirtualRegister is set for 32 bit int. In my modbus simulator i have register 1 set for as 0xDEAD and register 2 is 0xBEEF, when i read the registers individually as 16 bit and print them to a label in hex format they read normally, but when i read as a 32bit i get BEEFDE80 and using swapword i get DEADBF00.


Top
 Profile  
 
PostPosted: Thu Jan 21, 2010 11:50 am 
Offline
User avatar

Joined: Thu Mar 02, 2006 2:12 pm
Posts: 487
Location: Salt Lake City, Utah
Are you using the signed or unsigned 16 bit? The conversion problem could be that your expecting the 32bit to convert as an un-signed 32bit would. To get the exact hex your looking for from a 32-bit signed, you may need to use GetBytes to convert the 32bit integer to 4 bytes, then use FromBytes function to convert the 4 bytes to two unsigned ints (unibytes in Qlarity). Then convert the two unibytes to hex using the function _FormatAsHex.

_________________
Ron L.

http://www.beijerelectronicsinc.com/


Top
 Profile  
 
PostPosted: Thu Jan 21, 2010 11:55 am 
Offline
QSI Support
QSI Support
User avatar

Joined: Wed Mar 08, 2006 12:25 pm
Posts: 881
Location: Salt Lake City, Utah
If you can simplify this to a workspace with a single register object that queries those two consecutive registers, it might be helpful if you can post a screen shot of your Simulation View Communication Window (Be sure to turn on Hexidecimal display first).

_________________
Jeremy
http://www.beijerinc.com


Top
 Profile  
 
PostPosted: Thu Jan 21, 2010 1:23 pm 
Offline

Joined: Thu Jan 21, 2010 11:04 am
Posts: 9
Ok here is a screen shot.
ModbusComm is set for 16bit registers, swapword is false
Virtual Registers are set accordingly as Int16's and a Int32.
Valuechanged code in each Virtual Register is "Label1.value = _FormatAsHex(value,8,true)"


Attachments:
Qlarity_Error1.gif
Qlarity_Error1.gif [ 20.68 KiB | Viewed 2677 times ]
Top
 Profile  
 
PostPosted: Thu Jan 21, 2010 1:32 pm 
Offline

Joined: Thu Jan 21, 2010 11:04 am
Posts: 9
Sorry about that here is the proper picture.


Attachments:
Qlarity_Error2.gif
Qlarity_Error2.gif [ 95.28 KiB | Viewed 2678 times ]
Top
 Profile  
 
PostPosted: Thu Jan 21, 2010 3:09 pm 
Offline
User avatar

Joined: Thu Mar 02, 2006 2:12 pm
Posts: 487
Location: Salt Lake City, Utah
I don't see any problems with the data going back and forth. It's as I would expect. I'm probably going to need to look at your workspace to help.

_________________
Ron L.

http://www.beijerelectronicsinc.com/


Top
 Profile  
 
PostPosted: Thu Jan 21, 2010 3:18 pm 
Offline

Joined: Thu Jan 21, 2010 11:04 am
Posts: 9
Yes the data looks fine but what is being displayed on the label is not.


Attachments:
integererror.qly [6.83 KiB]
Downloaded 152 times
Top
 Profile  
 
PostPosted: Thu Jan 21, 2010 3:49 pm 
Offline
User avatar

Joined: Thu Mar 02, 2006 2:12 pm
Posts: 487
Location: Salt Lake City, Utah
I've reproduced the problem. To fix, change this code:

Code:
Data_32_400001.value = _FormatAsHex(value,8,true)


to

Code:
Data_32_400001.value = _FormatAsHex(intvalue,8,true)


You were losing precision when casting from a float32 which is what the property value is. To avoid this, use the intvalue property instead.

_________________
Ron L.

http://www.beijerelectronicsinc.com/


Top
 Profile  
 
PostPosted: Thu Jan 21, 2010 4:04 pm 
Offline

Joined: Thu Jan 21, 2010 11:04 am
Posts: 9
Ah i see, Thank you guys for the excellent support on this.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC - 7 hours


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 post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group