Copy Word to Struct - TIA Portal

ASF

Lifetime Supporting Member
Join Date
Jun 2012
Location
Australia
Posts
3,921
Hi all,

Straight up question: I'm using TIA Portal and an S7-1200, and wanting to do a straight bitwise copy of all the data in a Word to put it into a Struct (which consists of a byte and 8 bools). Is there a way to do this? I've tried all the instructions that I can think of, but they all give me data type mismatches.

Context (in case there's a better way to approach what I'm doing): I'm getting a 16-bit integer coming in from a Modbus TCP device, and this particular status word contains:
- Bit 0-7: Value 1
- Bit 8-12: Value 2
- Bits 13, 14 and 15: status bits
I can bring it straight into a Struct tag of one byte and 8 bools, but then I have no easy way of re-assembling the value from Bits 8-12 back into a meaningful value. So I decided to bring it into an Int. Then I can use AND's and bit shift instructions to extract Value 1 and Value 2, and all I have to do then is extract the Bool's.

I can think of two ways to do this:

1. Address the bit within the Int: e.g. just use %DB331.DBX2.13 to turn on a coil. That coil becomes my bool. This works, but gives me IEC warnings

2. Move the Int into an M word (e.g. MW100) and then address (e.g.) M100.5 to get my Bool. Which also works

And while they both work, I'm thinking it would still be much simpler if I could just do a straight copy. Is it possible, or should I just pick one of my two options above and move on?

Thanks!
 
I've just worked out that I can use a MOVE instruction if I specify the word in the DB by it's offset, rather than it's name (MOVE DB331.DBW6 to DB331.DBW18), but again, I get an IEC warning. So if there's a more elegant solution I'm eager to hear it :)
 
As far as I know, Siemens doesn't have any 5 bit data types, so your structure couldn't match up exactly. I don't think a straight copy is possible.

One other way to address the values within the tag in the 1200 is to use what they call "slice" addressing. If you address the memory location by name, you can append ".B0" to mean the least significant byte, ".X3" to grab the 4th bit (counting from 0), etc. So you could copy out the first value from "Datablock.tagname.B0", to do it symbolically, and not have to deal with the offsets. You could use .X15, or possibly .B1.X7 to grab one of the status bits at the end.

The above might avoid the IEC error, but you still have the same issues with the 5 byte value. In the end, you still have to do something like one of the ideas you already had.
 
Thanks for the response. I'm currently using the ".X3" to grab the bool's individually (DB331.DBX1.3), and it works, but still gives me the IEC error.

As far as the five bit data type, that's not an issue - I just get the whole Int and run it through an AND and a Bit Shift, and store it in another Int. Now I just want to try and grab the bool's - preferably without generating an IEC error :)
 
Aha, that works nicely!

Just to dot the i's and cross the t's, the addressing format is:

[DB Name].[Element Name].x[Bit Number]

e.g. Weight_TX_Data.Status_Word.x13 where Weight_TX_Data is the DB, Status_Word is a Word tag in the DB, and I'm addressing bit 13 of Status_Word.

Thanks!
 
Yes, that's correct! The optimized datablocks are overall better to use so try to use optimized rather then the non optimized. Sometimes you must use the non-optimized for communication etc as you might know :)
 
But what if one wants to do:
Weight_TX_Data.Status_Word.Enabled
In stead of:
Weight_TX_Data.Status_Word.X13
 
Ah, I don't think you can do that since we only "slice" the word into bits in the ladder. The bits we access isn't specified anywhere in any DB. At least as far as I know.
 
I haven't used TIA portal much but does it not support UDTs?

The conventional way to handle this would be to create a UDT with the structure that corresponds to the slave data and then transfer said data into an instance of the UDT.

Nick
 
Jeebs, what rQx said is correct - the Status tag is an Int, so I can't give each bit a symbolic name. I could make it a STRUCT tag with individual bools, and do that, but then I'm back to my previous problem of getting 5 Bool's packed back into an Int (see original post)

Manglemender - yes, I could create a UDT, but then I'm still stuck in the same place - how do I get my data into it? The biggest problem here was not that I need to extract 16 bits out of an Int - if it were 16 individual bits, I'd have just created a Struct tag with 16 bools, brought the data straight into there, and I'd already be at the pub. The problem is because I have 16 bits of information where the first 8 bits are to be read as a decimal value, the next 5 bits are to be read as a decimal value, and then the next three bits are to be read as individual bools. As mk42 said, there's no 5-bit data type in Siemens, so I can't create a UDT to fit them all into the right place.

In any case, with the information in posts 5-7, I've got what I needed. Thanks everyone!
 
Last edited:
I was thinking POKE_BLK, it doesn't care about data types, but I don't know if it would be acceptable under IEC'what do you call it'.
 

Similar Topics

This feels like a simple, elementary question, but I am a Siemens novice and know there are experts here. My S7-1500 project (TIA Portal v17) has...
Replies
10
Views
2,713
Hi, I'm still at the beginning of my journey with TIA, and am doing something for which I'm sure there must be a better way than writing each bit...
Replies
7
Views
2,741
Good evening Hope I can get a little help with this one :) When I scroll true my DB and find a certain value, I would like to copy the actual...
Replies
5
Views
2,328
Hi! I use the following code (from FC) to copy words between DBs given an word offset address and nbr of words to copy. But I found out it fails...
Replies
3
Views
2,865
Hi, I'm starting with the ladder logic programming and I want to enssure if the following bitwise operation is correct or not. I want to...
Replies
2
Views
1,708
Back
Top Bottom