I use snap7 to poll 4 data blocks of my Siemens S7 1200 PLC. It's works(with help of David) at the beginning. I declare 4 S7 Client and I store buffer into 4 arrays of bytes. After I parse buffer and store each variable. I use Thread to do this job with 20ms of delay.
However, one of S7 Client disconnects after few minutes and I don't have true values.
I want to know why one of client disconnects after approximately 30 minutes.
Thanks
Best regards
B.M
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
In this case you can use Cli_ReadMultiVars() that can transfer up to 20 vars in the same telegram.
The drawback (it's a S7 protocol limit) is that the whole size must fit in a single PDU including the header.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
OK
What do you mean about 20 vars (how many bytes?).What is the max PDU size for Siemens S71200 CPU 1214 PLC.Therefore i must use ReadMultiVars if i want to poll 4 DB simultaneously?
THX
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Where PDULength is the size negotiated during the connection, you can obtain it via Cli_GetPduLength() (ref man pag.163) and NumItems is the number of blocks read/wrote.
You can read up to 20 different DB at the same time if the total size is less or equal to the above value.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I had to modify ReadMultiVars in snap7.net.cs to to get it to work.
Instead of taking a ref to S7DataItem it takes an IntPtr to the first element of an array (that has been marshalled into unmanaged memory).
[DllImport(S7Consts.Snap7LibName)]protected static extern int Cli_ReadMultiVars(IntPtr Client, IntPtr Item, int ItemsCount);public int ReadMultiVars(IntPtr Item, int ItemsCount){return Cli_ReadMultiVars(Client, Item, ItemsCount);}
I also added this method to marshal the array back and forth.
public int ReadMultiVars(ref S7DataItem[] Items)
{
int iCount = Items.Length;
int iSize = Marshal.SizeOf(typeof(Snap7.S7Client.S7DataItem));
IntPtr pItems = Marshal.AllocHGlobal(iSize * iCount);
for (int i = 0; i < iCount; i++)
{
var pItem_i = new IntPtr(pItems.ToInt64() + (i * iSize));
Marshal.StructureToPtr(Items[i], pItem_i, false);
}
var result = this.ReadMultiVars(pItems, iCount);
for (int i = 0; i < iCount; i++)
{
var pItem_i = new IntPtr(pItems.ToInt64() + (i * iSize));
Items[i] = (S7DataItem)(Marshal.PtrToStructure(pItems, typeof(S7DataItem)));
}
Marshal.FreeHGlobal(pItems);
return result;
}
This is the first time I have ever done any work with unmanaged code so please review these changes for yourself before using them.
With this code I can read one int and two reals at a rate of 190 to 200 times per second from S7-319 CPU. This is reading from a Data Block.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
1) Modify ReadMultiVars and WriteMultiVars in snap7.net.cs:
[DllImport(S7Consts.Snap7LibName)]
protected static extern int Cli_ReadMultiVars(IntPtr Client, S7DataItem[] Items, int ItemsCount);
public int ReadMultiVars(S7DataItem[] Items, int ItemsCount)
{
return Cli_ReadMultiVars(Client, Items, ItemsCount);
}
[DllImport(S7Consts.Snap7LibName)]
protected static extern int Cli_WriteMultiVars(IntPtr Client, S7DataItem[] Items, int ItemsCount);
public int WriteMultiVars(S7DataItem[] Items, int ItemsCount)
{
return Cli_WriteMultiVars(Client, Items, ItemsCount);
}
Hello everybody,
I use snap7 to poll 4 data blocks of my Siemens S7 1200 PLC. It's works(with help of David) at the beginning. I declare 4 S7 Client and I store buffer into 4 arrays of bytes. After I parse buffer and store each variable. I use Thread to do this job with 20ms of delay.
However, one of S7 Client disconnects after few minutes and I don't have true values.
I want to know why one of client disconnects after approximately 30 minutes.
Thanks
Best regards
B.M
With S7 protocol you are using get/put :
https://support.automation.siemens.com/WW/llisapi.dll?func=cslib.csinfo&lang=en&objid=38051505&caller=view
OK.How can I use snap7 for pooling 4 DB at same moment? Are there function or methods for do this?
Thanks
How much is the total size that you need to transfer ?
for DB1 ---> 4 bytes
DB2 ---> 4 bytes
DB3 ----> 140 bytes
DB4 -----> 40 bytes
At total 188 bytes
In this case you can use Cli_ReadMultiVars() that can transfer up to 20 vars in the same telegram.
The drawback (it's a S7 protocol limit) is that the whole size must fit in a single PDU including the header.
OK
What do you mean about 20 vars (how many bytes?).What is the max PDU size for Siemens S71200 CPU 1214 PLC.Therefore i must use ReadMultiVars if i want to poll 4 DB simultaneously?
THX
A var is a Tag, i.e. a block of byte/word/dword, the same that you transfer via Cli_ReadArea() function.
The maximum payload is:
ReadMultiVars:
MaxPayloadSize=PDULength-(14+4*NumItems)
WriteMultiVars:
MaxPayloadSize=PDULength-(12+16*NumItems)
Where PDULength is the size negotiated during the connection, you can obtain it via Cli_GetPduLength() (ref man pag.163) and NumItems is the number of blocks read/wrote.
You can read up to 20 different DB at the same time if the total size is less or equal to the above value.
hello
I use ReadMultiVars function like this in C#:
I have an error with items[0].pData, I pass address but I have an error
THX
I use GCHandle for passing byte array address but I have another problem with myclient.ReadMultiVars(items, 4).It only take reference as argument.
I had to modify ReadMultiVars in snap7.net.cs to to get it to work.
Instead of taking a ref to S7DataItem it takes an IntPtr to the first element of an array (that has been marshalled into unmanaged memory).
I also added this method to marshal the array back and forth.
This is the first time I have ever done any work with unmanaged code so please review these changes for yourself before using them.
With this code I can read one int and two reals at a rate of 190 to 200 times per second from S7-319 CPU. This is reading from a Data Block.
Hi LanceL,
thanks for the contribute ;)
please, could you show a little example of calling this ?
Dave
1) Modify ReadMultiVars and WriteMultiVars in snap7.net.cs:
2) Modify S7DataItem in snap7.net.cs:
3) Calling example:
Last edit: compactview 2014-11-02
Thank you very much ;)
I will update the library asap.