Code for reading 4 address to get 1 value of sensor

Hi all,
I want to ask, how do I write code to read multiple address ? Because this multiple address will produce one reading of a sensor that I am currently using. I want to read the value of the sensor.


The addresses are in Byte. The 4 addresses are 89,90,91,92. Thanks in advance.

Best regards,
Umair

Hi Umair,

u can use an UNION DUT for this.

  1. You have to add an DUT
    image
  2. Fill it with your data structures, it is important to know tha every Variable in this union shares the same datastorage. for example you can write your REAL value, by accessing the array of bytes.
    image
  3. Instanciate the union in you POU (PLC_PRG, … ) and write your values. Sometimes you have to change the write order of the array, to change Byteorder of the REAL value.
    image
4 Likes

Hi Michael,

is myValue.rOutputData suppose to be the value of the sensor?

Hi Umair,

Exaclty. But i guess you have to change the Byte Order. Because some Comanies work with intel byte order and some with motorola.
Therefore you can just switch the bytes like this, until you have an plausible value:
abInputData[0] := %IB78;
abInputData[1] := %IB77;
abInputData[2] := %IB80;
abInputData[3] := %IB79;

best regards
Michael

Two options:

  1. This is the correct value
  2. Try the order:
    abInputData[1]:= %IB78;
    abInputData[0]:= %IB77;
    abInputData[3]:= %IB80;
    abInputData[2]:= %IB79;

Hi Michael,

what if I want to read 2 value of sensor where another sensor need to read another 4 address. Do I have to create new UNION DUT? And also create new same variable that I have created for the 1st sensor? Sorry for asking too much.

Best regards,
Umair

Hi Tomas,

this is the result that i get after using that particular order. myValue.rOutputData is the value of the sensor right?

What sensor do you have? What does it measure? Maybe there is documentation for it?

You’re Welcome, you just can make an second instance of the same datatype.
Which will look like this:
myValue1 : U_BYTE_CONVERT;
myValue2 : U_BYTE_CONVERT;

and so on, so you can map the different values.
For Example the mapping would look like this:

myValue1.abInputData[0] := %IB0;
myValue1.abInputData[1] := %IB1;
myValue1.abInputData[2] := %IB2;
myValue1.abInputData[3] := %IB3;

myValue2.abInputData[0] := %IB4;
myValue2.abInputData[1] := %IB5;
myValue2.abInputData[2] := %IB6;
myValue2.abInputData[3] := %IB7;

Its a thickness measurement sensor, i think now i need to verify the PLC’s value with the actual value from the sensor. Thank you for your support!

1 Like

Great, will try that. Thanks a lot for the guidance!

1 Like

Hi Michael,

another question I would like to ask. What if theres 2 sensor? where both first and second sensor are the same sensor and going to read the same address. How do I differentiate between these two if I want to read values from both sensors? Create new UNION DUT?

Best regards,
Umair

Umair,

When adding additional sensors, create a new unique variable name (ex: abInputData2) for the bytes so they are not the same address in the PLC.

And then, yes, create a new UNION DUT.

Regards,

Hi Jacob,

but how do i determine that this Union DUT is for this sensor 1 with IP address x.x.x.10 and this Union DUT is for sensor 2 with IP address x.x.x.9 for example.

You would need to add another device to the EthernetIP Scanner, and configure it the same as the first one.

Please continue to use this thread and not open new threads for the same support issue :slight_smile:

Great, thanks for the suggestion. Sure, I will continue the threads here. Sorry for that.

Hi,
For example below in the picture, this is the code for 1st sensor (IP210) at PLC_PRG. How can I write the code at PLC_PRG for 2nd sensor (IP209) . Both of the sensor have the same address that need to be read, but how do I write the code for another sensor since the code does not declare any IP address.

Best regards,
Umair