Merge pull request #10 from fbarresi/develop

Refactor of sharp7 with tests and coverage
This commit is contained in:
Federico Barresi
2020-05-06 23:31:18 +02:00
committed by GitHub
3 changed files with 692 additions and 555 deletions

View File

@@ -0,0 +1,144 @@
using System;
using System.Linq;
using Shouldly;
using Xunit;
namespace Sharp7.Tests
{
public class ClientWithoutServer
{
private S7Client client;
public ClientWithoutServer()
{
client = new S7Client();
}
public new void Dispose()
{
client.Disconnect();
}
[Fact]
public void CannotConnectTest()
{
var rc = client.ConnectTo("127.0.1.2", 0, 2);
rc.ShouldBe(Sharp7.S7Consts.errTCPConnectionFailed);
}
[Fact]
public void GetLastErrorTest()
{
var rc = client.LastError();
rc.ShouldBe(0);
}
[Fact]
public void GetRequestedPduTest()
{
var rc = client.RequestedPduLength();
rc.ShouldBe(480);
client.PduSizeRequested.ShouldBe(480);
}
[Fact]
public void GetNegotiatedPduTest()
{
var rc = client.NegotiatedPduLength();
rc.ShouldBe(0);
client.PduSizeNegotiated.ShouldBe(0);
}
[Fact]
public void SetPlcPortTest()
{
client.PLCPort = 104;
client.PLCPort.ShouldBe(104);
}
[Fact]
public void SetPduRequestedTest()
{
client.PduSizeRequested = 239;
client.PduSizeRequested.ShouldBe(240);
client.PduSizeRequested = 961;
client.PduSizeRequested.ShouldBe(960);
client.PduSizeRequested = 481;
client.PduSizeRequested.ShouldBe(481);
}
[Fact]
public void SetTimeoutTest()
{
client.ConnTimeout = 239;
client.ConnTimeout.ShouldBe(239);
client.RecvTimeout = 239;
client.RecvTimeout.ShouldBe(239);
client.SendTimeout = 239;
client.SendTimeout.ShouldBe(239);
}
[Fact]
public void GetExecTimeTest()
{
client.ExecutionTime.ShouldBe(client.ExecutionTime);
}
[Theory]
[InlineData(1, 0)]
[InlineData(2, 102)]
[InlineData(3, 2000)]
[InlineData(4, 2000)]
[InlineData(5, 2000)]
[InlineData(6, 0)]
[InlineData(7, 0)]
[InlineData(8, 0)]
[InlineData(9, 0)]
[InlineData(10, 480)]
[InlineData(11, 0)]
[InlineData(12, 0)]
[InlineData(13, 0)]
[InlineData(14, 0)]
[InlineData(15, 0)]
public void GetParameterTest(int parameterNumber, int expected)
{
int value = -1;
var result = client.GetParam(parameterNumber, ref value);
if(result == 0)
value.ShouldBe(expected);
else
result.ShouldBe(0x02500000);
}
[Theory]
[InlineData(1, 0)]
[InlineData(2, 103)]
[InlineData(3, 2001)]
[InlineData(4, 2001)]
[InlineData(5, 2001)]
[InlineData(6, 0)]
[InlineData(7, 0)]
[InlineData(8, 0)]
[InlineData(9, 0)]
[InlineData(10, 482)]
[InlineData(11, 0)]
[InlineData(12, 0)]
[InlineData(13, 0)]
[InlineData(14, 0)]
[InlineData(15, 0)]
public void SetParameterTest(int parameterNumber, int newValue)
{
var result = client.SetParam(parameterNumber, ref newValue);
if (result == 0)
{
int readValue = -1;
client.GetParam(parameterNumber, ref readValue);
readValue.ShouldBe(newValue);
}
else
result.ShouldBe(0x02500000);
}
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -2163,56 +2163,56 @@ namespace Sharp7
switch (Error) switch (Error)
{ {
case 0: return "OK"; case 0: return "OK";
case S7Consts.errTCPSocketCreation: return "SYS : Error creating the Socket"; case S7Consts.errTCPSocketCreation: return "SYS: Error creating the Socket";
case S7Consts.errTCPConnectionTimeout: return "TCP : Connection Timeout"; case S7Consts.errTCPConnectionTimeout: return "TCP: Connection Timeout";
case S7Consts.errTCPConnectionFailed: return "TCP : Connection Error"; case S7Consts.errTCPConnectionFailed: return "TCP: Connection Error";
case S7Consts.errTCPReceiveTimeout: return "TCP : Data receive Timeout"; case S7Consts.errTCPReceiveTimeout: return "TCP: Data receive Timeout";
case S7Consts.errTCPDataReceive: return "TCP : Error receiving Data"; case S7Consts.errTCPDataReceive: return "TCP: Error receiving Data";
case S7Consts.errTCPSendTimeout: return "TCP : Data send Timeout"; case S7Consts.errTCPSendTimeout: return "TCP: Data send Timeout";
case S7Consts.errTCPDataSend: return "TCP : Error sending Data"; case S7Consts.errTCPDataSend: return "TCP: Error sending Data";
case S7Consts.errTCPConnectionReset: return "TCP : Connection reset by the Peer"; case S7Consts.errTCPConnectionReset: return "TCP: Connection reset by the Peer";
case S7Consts.errTCPNotConnected: return "CLI : Client not connected"; case S7Consts.errTCPNotConnected: return "CLI: Client not connected";
case S7Consts.errTCPUnreachableHost: return "TCP : Unreachable host"; case S7Consts.errTCPUnreachableHost: return "TCP: Unreachable host";
case S7Consts.errIsoConnect: return "ISO : Connection Error"; case S7Consts.errIsoConnect: return "ISO: Connection Error";
case S7Consts.errIsoInvalidPDU: return "ISO : Invalid PDU received"; case S7Consts.errIsoInvalidPDU: return "ISO: Invalid PDU received";
case S7Consts.errIsoInvalidDataSize: return "ISO : Invalid Buffer passed to Send/Receive"; case S7Consts.errIsoInvalidDataSize: return "ISO: Invalid Buffer passed to Send/Receive";
case S7Consts.errCliNegotiatingPDU: return "CLI : Error in PDU negotiation"; case S7Consts.errCliNegotiatingPDU: return "CLI: Error in PDU negotiation";
case S7Consts.errCliInvalidParams: return "CLI : invalid param(s) supplied"; case S7Consts.errCliInvalidParams: return "CLI: Invalid param(s) supplied";
case S7Consts.errCliJobPending: return "CLI : Job pending"; case S7Consts.errCliJobPending: return "CLI: Job pending";
case S7Consts.errCliTooManyItems: return "CLI : too may items (>20) in multi read/write"; case S7Consts.errCliTooManyItems: return "CLI: Too many items (>20) in multi read/write";
case S7Consts.errCliInvalidWordLen: return "CLI : invalid WordLength"; case S7Consts.errCliInvalidWordLen: return "CLI: Invalid WordLength";
case S7Consts.errCliPartialDataWritten: return "CLI : Partial data written"; case S7Consts.errCliPartialDataWritten: return "CLI: Partial data written";
case S7Consts.errCliSizeOverPDU: return "CPU : total data exceeds the PDU size"; case S7Consts.errCliSizeOverPDU: return "CPU: Total data exceeds the PDU size";
case S7Consts.errCliInvalidPlcAnswer: return "CLI : invalid CPU answer"; case S7Consts.errCliInvalidPlcAnswer: return "CLI: Invalid CPU answer";
case S7Consts.errCliAddressOutOfRange: return "CPU : Address out of range"; case S7Consts.errCliAddressOutOfRange: return "CPU: Address out of range";
case S7Consts.errCliInvalidTransportSize: return "CPU : Invalid Transport size"; case S7Consts.errCliInvalidTransportSize: return "CPU: Invalid Transport size";
case S7Consts.errCliWriteDataSizeMismatch: return "CPU : Data size mismatch"; case S7Consts.errCliWriteDataSizeMismatch: return "CPU: Data size mismatch";
case S7Consts.errCliItemNotAvailable: return "CPU : Item not available"; case S7Consts.errCliItemNotAvailable: return "CPU: Item not available";
case S7Consts.errCliInvalidValue: return "CPU : Invalid value supplied"; case S7Consts.errCliInvalidValue: return "CPU: Invalid value supplied";
case S7Consts.errCliCannotStartPLC: return "CPU : Cannot start PLC"; case S7Consts.errCliCannotStartPLC: return "CPU: Cannot start PLC";
case S7Consts.errCliAlreadyRun: return "CPU : PLC already RUN"; case S7Consts.errCliAlreadyRun: return "CPU: PLC already RUN";
case S7Consts.errCliCannotStopPLC: return "CPU : Cannot stop PLC"; case S7Consts.errCliCannotStopPLC: return "CPU: Cannot stop PLC";
case S7Consts.errCliCannotCopyRamToRom: return "CPU : Cannot copy RAM to ROM"; case S7Consts.errCliCannotCopyRamToRom: return "CPU: Cannot copy RAM to ROM";
case S7Consts.errCliCannotCompress: return "CPU : Cannot compress"; case S7Consts.errCliCannotCompress: return "CPU: Cannot compress";
case S7Consts.errCliAlreadyStop: return "CPU : PLC already STOP"; case S7Consts.errCliAlreadyStop: return "CPU: PLC already STOP";
case S7Consts.errCliFunNotAvailable: return "CPU : Function not available"; case S7Consts.errCliFunNotAvailable: return "CPU: Function not available";
case S7Consts.errCliUploadSequenceFailed: return "CPU : Upload sequence failed"; case S7Consts.errCliUploadSequenceFailed: return "CPU: Upload sequence failed";
case S7Consts.errCliInvalidDataSizeRecvd: return "CLI : Invalid data size received"; case S7Consts.errCliInvalidDataSizeRecvd: return "CLI: Invalid data size received";
case S7Consts.errCliInvalidBlockType: return "CLI : Invalid block type"; case S7Consts.errCliInvalidBlockType: return "CLI: Invalid block type";
case S7Consts.errCliInvalidBlockNumber: return "CLI : Invalid block number"; case S7Consts.errCliInvalidBlockNumber: return "CLI: Invalid block number";
case S7Consts.errCliInvalidBlockSize: return "CLI : Invalid block size"; case S7Consts.errCliInvalidBlockSize: return "CLI: Invalid block size";
case S7Consts.errCliNeedPassword: return "CPU : Function not authorized for current protection level"; case S7Consts.errCliNeedPassword: return "CPU: Function not authorized for current protection level";
case S7Consts.errCliInvalidPassword: return "CPU : Invalid password"; case S7Consts.errCliInvalidPassword: return "CPU: Invalid password";
case S7Consts.errCliNoPasswordToSetOrClear: return "CPU : No password to set or clear"; case S7Consts.errCliNoPasswordToSetOrClear: return "CPU: No password to set or clear";
case S7Consts.errCliJobTimeout: return "CLI : Job Timeout"; case S7Consts.errCliJobTimeout: return "CLI: Job Timeout";
case S7Consts.errCliFunctionRefused: return "CLI : function refused by CPU (Unknown error)"; case S7Consts.errCliFunctionRefused: return "CLI: Function refused by CPU (Unknown error)";
case S7Consts.errCliPartialDataRead: return "CLI : Partial data read"; case S7Consts.errCliPartialDataRead: return "CLI: Partial data read";
case S7Consts.errCliBufferTooSmall: return "CLI : The buffer supplied is too small to accomplish the operation"; case S7Consts.errCliBufferTooSmall: return "CLI: The buffer supplied is too small to accomplish the operation";
case S7Consts.errCliDestroying: return "CLI : Cannot perform (destroying)"; case S7Consts.errCliDestroying: return "CLI: Cannot perform (destroying)";
case S7Consts.errCliInvalidParamNumber: return "CLI : Invalid Param Number"; case S7Consts.errCliInvalidParamNumber: return "CLI: Invalid Param Number";
case S7Consts.errCliCannotChangeParam: return "CLI : Cannot change this param now"; case S7Consts.errCliCannotChangeParam: return "CLI: Cannot change this param now";
case S7Consts.errCliFunctionNotImplemented: return "CLI : Function not implemented"; case S7Consts.errCliFunctionNotImplemented: return "CLI: Function not implemented";
default: return "CLI : Unknown error (0x" + Convert.ToString(Error, 16) + ")"; default: return "CLI: Unknown error (0x" + Convert.ToString(Error, 16) + ")";
}; };
} }