bytedreamer / OSDP.Net

A .NET Core control panel implementation of the Open Supervised Device Protocol(OSDP)
Apache License 2.0
47 stars 19 forks source link

Problem with discover (or connection) #177

Closed FKral closed 2 months ago

FKral commented 2 months ago

Hello, I have tried to connect from RPI on Debian 12 aarch64 without any success.

Discovery and connection works from Console, image so I copied all values that worked directly into code:

using OSDP.Net;
using OSDP.Net.Connections;
using OSDP.Net.PanelCommands.DeviceDiscover;

namespace App;

class Program
{
    public static async Task Main(string[] args) {
        Console.WriteLine("Discover");
        ControlPanel panel = new ControlPanel();
        await panel.Shutdown();

        void OnProgress(DiscoveryResult current)
        {
            string additionalInfo = "";

            switch(current.Status)
            {
                case DiscoveryStatus.Started:
                    System.Console.WriteLine("Device Discovery Started");
                    // NOTE Unlike other statuses, for this one we are intentionally not dropping down
                    return;
                case DiscoveryStatus.LookingForDeviceOnConnection:
                    System.Console.WriteLine($"Connection baud rate {current.Connection.BaudRate}...");
                    break;
                case DiscoveryStatus.ConnectionWithDeviceFound:
                    System.Console.WriteLine($"Connection baud rate {current.Connection.BaudRate}");
                    break;
                case DiscoveryStatus.LookingForDeviceAtAddress:
                    System.Console.WriteLine($"Address {current.Address}...");
                    break;
            }

            System.Console.WriteLine($"Device Discovery Progress: {current.Status}{additionalInfo}");
        }

        panel.ConnectionStatusChanged += (_, eventArgs) => {
            Console.WriteLine("ConnectionStatusChanged");
        };
        panel.NakReplyReceived += (_, args) => {
            Console.WriteLine("Nak recieved");
        };        

        var result = await panel.DiscoverDevice(
            SerialPortOsdpConnection.EnumBaudRates("/dev/ttyS0"),       // port
            new DiscoveryOptions {
                ProgressCallback = OnProgress,
                ResponseTimeout = TimeSpan.FromMilliseconds(1000),      // response timeout
                ReconnectDelay = TimeSpan.FromMilliseconds(0)           // delay
            }.WithDefaultTracer(true)
        );
        if (result != null)
            Console.WriteLine(result);
        else {
            Console.WriteLine("Not found");
            return;
        }
    }
}

dotnet publish proj.csproj --runtime linux-arm64 -p:PublishSingleFile=true --self-contained true

So I have to use library in the wrong way, any ideas how to make it work? Output image Console image

bytedreamer commented 2 months ago

I don't see anything wrong with the code.

Try connecting without discovery, by adding device using the baud rate and address. Add logging to see if there are any errors.

FKral commented 2 months ago

Ok, I probably found the issue

working build: dotnet publish proj.csproj --runtime linux-arm64 --self-contained true

bytedreamer commented 2 months ago

Try adding -p:IncludeNativeLibrariesForSelfExtract=true parameter when running -p:PublishSingleFile=true. I had to use it in the CI build scripts. There are native libraries for accessing the serial port.

FKral commented 2 months ago

Thats it, thank you very much