Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ArgumentOutOfRangeException when executing the method DBWrite #38

Open
PozitronikAkr opened this issue Jul 3, 2023 · 1 comment
Open
Assignees
Labels
bug Something isn't working enhancement New feature or request good first issue Good for newcomers

Comments

@PozitronikAkr
Copy link

PozitronikAkr commented Jul 3, 2023

Hi !

There is no physical connection.

The DBRead method predictably returns error number 9. CLI : Client not connected.
The DB Write method returns an exception System.ArgumentOutOfRangeException.
And it should, as it seemed to me, also return error number 9.
Why is this not the case?

namespace TestSharp7ConsoleApplication
{
    class Program
    {
        static void Main()
        {
            int result;
            byte[] db = new byte[20];
            S7Client client = new S7Client();
            client.ConnectTo("192.168.0.2", 0, 1);
            try
            {                
                result = client.DBRead(10, 0, db.Length, db);
                Console.WriteLine($"{result} : {client.ErrorText(result)}");
                result = client.DBWrite(10, 0, db.Length, db);
                Console.WriteLine($"{result} : {client.ErrorText(result)}");
            }
            catch (System.ArgumentOutOfRangeException e)
            {
                result = -1;
                Console.WriteLine($"{result} : {e.Message}");
            }
            Console.WriteLine();
            Console.WriteLine("Press <Enter> for exit");
            Console.ReadLine();        
        }
    }
}
@fbarresi fbarresi self-assigned this Jul 30, 2023
@fbarresi fbarresi added bug Something isn't working enhancement New feature or request good first issue Good for newcomers labels Jul 30, 2023
@fbarresi
Copy link
Owner

Hi!

Please apologize my delayed answer.

Thank you for pointing out this behavior.
I understand the problem.
Actually the write function will prepare the data telegram regardless of the connection state.
This is causing the different exception.

I will debug it and release a new version after the testing phase.

Apart this: is always a good practice to wait for connection established before start to read or write stuff.

If you need such a function that waits for the connection state you might give a try to Sharp7Rx a superimposition of Sharp7.

In this way you can dynamically wait for the connection state and you have some other comfortably function such an internal scheduler for thread safe operations.

await plc.ConnectionState
             .FirstAsync(c => c == Sharp7.Rx.Enums.ConnectionState.Connected)
             .ToTask();

Best regards,
FB

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants