Welcome, Guest
Username: Password: Remember me

TOPIC: how to swap word order on ChanAi?

how to swap word order on ChanAi? 5 years 5 months ago #9923

  • barnes
  • barnes's Avatar
  • OFFLINE
  • Expert Boarder
  • Posts: 92
  • Karma: 0
I have a modbus slave that is reading float32 values directly from memory. The 16 bit WORD order is reversed. I verified that the word order is opposite of what is being read into with the ChanAi.

How do I change the word order in the ChanAi Float32?

I verified this by writing a small c code running in the Proview PLC. I dont want to use this for every ChanAi.
Last Edit: 5 years 5 months ago by barnes.
The administrator has disabled public write access.

how to swap word order on ChanAi? 5 years 5 months ago #9924

  • claes
  • claes's Avatar
  • OFFLINE
  • Platinum Boarder
  • Posts: 3176
  • Thank you received: 502
  • Karma: 133
For other field busses it's possible to configure the slave endianess, but the Modbus network format is standardized to big endian so there is no such option for Modbus. As x86 is little endian all the 16-bit registers are swapped. If your slave doesn't follow the standard you have to read the channel with an ChanIi and swap back yourself in a CArithm.

/Claes
The administrator has disabled public write access.

how to swap word order on ChanAi? 5 years 5 months ago #9927

  • barnes
  • barnes's Avatar
  • OFFLINE
  • Expert Boarder
  • Posts: 92
  • Karma: 0
This is very interesting as I have a brand new Modicon with little endian Float32 byte order. I am investigating this further. Something is not quite right.

I wrote a small PLC code to put a native little endian Float32 in two 16 bit registers then swapped them around until proview read them correctly with the ChanAi.


LittleEndian______BigEndian_______ProviewEndian

Bytes A,B.C.D
A__________________ D ________________C
B___________________C ________________D
C__________________ B_________________A
D __________________A_________________B


The last column was read correctly. Another Koyo PLC has the ProviewEndian native also. So it appears as though people are not following the standard.
Last Edit: 5 years 5 months ago by barnes.
The administrator has disabled public write access.

how to swap word order on ChanAi? 5 years 5 months ago #9928

  • claes
  • claes's Avatar
  • OFFLINE
  • Platinum Boarder
  • Posts: 3176
  • Thank you received: 502
  • Karma: 133
How many bytes are swapped depends on the number of bits in Representation. If representation is Int16 2 bytes are swapped as in your example. If representation is Int32 four bytes are swapped.

/Claes
The administrator has disabled public write access.

how to swap word order on ChanAi? 5 years 5 months ago #9933

  • brunad
  • brunad's Avatar
  • OFFLINE
  • Gold Boarder
  • Posts: 247
  • Thank you received: 48
  • Karma: 11
Claes is right (as usual). ;)

Here is a sample code you can put in a CArithm object to reorder endian as you like (for a float):

union {
float f;
unsigned char b[4];
} dat1, dat2;

dat1.f = A1; // CArithm float input

dat2.b[0] = dat1.b[3]; // change order as you like
dat2.b[1] = dat1.b[2];
dat2.b[2] = dat1.b[1];
dat2.b[3] = dat1.b[0];

OA1 = dat2.f; // CArithm float output

}

/Bruno
The administrator has disabled public write access.

how to swap word order on ChanAi? 5 years 5 months ago #9934

  • barnes
  • barnes's Avatar
  • OFFLINE
  • Expert Boarder
  • Posts: 92
  • Karma: 0
Thanks for the replies,

Its way too much work to put a CArithm on every float. I have 200 Float32 values to deal with.

This belongs in the Modbus_TCP_Slave object or the Modbus_Module object.

I am working on another hack to make this happen.
The administrator has disabled public write access.
Time to create page: 7.775 seconds