Skip to content

Commit

Permalink
Merge pull request #38 from ChrisPulman/FixForSignedValues
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisPulman authored Oct 20, 2023
2 parents a5e42c0 + 00623cb commit 63017ef
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 19 deletions.
49 changes: 40 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,30 +65,61 @@ plc.Observe<double>("Tag1").Subscribe(x => Console.WriteLine($"Tag1: {x}"));
plc?.GetTag("Tag0")?.SetTagPollIng(true);
```

#### SetPolling

Polling is enabled by default when a Tag is added. You can disable and enable polling on a Tag at any time.
```csharp
// Create a new Tag and with no Polling
plc.AddUpdateTagItem<double>("Tag0", "DB500.DBD0").SetTagPollIng(false);

// Set Polling on Tag
plc?.GetTag("Tag0")?.SetTagPollIng(true);

// Stop Polling on Tag
plc?.GetTag("Tag0")?.SetTagPollIng(false);
```

#### Write to PLC
```csharp
plc.Value<double>("Tag0", 1.0);
```

#### Read PLC CPU Info
```csharp
// Get CPU Info from PLC Async
var cpuInfo = await plc.CpuInfo();

// Get CPU Info from PLC Reactive
plc.GetCpuInfo().Subscribe(info =>
{
});
```

This returns a `CpuInfo` string Array with the following values:
AS Name, Module Name, Copyright, Serial Number, Module Type Name, Order Code, Version.

#### Supported Data Types

- Bool
- Byte
- Int
- DInt
- Real
- LReal
- String
- Word
- DWord
- Bool - DB?.DBX?.?
- Byte - DB?.DBB?
- Byte[] - DB?.DBB? set length to number of bytes when creating array tags - example AddUpdateTagItem<byte[]>(PlcData, "DB100.DBB0", 64) - creates a tag with 64 bytes
- Int - DB?.DBW?
- Int[] - DB?.DBW?
- UInt - DB?.DBW?
- UInt[] - DB?.DBW?
- DInt - DB?.DBD?
- DInt[] - DB?.DBD?
- UDInt - DB?.DBD?
- UDInt[] - DB?.DBD?
- Real - DB?.DBD?
- Real[] - DB?.DBD?
- LReal - DB?.DBD?
- LReal[] - DB?.DBD?
- String - TODO - for the time being use a Byte Array
- Word - DB?.DBW?
- Word[] - DB?.DBW?
- DWord - DB?.DBD?
- DWord[] - DB?.DBD?

Further types will be added in the future.

Expand Down
2 changes: 1 addition & 1 deletion Version.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/main/src/NerdBank.GitVersioning/version.schema.json",
"version": "1.2.2",
"version": "1.2.3",
"publicReleaseRefSpec": [
"^refs/heads/master$",
"^refs/heads/main$"
Expand Down
8 changes: 7 additions & 1 deletion src/S7PlcRx/Core/S7SocketRx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ internal class S7SocketRx : IDisposable
{
private const string Failed = nameof(Failed);
private const string Success = nameof(Success);
private readonly IDisposable _disposable;
private readonly ISubject<Exception> _socketExceptionSubject = new Subject<Exception>();
private IDisposable _disposable;
private bool _disposedValue;
private bool _initComplete;
private bool? _isAvailable;
Expand Down Expand Up @@ -203,6 +203,12 @@ public S7SocketRx(string ip, CpuType plcType, short rack, short slot)
{
_isConnected = _socket.Connected || (_socket.Poll(1000, SelectMode.SelectRead) && _socket.Available == 0);
}
catch (ObjectDisposedException)
{
_isConnected = false;
_disposable.Dispose();
_disposable = Connect.Subscribe();
}
catch (Exception ex)
{
_isConnected = false;
Expand Down
26 changes: 18 additions & 8 deletions src/S7PlcRx/RxS7.cs
Original file line number Diff line number Diff line change
Expand Up @@ -804,19 +804,19 @@ private void GetTagValue(Tag? tag)
return Read<byte>(tag, DataType.DataBlock, dB, dbIndex, VarType.Byte);

case "DBW":
if (tag.Type == typeof(ushort[]))
if (tag.Type == typeof(short[]))
{
return Read<ushort[]>(tag, DataType.DataBlock, dB, dbIndex, VarType.Word);
return Read<short[]>(tag, DataType.DataBlock, dB, dbIndex, VarType.Int);
}

if (tag.Type == typeof(short[]))
if (tag.Type == typeof(short))
{
return Read<short[]>(tag, DataType.DataBlock, dB, dbIndex, VarType.Word);
return Read<short>(tag, DataType.DataBlock, dB, dbIndex, VarType.Int);
}

if (tag.Type == typeof(short))
if (tag.Type == typeof(ushort[]))
{
return Read<short>(tag, DataType.DataBlock, dB, dbIndex, VarType.Word);
return Read<ushort[]>(tag, DataType.DataBlock, dB, dbIndex, VarType.Word);
}

return Read<ushort>(tag, DataType.DataBlock, dB, dbIndex, VarType.Word);
Expand All @@ -842,9 +842,19 @@ private void GetTagValue(Tag? tag)
return Read<float[]>(tag, DataType.DataBlock, dB, dbIndex, VarType.Real);
}

if (tag.Type == typeof(int))
{
return Read<int>(tag, DataType.DataBlock, dB, dbIndex, VarType.DInt);
}

if (tag.Type == typeof(int[]))
{
return Read<int[]>(tag, DataType.DataBlock, dB, dbIndex, VarType.DInt);
}

if (tag.Type == typeof(uint[]))
{
return Read<uint>(tag, DataType.DataBlock, dB, dbIndex, VarType.DWord);
return Read<uint[]>(tag, DataType.DataBlock, dB, dbIndex, VarType.DWord);
}

return Read<uint>(tag, DataType.DataBlock, dB, dbIndex, VarType.DWord);
Expand Down Expand Up @@ -1276,7 +1286,7 @@ private bool Write(Tag tag, DataType dataType, int db, int startByteAdr, object?
byte[] package;
switch (tag.Type.Name)
{
// TODO: fix Bit
case "Boolean":
case "Byte":
package = new byte[] { (byte)(value ?? Convert.ChangeType(tag.NewValue, typeof(byte)))! };
break;
Expand Down

0 comments on commit 63017ef

Please sign in to comment.