Im working with S7-300 and c#. Im trying to handle with Counters but with no luck. I receive something to buffer, but how to get number? S7.GetIntAt doesnt work. Here is part of code:
ushort[] Buffer = new ushort[4];
int Result;
Result = Client.CTRead(77, 4, Buffer);
txt.Text = "C77 = " + S7.GetIntAt(Buffer, 0).ToString();
Thanks for advices!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi,
you cannot use S7.GetIntAt() because it needs of an array of byte, CTRead fills an array of ushort (unsigned 16 bit integer).
Moreover the counter value is not an integer but a BCD (an hex value with digits between 0..9).
So there's no chance without coding something...
ushort[] Buffer = new ushort[4];
int Result;
Result = Client.CTRead(77, 4, Buffer);
txt.Text = "C77 = " + S7.GetCounterAt(Buffer,0).ToString();
Remember that you are reading an array of 4 counters (it's not a byte[] buffer)
i.e. you are reading C77 into Buffer[0], C78 into Buffer[1] and so on..
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thanks a lot - it works.
Glad to know you are going to release new version of library. Will it contain this counter reading and writing and timer handle?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
If the library is updated to include built-in methods for reading and writing counters and handling timers, that would make development much easier for people working with S7 PLCs. Counters are a common part of industrial logic, and having a direct function instead of manually decoding BCD values would simplify the code a lot. It’s similar to how a number Counter works in general software—once the value is properly converted and exposed by the library, developers can simply read and display it without worrying about the underlying format.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi guys,
Im working with S7-300 and c#. Im trying to handle with Counters but with no luck. I receive something to buffer, but how to get number? S7.GetIntAt doesnt work. Here is part of code:
Thanks for advices!
Hi,
you cannot use S7.GetIntAt() because it needs of an array of byte, CTRead fills an array of ushort (unsigned 16 bit integer).
Moreover the counter value is not an integer but a BCD (an hex value with digits between 0..9).
So there's no chance without coding something...
Add this method to the class S7 into snap7.net.cs
than your code will be:
Remember that you are reading an array of 4 counters (it's not a byte[] buffer)
i.e. you are reading C77 into Buffer[0], C78 into Buffer[1] and so on..
Thanks a lot - it works.
Glad to know you are going to release new version of library. Will it contain this counter reading and writing and timer handle?
If the library is updated to include built-in methods for reading and writing counters and handling timers, that would make development much easier for people working with S7 PLCs. Counters are a common part of industrial logic, and having a direct function instead of manually decoding BCD values would simplify the code a lot. It’s similar to how a number Counter works in general software—once the value is properly converted and exposed by the library, developers can simply read and display it without worrying about the underlying format.