Write bit to PLC using DBWrite doesn't give the right result (result = 3145782)

1.7k Views Asked by At

I have a Siemens S7-1212 AC/DC/Rly PLC and i want communicate with a C# application using Snap7. I have successfully made a connection to the PLC in the UI and i can even successfully write values to the PLC. Sometimes however (1 in 3 times) the write function doesnt write the values to the PLC and gives a very high integer as result.

C# code: The connection with the PLC has been established with the following code:

private S7Client Client = new S7Client();
Client.ConnectTo("192.186.0.1", 0, 0);

The write function:

byte[] writeBuffer = new byte[1];
S7.SetBitAt(ref writeBuffer, 0, 0, true);
int result = Client.DbWrite(1,0,writeBuffer.Length, writeBuffer);
if (result == 0)
     // connection successfull
else 
     // Connection unsuccessfull

Now sometimes when i execute the function above the result is 0, so the write action has been executed successfully. I can see the value change to true in the PLC program. But sometimes (about 1 in 3 times) the result is a very high value, most of the times 3145728 (sometimes another number but mostly this). In this case the function does run (no exeption or error given), but the value does not change.

My question:

Why do i get this high value as result and what does it mean?

Thanks in advance, Theo

0

There are 0 best solutions below