简体   繁体   English

在多个应用程序之间共享单个资源(串行端口)

[英]Sharing a single resource (serial port) between multiple applications

I need some architectural guidance. 我需要一些架构指导。 Here's my objective. 这是我的目标。

I have a piece of robotic hardware that performs multiple functions (ie it has several distinct logical devices). 我有一个执行多种功能的机器人硬件(即,它具有几个不同的逻辑设备)。 For the sake of illustration, let's say it is a robot arm with interchangeable attachments. 为了说明起见,假设它是带有可互换附件的机械手。 The hardware has a single serial port that it uses to connect to a PC, so that it can be controlled by various different applications. 该硬件具有用于连接到PC的单个串行端口,因此可以由各种不同的应用程序控制。 The arm itself and the interchangeable attachments are addressable over the single serial port. 臂本身和可互换的附件可通过单个串行端口寻址。

I need to provide a service, the exposes several interfaces. 我需要提供服务,公开了几个接口。 The interfaces might be called something like: 这些接口可能称为:

IRobotArm IRobotArm
IGrabberAttachment IGrabberAttachment
IDrillAttachment IDrillAttachment

and so on. 等等。 Now, here's the tricky part. 现在,这是棘手的部分。 Each interface needs to be accessed by completely different, isolated applications. 每个接口都需要完全不同的隔离应用程序访问。 So the robot arm and the grabber attachment might be controlled by completely different applications, simultaneously - or they might be controlled by the same application. 因此,机械手和抓取器附件可能同时由完全不同的应用程序控制-或它们可能由同一应用程序控制。

So that's my architectural puzzle. 这就是我的建筑难题。 How do I provide multiple interfaces to multiple client applications, while ensuring only one instance of the serial port is created and that commands can be correctly serialized, etc. 如何为多个客户端应用程序提供多个接口,同时确保仅创建一个串行端口实例,并且可以正确地序列化命令等。

One more additional requirement: I'll be working in .NET/C# but the client applications may well expect to use a COM interface, so the solution needs to work with COM Interop. 另一个附加要求:我将在.NET / C#中工作,但是客户端应用程序可能希望使用COM接口,因此该解决方案需要与COM Interop一起使用。

Ideas please! 请点子!

You need one controller process that actually opens the serial port and sends/receives data. 您需要一个控制器进程来实际打开串行端口并发送/接收数据。

Your DLLs exposing those interfaces should communicate with the controller process, not the serial port directly. 公开这些接口的DLL应该与控制器进程通信,而不是直接与串行端口通信。

If your controller process (the part that opens/closes port and does transmit and receive) is accessible by several applications simultaneously you are going to need to put some synchronization mechanisms in place, otherwise two applications could attempt to send a message simultaneously, which could cause problems parsing the response from the robot arms. 如果您的控制器进程(打开/关闭端口并进行发送和接收的部分)可以同时被多个应用程序访问,则您将需要采用一些同步机制,否则,两个应用程序可能会尝试同时发送消息,这可能导致无法解析机器人手臂的响应。

Assuming you have a single controller process or service running, you could just use the C# lock keyword, like shown below (assuming a simple ASCII type protocol) 假设您正在运行单个控制器进程或服务,则可以只使用C#lock关键字,如下所示(假设使用简单的ASCII类型协议)

This only works if the controller is in its own process, the "lock" keyword will not work cross process. 这仅在控制器处于其自己的进程中时才有效,而“ lock”关键字在跨进程中将不起作用。 Also don't forget to Dispose of the SerialPort class when you shut down. 另外,当您关闭计算机时,也不要忘记处理SerialPort类。

    Object _access = new Object();
    SerialPort _port;

    public void Open()
    {
        lock( _port )
        {
            if( _port == null )
            {
                _port = new SerialPort("COM1", 9600, Parity.None, 8,StopBits.One );
                _port.Open();
                _port.NewLine = "\r";
            }
        }
    }

    public void Close()
    {
       // TBD
    }

    public string SendAndRecieve( string cmd )
    {
        String response;

        lock( _access )
        {
            // Assume port is already opened
            _port.WriteLine(cmd);
            response = _port.ReadLine();
        }

        return response;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM