3/16/2010 6:28 PM | |
Joined: 1/17/2007 Last visit: 3/30/2023 Posts: 1523 Rating:
|
By the way welcome to the world of SCL. I am also a 'c' programmer that has had to start programming in SCL. It is quite a rude awakening how limited the compiler is. But hey ho, it is what it is. You are correct - you cannot have an ANY pointer in a structure. But there is a solution. You can store a dissassembled ANY pointer in the structure. You can then modify the dissassembled ANY pointer members to build an ANY pointer that points to what / where you like and then use it. An ANY pointer is not like a pointer in 'c'. It is best described as a memory record. It contains details about the address, type, DB number, memory area and size to access. The best way I have found is to make a UDT type that is a your dissassembled ANY pointer thus:- [code]TYPE ANYPOINTER TITLE = DISSASEMBLED ANYPOINTER VERSION : 1.0 AUTHOR : 'ME' NAME : 'ANYPTR' FAMILY : 'CODE' STRUCT S7Code : BYTE; // Code for S7 = 0x10 DataType : BYTE; // Code for data type = 0x02 = byte Length : INT; // Repetition factor = Send/receive length DBNumber : INT; // Data block Number MemoryArea : BYTE; // Specified memory area = 0x84 = data block ByteAddressMSB : BYTE; // Byte address most significant bits ByteAddressLSB : WORD; // Byte address least significant bits END_STRUCT; END_TYPE[/code] Then you can add this to your structure thus:- [code]SMIFFY : STRUCT pAny: ANYPOINTER; END_STRUCT;[/code] What you need then is a way of working out the DB address thus in your TEMP declaratrion area:- [code]u1Addr : DWORD; // Union pointer for calculation of byte offset address u1 AT u1Addr: STRUCT // This is equivalent to a union in 'c'. A dword goes in and word / byte LSB / byte MSB come out ByteAddrLSBPad : BYTE; // Not Used ByteAddrMSB : BYTE; // Byte address most significant bits WordAddr : WORD; // Byte address least significant bits END_STRUCT; [/code] You then use 'union' in your code by setting the DB offset address into u1addr and reading out the u1.ByteAddrMSB and u1.WordAddr values and putting them into the corresponding dissassembled ANY pointers address members ByteAddressMSB and ByteAddressLSB. Note the the DB offset address is the offset into the DB memory (starting at zero) and is bit based. That is a value of 0 (zero) addresses the first byte, a value of 8 (eight) addresses the second byte etc. So in you code you would use it like this (where <byte address> is your required byte address offset into the DB memory and <db number> is the number of your shared DB):- [code]SMIFFY.pAny.S7Code := 16#10; SMIFFY.pAny.DataType := 16#02; // For byte access SMIFFY.pAny.MemoryArea := 16#84; // For shared DB access SMIFFY.pAny.DBNumber := <db number>; // This is the shared DB number SMIFFY.pAny.Length := 1; // This is the ammount of data addressed - e.g. the ammount of data copied is using BLKMOV for instance u1Addr := DINT_TO_DWORD(<byte address> * 8); // Set the required DB offset address SMIFFY.pAny.ByteAddressMSB := u1.ByteAddrMSB; // Store the DB offset address in the dissassembled ANY pointer SMIFFY.pAny.ByteAddressLSB := u1.WordAddr;[/code] You would then call your FC passing in your structure (SMIFFY in this case). Then in your function, you would re-assemble the dissassembled ANY pointer so that you can use it. Firstly you would need an ANY pointer and make it so you can manipulate it via the dissassembled ANY pointer thus in your TEMP decalration area:- [code]pDB : ANY; // Pointer for receive data DBAny AT pDB : ANYPOINTER; // ANY pointer data structure [/code] You would then use it in your code thus (where <dap fc input param> is your dissassembled ANY pointer structure input parameter name in your FC declaration:- [code] DBAny := <dap fc input param>; // This copies over dissassembled ANY pointer structure from the caller to the local dissassembled ANY pointer in the FC [/code] Then you can use the pDB ANY pointer to access the DB data e.g. by say using BLKMOV etc. The type, memory area, length etc. can be tailored to your specific requirements. I have explained it all rather nicely here Hope that all makes sense! |
Programming today is the race between software engineers building bigger and better idiot proof programs, and the universe producing bigger and better idiots. |
|
6/22/2015 6:49 PM | |
Posts: 160 Rating:
|
Hi Smiffy, thank you very much for clearing all doubts! Janko |
Follow us on