Read data from PLC with Delphi and libnodave library

2.1k Views Asked by At

I’m here again with a new question; this time about PLC.

I start by saying I’m new of PLC and I’ve never saw one of them until a couple of month ago. I’m asked to write a program that read, from Delphi, some data from a PLC Siemens S7-300 in order to archive them in a SQL Server database. I’m using the “libnodave” library.

The program is quite simple. I must verify a bit and when it is on I have to read the data from the PLC and set off the bit. With the library I’ve told about I can read and write without problems, but the data I have to read are stored in a group of byte (about 60 bytes), so I’ve to read some bytes, skip some others and read others bytes. Moreover the bit I must test is in the end of this group of bytes.

So I read the entire group of bytes I put the data red in a group of variables and then I test the bit and, if it is on, I store the data into the database. In order to skip the byte I don’t have to read I use this kind of statements:

for i := 1 to 14 do
    daveGetU8(dc);
for i := 1 to 6 do
    daveGetU16(dc);

My questions are these:

  • There is a better way to read the data skipping the ones I don’t have to read?
  • Is it convenient to read the entire group of bytes and after test the bit or is better to make two reading separated?

I say this because I’ve found in internet that the read operations requires some time, so is better to make the minimum numbers of reading possible.

Eros

1

There are 1 best solutions below

2
On

Communicating with a PLC involves some overhead.

You send a request and after some time you receive an answer. Often the communication is through a serial line with limited bandwidth.

The timing then involves:

  • Time to send the request
  • Time for the PLC to respond
  • Time to transfer the response

It is difficult to give a definite answer to your questions, since we don't know how critical the timing is.

Anyway, polling the flag byte only seems like reasonable way to go.

When the flag is set, read the entire block in one command and then clear the flag. Reading the data in small parts to avoid the gaps, is probably more time consuming than reading the entire block at once.

You can make the maths yourself since you know the specifications.

Example: Lets say the baud rate is 9600 baud. This means roughly 1 byte per millisecond transfer time. The command to read is about 10 bytes long and the block answer about 70 bytes (assuming the protocol is binary). The PLC delay time about 50 ms. This adds to 130 ms, while reading the flag only adds to about 70 ms.

Only you can say if the additional polling time of 70 ms is acceptable.


Edit: In a comment it is stated that the communication is via ethernet on a 100+ MBit/s line. In that case, I suggest to read all data in one command and process it in the PC. Timing is of little concern with such bandwidth.