Skip to content

Conversation

@Jason-Jelks
Copy link
Owner

Classes can now include String properties by adding the "S7StringAttribute" to the Properties to define maximum string size. The Attribute is required for Class type implementing a String.

Classes can now include DateTime Properties. The internal call structures now support passing the current CPU instantiated so that the proper DateTime structure is implemented based on the PLC.
S7-1200 & S71500 : use DateTimeLong (DTL - 12-bytes)
all others use: older DT 8-byte format as they don't support the DTL
The CPU parameter does not break existing implementations, but does default direct calls made by an external source to S7-300 if not defined.

timverwaal and others added 30 commits December 19, 2019 10:55
Types/Class: Start arrays on even bytes.

Close #175, #220.
Tests/TypeTests: Add ClassTests from #178
PR #246 included most types also included in #277, this adds OB, OW and
OD that were only in #277.

Close #277.
PLCAddress: Add OB, OW, OD types from PR #277
PLC: Improve exceptions on Read
Add new class Types.Dtl by taking the DateTime type and adjusting things.

Also add unit test with binary data calculated by hand. (Need to verify with actual S7 data)
Convert UnitTest project to SDK project type.
- Change build image to VS 2019
- Update Microsoft.SourceLink.GitHub package
- Update solution file
- Build separate symbol package (.snupkg)
This requires reference types that can be null to be annotated by a ? operator, similar to value types.

This gives the advantage that the compiler can warn against any null dereference exceptions, of which this commits elimits a few.

To make the underlying protocol implementation not any more complicated and to eliminate existing problems, and not that precise error reporting, I replaced some return null statements with explicit Exceptions. This lead to the assumption that those core protocoll functions always return non-null objects if they do not throw, making the PLC code simpler.

Adjust some NotConnected tests to look for explicit PlcException instead of NullReferenceException.
Add nullable support throughout the whole library.
Both Synchronous and Asynchronous need to build the same binary data package to write a bytes array. Move that package building out into a common function.

Also use IEnumerable to pass in data instead of converting it to array and back multiple times. Not that happy with the whole ByteArray class, we could probably just use a MemoryStream instead.
Merges data creation between sync and async for writing bit values.
Instead of using a loop, use Array.Copy to
mycroes and others added 6 commits June 7, 2021 22:23
Default ReadTimeout and WriteTimeout to 10 seconds
Add unit test to ensure a connection has been properly closed
Use consistent DWord conversion for both Int32 and UInt32. Unfortunately there is no Span or even a FromByteArray function accepting a offset, so just use the same Array.Copy falls used for double.
@dhullett08
Copy link

dhullett08 commented Oct 6, 2021

I tried using the new string functionality for classes and it wouldn't work. It works perfectly for structs but I had to change from using the underlying type of the property to propertyinfo to be able to get the S7StringAttribute as well as increment numBytes with the ReservedLengthInBytes property.

// new get property value
private static object? GetPropertyValue(PropertyInfo property, byte[] bytes, ref double numBytes, CpuType cpu) `

// getting attribute from instance not type and increment numBytes
S7StringAttribute? attribute = property.GetCustomAttributes().SingleOrDefault();

if (attribute == default(S7StringAttribute))
throw new ArgumentException("Please add S7StringAttribute to the string property");
var parsebytes = new byte[attribute.ReservedLength];
Array.Copy(bytes, (int)numBytes, parsebytes, 0, attribute.ReservedLength);
numBytes += attribute.ReservedLengthInBytes;
value = attribute.Type switch
{
S7StringType.S7String => S7String.FromByteArray(parsebytes),
S7StringType.S7WString => S7WString.FromByteArray(parsebytes),
_ => throw new ArgumentException("Please use a valid string type for the S7StringAttribute")
};

@Jason-Jelks
Copy link
Owner Author

Thanks for the heads up, I will check it.
Which framework are you using .Net Framework, .Net Standard or .NET5?
What model of PLC are you connecting too?

@Jason-Jelks
Copy link
Owner Author

@dhullett08 - It appears yesterday's commit had some merge issues, which I don't have time to look into at this moment. feel free to pull from my tree: jjelks for the latest update, which is working for me on the S7-1200 & S7-1500.

@dhullett08
Copy link

@Jason-Jelks I am using S71500S software PLC on a Siemens Microbox PC and .NET 5.0. I tried full framework as well and I couldn't get classes to work? Thats when I went and started digging on MSDN and noticed that it states to GetCustomAttributes of a property it has to be a PropertyInfo object and the method needs called on the instance, not the underlying type (System.String). For the Struct using FieldInfo works because it is for that instance of a Field. I'm not sure what the disconnect is if it works for you.

gfoidl and others added 19 commits December 26, 2021 19:26
…alize S7String, instead of always using Encoding.ASCII
Allow changing the default Encoding used in S7String
Flowed cancellation token to TcpClient.ConnectAsync in .NET 5.0 target
Added support for string/wstring in a class
ci: Run test on ubuntu-20.04 due to lack of snap7 on newer ubuntu
add Read/WriteBytes(Async) overloads accepting Span<byte>/Memory<byte> for .NET5 or greater
Use System.Memory for < .NET 5 and avoid (some) unnecessary allocations
@mycroes mycroes deleted the develop branch May 30, 2023 20:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.